Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | Rev 302 | 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
use Laminas\Db\Adapter\AdapterInterface;
12
 
13
use Laminas\Log\LoggerInterface;
14
use LeadersLinked\Mapper\PushMapper;
15
use LeadersLinked\Model\Push;
16
use Laminas\Mvc\I18n\Translator;
17
use LeadersLinked\Cache\CacheInterface;
18
 
19
class ProcessQueuePushCommand extends Command
20
{
21
    /**
22
     *
23
     * @var \Laminas\Db\Adapter\AdapterInterface
24
     */
25
    private $adapter;
26
 
27
    /**
28
     *
29
     * @var \LeadersLinked\Cache\CacheInterface
30
     */
31
    private $cache;
32
 
33
 
34
    /**
35
     *
36
     * @var \Laminas\Log\LoggerInterface
37
     */
38
    private $logger;
39
 
40
    /**
41
     *
42
     * @var array
43
     */
44
    private $config;
45
 
46
 
47
    /**
48
     *
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
59
     * @param array $config
60
     * @param \Laminas\Mvc\I18n\Translator $translator
61
     */
62
    public function __construct($adapter, $cache, $logger, $config, $translator)
63
    {
64
        $this->adapter      = $adapter;
65
        $this->cache        = $cache;
66
        $this->logger       = $logger;
67
        $this->config       = $config;
68
        $this->translator   = $translator;
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) {
283 www 117
 
1 efrain 118
                        $succes = isset($json['success']) ? $json['success'] : 0;
119
                        $failure = isset($json['failure']) ? $json['failure'] : 0;
120
 
121
                        $pushError = $pushError + $failure;
122
                        $pushCompleted = $pushCompleted +  $succes;
123
 
124
                        if($succes) {
125
                            $push->status = Push::STATUS_COMPLETED;
126
                        } else {
127
                            $push->status = Push::STATUS_ERROR;
128
                        }
129
                    } else {
130
                        $push->status = Push::STATUS_ERROR;
131
                    }
132
 
133
 
134
 
135
 
136
                } else {
137
                    $push->status = Push::STATUS_ERROR;
138
                }
139
 
140
                $pushMapper->update($push);
141
 
142
 
143
 
144
 
145
            }
146
        }
147
 
148
        $output->writeln('Push con Errores descartados: ' . $pushError);
149
        $output->writeln('Push enviados correctamente:'  . $pushCompleted);
150
 
151
        $output->writeln('Fin del proceso de la cola de Push');
152
 
153
        return 0;
154
    }
155
 
156
 
157
 
158
 
159
 
160
}