Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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