Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6849 | | Comparar con el anterior | Ultima modificación | Ver Log |

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