Proyectos de Subversion LeadersLinked - Services

Rev

Rev 302 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 302 Rev 345
Línea 1... Línea 1...
1
<?php
1
<?php
2
 
-
 
3
declare(strict_types=1);
2
declare(strict_types = 1);
4
 
-
 
5
namespace LeadersLinked\Command;
3
namespace LeadersLinked\Command;
Línea 6... Línea 4...
6
 
4
 
7
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
-
 
10
 
-
 
11
 
7
use Symfony\Component\Console\Output\OutputInterface;
12
use LeadersLinked\Mapper\PushMapper;
8
use LeadersLinked\Mapper\PushMapper;
Línea 13... Línea -...
13
use LeadersLinked\Model\Push;
-
 
14
 
9
use LeadersLinked\Model\Push;
15
 
10
 
-
 
11
class ProcessQueuePushCommand extends Command
16
class ProcessQueuePushCommand extends Command
12
{
17
{
13
 
18
    /**
14
    /**
19
     *
15
     *
20
     * @var \Laminas\Db\Adapter\AdapterInterface
16
     * @var \Laminas\Db\Adapter\AdapterInterface
21
     */
17
     */
22
    private $adapter;
18
    private $adapter;
23
    
19
 
24
    /**
20
    /**
25
     *
21
     *
26
     * @var \LeadersLinked\Cache\CacheInterface
22
     * @var \LeadersLinked\Cache\CacheInterface
27
     */
-
 
28
    private $cache;
23
     */
29
    
24
    private $cache;
30
    
25
 
31
    /**
26
    /**
32
     *
27
     *
33
     * @var \Laminas\Log\LoggerInterface
28
     * @var \Laminas\Log\LoggerInterface
34
     */
29
     */
35
    private $logger;
30
    private $logger;
36
    
31
 
37
    /**
32
    /**
38
     *
33
     *
39
     * @var array
34
     * @var array
40
     */
-
 
41
    private $config;
35
     */
42
    
36
    private $config;
43
    
37
 
44
    /**
38
    /**
45
     *
39
     *
46
     * @var \Laminas\Mvc\I18n\Translator
40
     * @var \Laminas\Mvc\I18n\Translator
47
     */
-
 
48
    private $translator;
41
     */
49
    
42
    private $translator;
50
    
43
 
51
    /**
44
    /**
52
     *
45
     *
-
 
46
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
53
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
47
     * @param \LeadersLinked\Cache\CacheInterface $cache
54
     * @param \LeadersLinked\Cache\CacheInterface $cache
48
     * @param
55
     * @param \Laminas\Log\LoggerInterface
49
     *            \Laminas\Log\LoggerInterface
56
     * @param array $config
50
     * @param array $config
57
     * @param \Laminas\Mvc\I18n\Translator $translator
51
     * @param \Laminas\Mvc\I18n\Translator $translator
58
     */
52
     */
59
    public function __construct($adapter, $cache, $logger, $config, $translator)
53
    public function __construct($adapter, $cache, $logger, $config, $translator)
60
    {
54
    {
61
        $this->adapter      = $adapter;
55
        $this->adapter = $adapter;
62
        $this->cache        = $cache;
56
        $this->cache = $cache;
63
        $this->logger       = $logger;
57
        $this->logger = $logger;
64
        $this->config       = $config;
58
        $this->config = $config;
65
        $this->translator   = $translator;
59
        $this->translator = $translator;
66
        
60
 
Línea 67... Línea -...
67
        parent::__construct();
-
 
68
    }
61
        parent::__construct();
69
 
62
    }
70
    
-
 
71
    protected function execute(InputInterface $input, OutputInterface $output) : int
-
 
72
    {
-
 
73
        
63
 
74
 
64
    protected function execute(InputInterface $input, OutputInterface $output): int
Línea 75... Línea 65...
75
 
65
    {
76
        $batch_size = $this->config['leaderslinked.fcm.batch_size'];
66
        $batch_size = $this->config['leaderslinked.fcm.batch_size'];
77
        $output->writeln('Inicio del proceso de la cola de Push');
67
        $output->writeln('Inicio del proceso de la cola de Push');
78
 
68
 
79
        $pushCompleted = 0;
69
        $pushCompleted = 0;
80
        $pushError = 0;
-
 
81
        
70
        $pushError = 0;
82
        $pushMapper = PushMapper::getInstance($this->adapter);
-
 
83
        $pushs = $pushMapper->fetchBatch($batch_size);
71
 
84
        
72
        $pushMapper = PushMapper::getInstance($this->adapter);
85
      
-
 
86
        
73
        $pushs = $pushMapper->fetchBatch($batch_size);
87
        if($pushs) {
74
 
88
            foreach($pushs as $push)
75
        if ($pushs) {
Línea 89... Línea -...
89
            {
-
 
90
                $data = json_decode($push->data, true);
76
            foreach ($pushs as $push) {
91
                $auth_key = $data['server']['key'] ;
77
                $data = json_decode($push->data, true);
92
                $fields = $data['push'];
78
                $auth_key = $data['server']['key'];
93
 
79
                $fields = $data['push'];
94
                
-
 
95
                $headers = [
80
 
96
                    'Authorization: key=' . $auth_key,
81
                $headers = [
97
                    'Content-Type: application/json'
82
                    'Authorization: key=' . $auth_key,
98
                ];
83
                    'Content-Type: application/json'
99
                
84
                ];
100
                    
85
 
101
                $ch = curl_init();
86
                $ch = curl_init();
102
                curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
87
                curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
103
                curl_setopt( $ch,CURLOPT_POST, true );
88
                curl_setopt($ch, CURLOPT_POST, true);
104
                curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
89
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
105
                curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
90
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
106
                curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
91
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
107
                curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
92
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
108
                $result = curl_exec($ch );
93
                $result = curl_exec($ch);
Línea 109... Línea 94...
109
                curl_close( $ch );
94
                curl_close($ch);
110
 
95
 
111
                if($result) {
96
                if ($result) {
112
                    $json = json_decode($result, true); 
97
                    $json = json_decode($result, true);
113
                    if($json) {
98
                    if ($json) {
114
 
99
 
115
                        $succes = isset($json['success']) ? $json['success'] : 0;
100
                        $succes = isset($json['success']) ? $json['success'] : 0;
116
                        $failure = isset($json['failure']) ? $json['failure'] : 0;
101
                        $failure = isset($json['failure']) ? $json['failure'] : 0;
117
                        
102
 
118
                        $pushError = $pushError + $failure;
103
                        $pushError = $pushError + $failure;
119
                        $pushCompleted = $pushCompleted +  $succes;
104
                        $pushCompleted = $pushCompleted + $succes;
120
                        
105
 
121
                        if($succes) {
106
                        if ($succes) {
122
                            $push->status = Push::STATUS_COMPLETED;
107
                            $push->status = Push::STATUS_COMPLETED;
123
                        } else {
-
 
124
                            $push->status = Push::STATUS_ERROR;
-
 
125
                        }
-
 
126
                    } else {
-
 
127
                        $push->status = Push::STATUS_ERROR;
108
                        } else {
128
                    }
109
                            $push->status = Push::STATUS_ERROR;
129
                    
110
                        }
130
                    
111
                    } else {
131
                    
112
                        $push->status = Push::STATUS_ERROR;
132
                    
-
 
133
                } else {
-
 
134
                    $push->status = Push::STATUS_ERROR;
-
 
135
                }
-
 
136
                
113
                    }
137
                $pushMapper->update($push);
114
                } else {
138
                
115
                    $push->status = Push::STATUS_ERROR;
139
                
116
                }
140
        
117
 
141
                
118
                $pushMapper->update($push);
142
            }
119
            }
143
        }
120
        }
144
        
121
 
145
        $output->writeln('Push con Errores descartados: ' . $pushError);
122
        $output->writeln('Push con Errores descartados: ' . $pushError);
146
        $output->writeln('Push enviados correctamente:'  . $pushCompleted);
-
 
147
        
-
 
148
        $output->writeln('Fin del proceso de la cola de Push');
-
 
149
        
-
 
150
        return 0;
-
 
151
    }
123
        $output->writeln('Push enviados correctamente:' . $pushCompleted);
152
    
124