src/EventSubscriber/AlertEventSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Alert;
  5. use App\Entity\TheProperty;
  6. use App\Message\AlertMessage;
  7. use App\Message\ShowInterestMessage;
  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. class AlertEventSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var MessageBusInterface
  17.      */
  18.     private $messageBus;
  19.     public function __construct(MessageBusInterface $messageBus)
  20.     {
  21.         $this->messageBus $messageBus;
  22.     }
  23.     /**
  24.      * @return array<string, mixed>
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             KernelEvents::VIEW => ['alert'EventPriorities::POST_WRITE],
  30.         ];
  31.     }
  32.     public function alert(ViewEvent $event): void
  33.     {
  34.         $object $event->getControllerResult();
  35.         if ($object instanceof Alert && $object->getAddress() != null) {
  36.             $this->messageBus->dispatch(new ShowInterestMessage($object->getId()));
  37.         } elseif ($object instanceof TheProperty) {
  38.             if ($event->getRequest()->isMethod(Request::METHOD_PUT)) {
  39.                 $this->messageBus->dispatch(new AlertMessage($object->getId(), true));
  40.             } elseif ($event->getRequest()->isMethod(Request::METHOD_POST)) {
  41.                 $this->messageBus->dispatch(new AlertMessage($object->getId()));
  42.             }
  43.         }
  44.     }
  45. }