src/EventSubscriber/EstablishmentSubscriber.php line 55
<?phpnamespace App\EventSubscriber;use App\Entity\Establishment;use App\Event\Establishment\EstablishmentCreatedEvent;use App\Event\Establishment\EstablishmentDeletedEvent;use App\Event\Establishment\EstablishmentUpdatedEvent;use App\Kimia;use App\Repository\EstablishmentRepository;use App\Service\Establishment\EstablishmentManager;use App\Service\NotificationService;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\Event\RequestEvent;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;class EstablishmentSubscriber implements EventSubscriberInterface{public function __construct(private NotificationService $notificationService,private UrlGeneratorInterface $urlGenerator,private EstablishmentManager $manager) {}public static function getSubscribedEvents(): array{return [EstablishmentCreatedEvent::class => 'onCreated',EstablishmentUpdatedEvent::class => 'onUpdated',EstablishmentDeletedEvent::class => 'onDeleted',RequestEvent::class => 'onKernelRequest',];}public function onCreated(EstablishmentCreatedEvent $event): void{$establishment = $event->getEstablishment();$establishment->setType(Kimia::TYPE_BILLING);$this->manager->getRepository()->save($establishment, true);$this->notificationService->notifyUser($establishment->getUser(),'Établissement ajouté',sprintf("Vous venez d'ajouter l'établissement <strong>%s</strong>",$establishment->getName()),$establishment);}public function onUpdated(EstablishmentUpdatedEvent $event): void{$establishment = $event->getEstablishment();$this->notificationService->notifyUser($establishment->getUser(),'Établissement modifié',sprintf("Vous venez de modifier l'établissement <strong>%s</strong>",$establishment->getName()),$establishment);}public function onDeleted(EstablishmentDeletedEvent $event): void{$establishment = $event->getEstablishment();$this->notificationService->notifyUser($establishment->getUser(),'Établissement supprimé',sprintf("Vous venez de supprimer l'établissement <strong>%s</strong>",$establishment->getName()),$establishment);}public function onKernelRequest(RequestEvent $event): void{//$establishment = $this->manager->getConnected();//dd($this->manager);//TODO: a revoir s'il a le droit d'acceder ou pas une route en fonction de la souscription/*if ($establishment?->checkSubscription()) {$request = $event->getRequest();if (!$this->isAutorisedRoute($request)) {$response = new RedirectResponse($this->urlGenerator->generate('app_establishment_expiry'), 302);$event->setResponse($response);}//}//else //pas d'abonnement pour cet établissement*/}private function isAutorisedRoute(Request $request): bool{$currentRouteName = $request->attributes->get('_route');return in_array($currentRouteName, ['site_landing', 'app_self', 'app_establishment_new', 'app_establishment_expiry', 'app_invitation_show']);}}