Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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