Proyectos de Subversion LeadersLinked - Services

Rev

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

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