Proyectos de Subversion LeadersLinked - Services

Rev

Rev 283 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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