src/EventSubscriber/CompanySubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Company;
  4. use App\Entity\SystemEmail;
  5. use App\Enum\Role;
  6. use App\Event\CompanyBirthdayArrivedEvent;
  7. use App\Event\CompanyUpdatedEvent;
  8. use App\Repository\SystemEmailRepository;
  9. use App\Repository\UserRepository;
  10. use App\Service\FrontAPIService;
  11. use App\Service\NotificationService;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class CompanySubscriber implements EventSubscriberInterface
  15. {
  16.     public function __construct(
  17.         private FrontAPIService $frontAPIService,
  18.         private EntityManagerInterface $entityManager,
  19.         private UserRepository $userRepository,
  20.         private SystemEmailRepository $systemEmailRepository,
  21.         private NotificationService $notificationService,
  22.     ){}
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             CompanyUpdatedEvent::NAME => 'onCompanyUpdated',
  27.             CompanyBirthdayArrivedEvent::NAME => 'onCompanyBirthdayArrived',
  28.         ];
  29.     }
  30.     public function onCompanyUpdated(CompanyUpdatedEvent $event)
  31.     {
  32.         $company $event->getCompany();
  33.         if (!$company instanceof Company) {
  34.             return;
  35.         }
  36.         $response $this->frontAPIService->pushCompanyToFront($company);
  37.         if (null !== $response && null === $company->getFrontId()) {
  38.             $company->setFrontId($response['id']);
  39.             $this->entityManager->persist($company);
  40.             $this->entityManager->flush();
  41.         }
  42.     }
  43.     public function onCompanyBirthdayArrived(CompanyBirthdayArrivedEvent $event)
  44.     {
  45.         $company $event->getCompany();
  46.         if (!$company instanceof Company) {
  47.             return;
  48.         }
  49.         $emailSystem null;
  50.         if ($event->getCompany()) {
  51.             $role Role::ROLE_CLIENT_ADMIN->value;
  52.             $clientAdmin $this->userRepository->findClientAdmin($company->getId(), $role);
  53.             
  54.             $user sizeof($clientAdmin) > $clientAdmin [0] : null;
  55.             $emailSystem $this->systemEmailRepository->findOneBy(['code' => SystemEmail::NOTIFICATION_ANNIVERSAIRE_CONTRAT],[],$user );
  56.         }
  57.         if ($emailSystem !== null) {
  58.             foreach ($clientAdmin ?? [] as $client) {
  59.                 $this->notificationService->create($emailSystem$client);
  60.             }
  61.         }
  62.     }
  63. }