<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use App\Message\ListingUpdatedMessage;
use Aqarmap\Bundle\ListingBundle\Event\ListingUpdatedEvent;
use Aqarmap\Bundle\ListingBundle\Service\ListingLabelQueueProducer;
use Aqarmap\Bundle\ListingBundle\Service\ListingRateService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class ListingUpdatedListener implements EventSubscriberInterface
{
/** @var ListingRateService */
private $listingRateService;
/** @var UnitOfWork */
private $unitOfWork;
/** @var ListingLabelQueueProducer */
private $listingLabelQueueProducer;
private MessageBusInterface $messageBus;
public function __construct(ListingRateService $listingRateService, EntityManagerInterface $entityManager, ListingLabelQueueProducer $listingLabelQueueProducer, MessageBusInterface $messageBus)
{
$this->listingRateService = $listingRateService;
$this->unitOfWork = $entityManager->getUnitOfWork();
$this->listingLabelQueueProducer = $listingLabelQueueProducer;
$this->messageBus = $messageBus;
}
public function postUpdate($listing, $event): void
{
$this->messageBus->dispatch(new ListingUpdatedMessage($listing->getId()));
$this->unitOfWork->computeChangeSets();
$changedFields = $this->unitOfWork->getEntityChangeSet($listing);
$this->listingLabelQueueProducer->publish($listing, array_keys($changedFields));
}
public function onUpdate(ListingUpdatedEvent $event): void
{
$this->listingRateService->markRateStatusUpdated($event->getListing(), $event->getFields());
}
/**
* {@inheri tdoc}.
*/
public static function getSubscribedEvents()
{
return [
ListingUpdatedEvent::UPDATED => 'onUpdate',
];
}
}