<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Alert;
use App\Entity\TheProperty;
use App\Message\AlertMessage;
use App\Message\ShowInterestMessage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Messenger\MessageBusInterface;
class AlertEventSubscriber implements EventSubscriberInterface
{
/**
* @var MessageBusInterface
*/
private $messageBus;
public function __construct(MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => ['alert', EventPriorities::POST_WRITE],
];
}
public function alert(ViewEvent $event): void
{
$object = $event->getControllerResult();
if ($object instanceof Alert && $object->getAddress() != null) {
$this->messageBus->dispatch(new ShowInterestMessage($object->getId()));
} elseif ($object instanceof TheProperty) {
if ($event->getRequest()->isMethod(Request::METHOD_PUT)) {
$this->messageBus->dispatch(new AlertMessage($object->getId(), true));
} elseif ($event->getRequest()->isMethod(Request::METHOD_POST)) {
$this->messageBus->dispatch(new AlertMessage($object->getId()));
}
}
}
}