src/Aqarmap/Bundle/CreditBundle/EventListener/ChurnAndExpiryEventSubscriber.php line 31

  1. <?php
  2. namespace Aqarmap\Bundle\CreditBundle\EventListener;
  3. use Aqarmap\Bundle\CreditBundle\Event\ChurnAndExpiryEvent;
  4. use Aqarmap\Bundle\CreditBundle\Services\ChurnAndExpiryManager;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class ChurnAndExpiryEventSubscriber implements EventSubscriberInterface
  8. {
  9. /**
  10. * ChurnAndExpiryListener constructor.
  11. */
  12. public function __construct(private readonly EntityManagerInterface $em, private readonly ChurnAndExpiryManager $churnAndExpiryManager)
  13. {
  14. }
  15. public function onUserRenewal(ChurnAndExpiryEvent $churnAndExpiryEvent): void
  16. {
  17. $account = $churnAndExpiryEvent->getUser();
  18. $staffHistory = $this->em->getRepository(\Aqarmap\Bundle\CRMBundle\Entity\StaffHistory::class)
  19. ->findOneBy(['user' => $account], ['createdAt' => 'DESC']);
  20. if ($staffHistory) {
  21. try {
  22. $this->churnAndExpiryManager->updateChurnHistory($account, $staffHistory);
  23. } catch (\Exception) {
  24. return;
  25. }
  26. }
  27. }
  28. public static function getSubscribedEvents(): array
  29. {
  30. return [
  31. ChurnAndExpiryEvent::NAME => 'onUserRenewal',
  32. ];
  33. }
  34. }