Rev 6749 | Ir a la última revisión | 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 Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\LoggerInterface;
use LeadersLinked\Mapper\PushMapper;
use LeadersLinked\Model\Push;
class ProcessQueuePushCommand extends Command
{
/**
*
* @var AdapterInterface
*/
private $adapter;
/**
*
* @var LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @param AdapterInterface $adapter
* @param LoggerInterface $logger
* @param array $config
*/
public function __construct($adapter, $logger, $config)
{
$this->adapter = $adapter;
$this->logger = $logger;
$this->config = $config;
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) {
//{"multicast_id":8921821776092049363,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
//print_r($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;
}
}