src/Aqarmap/Bundle/ListingBundle/EventListener/CreditStatusListener.php line 28

  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\CreditBundle\Constant\CreditStatus;
  4. use Aqarmap\Bundle\ListingBundle\Entity\ListingFeature;
  5. use Aqarmap\Bundle\ListingBundle\Event\ListingEvent;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class CreditStatusListener implements EventSubscriberInterface
  9. {
  10. /**
  11. * @var EntityManagerInterface
  12. */
  13. protected $em;
  14. /**
  15. * Constructor.
  16. *
  17. * @param EntityManager $entityManager
  18. */
  19. public function __construct(EntityManagerInterface $entityManager)
  20. {
  21. $this->em = $entityManager;
  22. }
  23. public function onListingPublishEvent(ListingEvent $event): void
  24. {
  25. $listing = $event->getListing();
  26. $featuredListings = $listing->getListingFeatures();
  27. /** @var ListingFeature $listing */
  28. foreach ($featuredListings as $listing) {
  29. $credit = $listing->getCredit();
  30. if ($credit && CreditStatus::PENDING == $credit->getStatus()) {
  31. $credit->setStatus(CreditStatus::SUCCESS);
  32. $this->em->persist($credit);
  33. $this->em->flush($credit);
  34. }
  35. }
  36. }
  37. public static function getSubscribedEvents(): array
  38. {
  39. return [
  40. 'aqarmap.listing.publish' => [
  41. ['onListingPublishEvent', 100],
  42. ],
  43. ];
  44. }
  45. }