<?php
namespace Aqarmap\Bundle\CreditBundle\EventListener;
use Aqarmap\Bundle\CreditBundle\Event\ChurnAndExpiryEvent;
use Aqarmap\Bundle\CreditBundle\Services\ChurnAndExpiryManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ChurnAndExpiryEventSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var ChurnAndExpiryManager
*/
private $churnAndExpiryManager;
/**
* ChurnAndExpiryListener constructor.
*/
public function __construct(EntityManagerInterface $em, ChurnAndExpiryManager $churnAndExpiryManager)
{
$this->em = $em;
$this->churnAndExpiryManager = $churnAndExpiryManager;
}
public function onUserRenewal(ChurnAndExpiryEvent $churnAndExpiryEvent): void
{
$account = $churnAndExpiryEvent->getUser();
$staffHistory = $this->em->getRepository('AqarmapCRMBundle:StaffHistory')
->findOneBy(['user' => $account], ['createdAt' => 'DESC']);
if ($staffHistory) {
try {
$this->churnAndExpiryManager->updateChurnHistory($account, $staffHistory);
} catch (\Exception $exception) {
return;
}
}
}
public static function getSubscribedEvents()
{
return [
ChurnAndExpiryEvent::NAME => 'onUserRenewal',
];
}
}