src/Aqarmap/Bundle/ListingBundle/EventListener/ListingUpdatedListener.php line 43

  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use App\Message\ListingUpdatedMessage;
  4. use Aqarmap\Bundle\ListingBundle\Event\ListingUpdatedEvent;
  5. use Aqarmap\Bundle\ListingBundle\Service\ListingLabelQueueProducer;
  6. use Aqarmap\Bundle\ListingBundle\Service\ListingRateService;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\ORM\UnitOfWork;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Messenger\MessageBusInterface;
  11. class ListingUpdatedListener implements EventSubscriberInterface
  12. {
  13. /** @var UnitOfWork */
  14. private $unitOfWork;
  15. public function __construct(private readonly ListingRateService $listingRateService, EntityManagerInterface $entityManager, private readonly ListingLabelQueueProducer $listingLabelQueueProducer, private readonly MessageBusInterface $messageBus)
  16. {
  17. $this->unitOfWork = $entityManager->getUnitOfWork();
  18. }
  19. public function postUpdate($listing, $event): void
  20. {
  21. $this->messageBus->dispatch(new ListingUpdatedMessage($listing->getId()));
  22. $this->unitOfWork->computeChangeSets();
  23. $changedFields = $this->unitOfWork->getEntityChangeSet($listing);
  24. $this->listingLabelQueueProducer->publish($listing, array_keys($changedFields));
  25. }
  26. public function onUpdate(ListingUpdatedEvent $event): void
  27. {
  28. $this->listingRateService->markRateStatusUpdated($event->getListing(), $event->getFields());
  29. }
  30. /**
  31. * {@inheri tdoc}.
  32. */
  33. public static function getSubscribedEvents(): array
  34. {
  35. return [
  36. ListingUpdatedEvent::UPDATED => 'onUpdate',
  37. ];
  38. }
  39. }