<?php
namespace App\EventSubscriber\Metier;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Doctrine\ORM\EntityManagerInterface;
use App\AppEvents;
use App\Events\Metier\ContratEvent;
use App\Events\Metier\SinistreEvent;
use App\Helpers\UserTypeConst;
use App\Service\User\ExpertUserManager;
use App\Service\Firebase\DoPush;
class ContratSubscriber implements EventSubscriberInterface
{
private $em;
private $token;
private $doPush;
private $usermanager;
public function __construct(EntityManagerInterface $em, TokenStorageInterface $token, ExpertUserManager $usermanager, DoPush $doPush)
{
$this->em = $em;
$this->token = $token;
$this->doPush = $doPush;
$this->usermanager = $usermanager;
}
public function onContratAlert(ContratEvent $event)
{
$contrat = $event->getContrat();
$avenant = $event->getAvenant();
$nbjr = $event->getNombreJour();
$extra = ['type' => UserTypeConst::TYPE_CONTRAT, 'method' => UserTypeConst::METHOD_ALERT, 'id' => $contrat->getId(), 'avenant' => $avenant->getId()];
$message = '';
$notify = $this->em->getRepository('Admin:Parametre')->findOneBy(['cle' => UserTypeConst::PARAMS_ALERT_CONTRAT_MESSAGE]);
if (!is_null($notify)) {
$handler = [$contrat->getNumPolice(), $avenant->getDateExpiration()->format('d/m/Y')];
$holder = ['%num_police%', '%date%'];
$message = str_replace($holder, $handler, $notify->getValeur());
}
$client = $contrat->getClient();
if (!is_null($client)) {
$this->doPush->setNumClient($client->getNumClient())->setExtra($extra)->setTelepohne($client->getTel())->setTitle('myFidelia')->setMessage($message)->doPush();
}
}
public function onSinistreAlert(SinistreEvent $event)
{
$sinistre = $event->getSinistre();
$nbjr = $event->getNombreJour();
$extra = ['type' => UserTypeConst::TYPE_SINISTRE, 'method' => UserTypeConst::METHOD_ALERT, 'id' => $sinistre->getId()];
$message = '';
$notify = $this->em->getRepository('Admin:Parametre')->findOneBy(['cle' => UserTypeConst::PARAMS_ALERT_SINISTRE_MESSAGE]);
if (!is_null($notify)) {
$handler = [$sinistre->getNumSinistre(), $sinistre->getDateEcheance()->format('d/m/Y')];
$holder = ['%num_sinistre', '%date'];
$message = str_replace($holder, $handler, $notify->getValeur());
}
$client = $sinistre->getContrat()->getClient();
$this->doPush->setExtra($extra)->setTelepohne($client->getTel())->setTitle('myFidelia')->setMessage($message)->doPush();
}
public function onAllContratDelete(PostResponseEvent $event)
{
//$controller = $event->getRequest()->get
}
public static function getSubscribedEvents()
{
return [
KernelEvents::TERMINATE => 'onAllContratDelete',
AppEvents::CONTRAT_ALERT => 'onContratAlert',
AppEvents::SINISTRE_ALERT => 'onSinistreAlert'
];
}
}