<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use Aqarmap\Bundle\CreditBundle\Constant\CreditStatus;
use Aqarmap\Bundle\ListingBundle\Entity\ListingFeature;
use Aqarmap\Bundle\ListingBundle\Event\ListingEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CreditStatusListener implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* Constructor.
*
* @param EntityManager $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
public function onListingPublishEvent(ListingEvent $event): void
{
$listing = $event->getListing();
$featuredListings = $listing->getListingFeatures();
/** @var ListingFeature $listing */
foreach ($featuredListings as $listing) {
$credit = $listing->getCredit();
if ($credit && CreditStatus::PENDING == $credit->getStatus()) {
$credit->setStatus(CreditStatus::SUCCESS);
$this->em->persist($credit);
$this->em->flush($credit);
}
}
}
public static function getSubscribedEvents()
{
return [
'aqarmap.listing.publish' => [
['onListingPublishEvent', 100],
],
];
}
}