Proyectos de Subversion LeadersLinked - Services

Rev

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

<?php
declare(strict_types = 1);
namespace LeadersLinked\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use LeadersLinked\Mapper\PushMapper;
use LeadersLinked\Model\Push;

class ProcessQueuePushCommand extends Command
{

    /**
     *
     * @var \Laminas\Db\Adapter\AdapterInterface
     */
    private $adapter;

    /**
     *
     * @var \LeadersLinked\Cache\CacheInterface
     */
    private $cache;

    /**
     *
     * @var \Laminas\Log\LoggerInterface
     */
    private $logger;

    /**
     *
     * @var array
     */
    private $config;

    /**
     *
     * @var \Laminas\Mvc\I18n\Translator
     */
    private $translator;

    /**
     *
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
     * @param \LeadersLinked\Cache\CacheInterface $cache
     * @param
     *            \Laminas\Log\LoggerInterface
     * @param array $config
     * @param \Laminas\Mvc\I18n\Translator $translator
     */
    public function __construct($adapter, $cache, $logger, $config, $translator)
    {
        $this->adapter = $adapter;
        $this->cache = $cache;
        $this->logger = $logger;
        $this->config = $config;
        $this->translator = $translator;

        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $batch_size = $this->config['leaderslinked.fcm.batch_size'];
        $output->writeln('Inicio del proceso de la cola de Push');

        $pushCompleted = 0;
        $pushError = 0;

        $pushMapper = PushMapper::getInstance($this->adapter);
        $pushs = $pushMapper->fetchBatch($batch_size);

        if ($pushs) {
            foreach ($pushs as $push) {
                $data = json_decode($push->data, true);
                $auth_key = $data['server']['key'];
                $fields = $data['push'];

                $headers = [
                    'Authorization: key=' . $auth_key,
                    'Content-Type: application/json'
                ];

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                $result = curl_exec($ch);
                curl_close($ch);

                if ($result) {
                    $json = json_decode($result, true);
                    if ($json) {

                        $succes = isset($json['success']) ? $json['success'] : 0;
                        $failure = isset($json['failure']) ? $json['failure'] : 0;

                        $pushError = $pushError + $failure;
                        $pushCompleted = $pushCompleted + $succes;

                        if ($succes) {
                            $push->status = Push::STATUS_COMPLETED;
                        } else {
                            $push->status = Push::STATUS_ERROR;
                        }
                    } else {
                        $push->status = Push::STATUS_ERROR;
                    }
                } else {
                    $push->status = Push::STATUS_ERROR;
                }

                $pushMapper->update($push);
            }
        }

        $output->writeln('Push con Errores descartados: ' . $pushError);
        $output->writeln('Push enviados correctamente:' . $pushCompleted);

        $output->writeln('Fin del proceso de la cola de Push');

        return 0;
    }
}