<?php
declare(strict_types=1);
namespace App\EventSubscriber\SuiviConso;
use App\Entity\LeakAlertActivation;
use App\Event\SuiviConso\EndConsentsEvent;
use App\Event\SuiviConso\EndServiceEvent;
use App\Manager\LeakAlertManager;
use App\Repository\App\LeakAlertActivationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SuiviConsoSubscriber implements EventSubscriberInterface
{
private $em;
private $leakAlertActivationRepository;
private $leakAlertManager;
public function __construct(
EntityManagerInterface $em,
LeakAlertActivationRepository $leakAlertActivationRepository,
LeakAlertManager $leakAlertManager
) {
$this->em = $em;
$this->leakAlertActivationRepository = $leakAlertActivationRepository;
$this->leakAlertManager = $leakAlertManager;
}
public static function getSubscribedEvents(): array
{
return [
EndServiceEvent::class => 'onEndService',
EndConsentsEvent::class => 'onEndConsents',
];
}
public function onEndService(EndServiceEvent $event)
{
$leakActivations = $this->leakAlertActivationRepository
->findActiveByUserAndContractInfo($event->getUser(), $event->getPostNumber(), $event->getContractNumber());
if (!$leakActivations) {
return;
}
foreach ($leakActivations as $activation) {
$this->leakAlertManager->deactivate($activation);
}
$this->em->flush();
}
public function onEndConsents(EndConsentsEvent $event)
{
$leakActivations = $this->leakAlertActivationRepository->findActiveByUser($event->getUser());
if (!$leakActivations) {
return;
}
foreach ($leakActivations as $activation) {
$this->leakAlertManager->deactivate($activation);
}
$this->em->flush();
}
}