src/EventListener/OpensystemSubscriber.php line 77

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\EventListener;
  3. use App\Entity\OpensystemLog;
  4. use App\Event\OpenSystemEvent;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Twig\Environment;
  9. /**
  10.  * Class ApidaeSubscriber
  11.  * @package App\EventListener
  12.  */
  13. class OpensystemSubscriber implements EventSubscriberInterface
  14. {
  15.     /** @var EntityManagerInterface */
  16.     private $entityManager;
  17.     private $mailer;
  18.     private $logger;
  19.     private $template;
  20.     /**
  21.      * UserSubscriber constructor.
  22.      *
  23.      * @param EntityManagerInterface $entityManager
  24.      * @param \Swift_Mailer $mailer
  25.      * @param LoggerInterface $logger
  26.      * @param Environment $template
  27.      */
  28.     public function __construct(EntityManagerInterface $entityManager,
  29.                                 \Swift_Mailer $mailer,
  30.                                 LoggerInterface $logger,
  31.                                 Environment $template)
  32.     {
  33.         $this->entityManager $entityManager;
  34.         $this->mailer   $mailer;
  35.         $this->logger   $logger;
  36.         $this->template $template;
  37.     }
  38.     /**
  39.      * @return array
  40.      */
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             OpenSystemEvent::OPENSYSTEM_AVAILABILITY_ERROR    => ['availabilityError'],
  45.             OpenSystemEvent::OPENSYSTEM_AVAILABILITY_INFO     => ['availabilityInfo']
  46.         ];
  47.     }
  48.     /**
  49.      * @param OpenSystemEvent $event
  50.      */
  51.     public function availabilityError(OpenSystemEvent $event)
  52.     {
  53.         $apiLog = new OpensystemLog();
  54.         $apiLog->setState(OpenSystemEvent::OPENSYSTEM_AVAILABILITY_ERROR);
  55.         $apiLog->setMessage($event->getMessage());
  56.         $apiLog->setCode($event->getCode());
  57.         $apiLog->setObjectId($event->getExperience()->getId());
  58.         $this->entityManager->persist($apiLog);
  59.         $this->entityManager->flush();
  60.     }
  61.     /**
  62.      * @param OpenSystemEvent $event
  63.      * @throws \Twig\Error\LoaderError
  64.      * @throws \Twig\Error\RuntimeError
  65.      * @throws \Twig\Error\SyntaxError
  66.      */
  67.     public function availabilityInfo(OpenSystemEvent $event)
  68.     {
  69.         $returncode $event->getCode();
  70.         $apiLog = new OpensystemLog();
  71.         $apiLog->setState(OpenSystemEvent::OPENSYSTEM_AVAILABILITY_INFO);
  72.         $apiLog->setMessage($event->getMessage());
  73.         $apiLog->setCode($returncode);
  74.         $apiLog->setObjectId($event->getExperience()->getId());
  75.         $this->entityManager->persist($apiLog);
  76.         $this->entityManager->flush();
  77.         if ($returncode>=0){
  78.             switch ($returncode){
  79.                 case 0:
  80.                     $subject '[evasion] Une expérience a été re-publiée';
  81.                     $template 'email/republish.html.twig';
  82.                     break;
  83.                 case 1:
  84.                     $subject '[evasion] Une expérience a été marquée indisponible';
  85.                     $template 'email/unavailability.html.twig';
  86.                     break;
  87.             }
  88.             $message = (new \Swift_Message())
  89.                 ->setSubject($subject)
  90.                 ->setTo([ '[email protected]''[email protected]''[email protected]''[email protected]])
  91.                 ->setBcc([ '[email protected]''[email protected]])
  92.                 ->setFrom(['[email protected]=> 'Isère Attractivité'])
  93.                 ->setBody(
  94.                     $this->template->render($template, [
  95.                         'experience' => $event->getExperience(),
  96.                     ]),
  97.                     'text/html'
  98.                 )
  99.             ;
  100.             try {
  101.                 $this->mailer->send($message);
  102.             } catch (\Exception $e) {
  103.                 $this->logger->error('Error sending email', ['message' => $e->getMessage(), 'code' => $e->getCode()]);
  104.             }
  105.         }
  106.     }
  107. }