src/EventSubscriber/SuiviConso/SuiviConsoSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber\SuiviConso;
  4. use App\Entity\LeakAlertActivation;
  5. use App\Event\SuiviConso\EndConsentsEvent;
  6. use App\Event\SuiviConso\EndServiceEvent;
  7. use App\Manager\LeakAlertManager;
  8. use App\Repository\App\LeakAlertActivationRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class SuiviConsoSubscriber implements EventSubscriberInterface
  12. {
  13.     private $em;
  14.     private $leakAlertActivationRepository;
  15.     private $leakAlertManager;
  16.     public function __construct(
  17.         EntityManagerInterface $em,
  18.         LeakAlertActivationRepository $leakAlertActivationRepository,
  19.         LeakAlertManager $leakAlertManager
  20.     ) {
  21.         $this->em $em;
  22.         $this->leakAlertActivationRepository $leakAlertActivationRepository;
  23.         $this->leakAlertManager $leakAlertManager;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             EndServiceEvent::class => 'onEndService',
  29.             EndConsentsEvent::class => 'onEndConsents',
  30.         ];
  31.     }
  32.     public function onEndService(EndServiceEvent $event)
  33.     {
  34.         $leakActivations $this->leakAlertActivationRepository
  35.             ->findActiveByUserAndContractInfo($event->getUser(), $event->getPostNumber(), $event->getContractNumber());
  36.         if (!$leakActivations) {
  37.             return;
  38.         }
  39.         foreach ($leakActivations as $activation) {
  40.             $this->leakAlertManager->deactivate($activation);
  41.         }
  42.         $this->em->flush();
  43.     }
  44.     public function onEndConsents(EndConsentsEvent $event)
  45.     {
  46.         $leakActivations $this->leakAlertActivationRepository->findActiveByUser($event->getUser());
  47.         if (!$leakActivations) {
  48.             return;
  49.         }
  50.         foreach ($leakActivations as $activation) {
  51.             $this->leakAlertManager->deactivate($activation);
  52.         }
  53.         $this->em->flush();
  54.     }
  55. }