src/EventSubscriber/MandateSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Mandate;
  5. use App\Exception\EmailException;
  6. use App\Mailer\Mandate\MandateMailerInterface;
  7. use App\Message\GenerateAdditionalPropertiesMandateMessage;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Messenger\MessageBusInterface;
  13. use Psr\Log\LoggerInterface;
  14. class MandateSubscriber implements EventSubscriberInterface
  15. {
  16.     private MandateMailerInterface $mailer;
  17.     private MessageBusInterface $bus;
  18.     private LoggerInterface $logger;
  19.     /**
  20.      * @param MandateMailerInterface $mailer
  21.      * @param MessageBusInterface    $bus
  22.      * @param LoggerInterface        $logger
  23.      */
  24.     public function __construct(
  25.         MandateMailerInterface $mailer,
  26.         MessageBusInterface $bus,
  27.         LoggerInterface $logger
  28.     ){
  29.         $this->mailer $mailer;
  30.         $this->bus $bus;
  31.         $this->logger $logger;
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             KernelEvents::VIEW => [
  37.                 ['onPost'EventPriorities::POST_WRITE],
  38.                 ['onPut'EventPriorities::POST_WRITE]
  39.             ]
  40.         ];
  41.     }
  42.     /**
  43.      * @param ViewEvent $event
  44.      *
  45.      * @return void
  46.      *
  47.      * @throws EmailException
  48.      */
  49.     public function onPost(ViewEvent $event): void
  50.     {
  51.         $mandate $event->getControllerResult();
  52.         $method $event->getRequest()->getMethod();
  53.         if (!$mandate instanceof Mandate || Request::METHOD_POST !== $method) {
  54.             return;
  55.         }
  56.         if(empty($mandate->getProperty()->getRecommendation())){
  57.             return;
  58.         }
  59.         $this->mailer->notifyRecommended($mandate);
  60.     }
  61.     /**
  62.      * @param ViewEvent $event
  63.      *
  64.      * @return void
  65.      *
  66.      * @throws EmailException
  67.      */
  68.     public function onPut(ViewEvent $event): void
  69.     {
  70.         $mandate $event->getControllerResult();
  71.         $method $event->getRequest()->getMethod();
  72.         if (!$mandate instanceof Mandate || Request::METHOD_PUT !== $method) {
  73.             return;
  74.         }
  75.         $propertyIds = [];
  76.         foreach ($mandate->getAdditionalProperties()->getIterator() as $additionalProperty) {
  77.             $propertyIds[] = $additionalProperty->getId();
  78.         }
  79.         $mandateId $mandate->getId();
  80.         try {
  81.             $message = new GenerateAdditionalPropertiesMandateMessage($mandateId$propertyIds);
  82.             $this->bus->dispatch($message);
  83.         } catch (\Throwable $exception) {
  84.             $this->logger->error(
  85.                 'Failed to publish GenerateAdditionalPropertiesMandateMessage !',
  86.                 [
  87.                     'trace' => $exception->getTrace(),
  88.                     'line' => $exception->getLine(),
  89.                     'message' => $exception->getMessage(),
  90.                     'file' => $exception->getFile(),
  91.                 ]
  92.             );
  93.         }
  94.     }
  95. }