src/EventSubscriber/JobPostingSubscriber.php line 181

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Application;
  5. use App\Entity\BaseUser;
  6. use App\Entity\JobPosting;
  7. use App\Entity\User;
  8. use App\Entity\ViewJobPosting;
  9. use App\Event\JobPosting\ProvidedJobPostingEvent;
  10. use App\Event\JobPosting\SevenDaysLeftEvent;
  11. use App\Repository\BaseUserRepository;
  12. use App\Repository\UserRepository;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use ExpoSDK\Expo;
  15. use ExpoSDK\ExpoMessage;
  16. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  20. use Symfony\Component\HttpKernel\Event\RequestEvent;
  21. use Symfony\Component\HttpKernel\Event\ViewEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. use Symfony\Component\Mailer\MailerInterface;
  24. use Symfony\Component\Mime\Address;
  25. use Symfony\Component\Security\Core\Security;
  26. final class JobPostingSubscriber implements EventSubscriberInterface
  27. {
  28.     private Security $security;
  29.     private EntityManagerInterface $em;
  30.     private MailerInterface $mailer;
  31.     private BaseUserRepository $baseUserRepository;
  32.     private UserRepository $userRepository;
  33.     public function __construct(Security $securityEntityManagerInterface $emMailerInterface $mailerBaseUserRepository $baseUserRepositoryUserRepository $userRepository)
  34.     {
  35.         $this->security $security;
  36.         $this->em $em;
  37.         $this->mailer $mailer;
  38.         $this->baseUserRepository $baseUserRepository;
  39.         $this->userRepository $userRepository;
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             KernelEvents::REQUEST => ['VIEW'EventPriorities::POST_READ],
  45.             KernelEvents::VIEW => [
  46.                 ['create'EventPriorities::POST_WRITE],
  47.                 ['delete'EventPriorities::POST_WRITE]
  48.             ],
  49.             SevenDaysLeftEvent::class => ['sevenDaysLeft'],
  50.             ProvidedJobPostingEvent::class => ['provided']
  51.         ];
  52.     }
  53.     public function provided(ProvidedJobPostingEvent $event)
  54.     {
  55.         $jobPosting $event->getJobPosting();
  56.         $accepted $jobPosting->getValidateApplication();
  57.         switch ($accepted->getUser()->getNotificationType()) {
  58.             case BaseUser::NOTIFICATION_MAIL:
  59.                 $this->sendMailApplicationAccepted($accepted);
  60.                 break;
  61.             case BaseUser::NOTIFICATION_PUSH:
  62.                 if ($d $accepted->getUser()->getDevicesTokensExpo()) {
  63.                     $this->sendPushApplicationAccepted($d$jobPosting);
  64.                 }
  65.                 break;
  66.             case BaseUser::NOTIFICATION_PUSH_MAIL:
  67.                 $this->sendMailApplicationAccepted($accepted);
  68.                 if ($d $accepted->getUser()->getDevicesTokensExpo()) {
  69.                     $this->sendPushApplicationAccepted($d$jobPosting);
  70.                 }
  71.                 break;
  72.         }
  73.         $jobPosting->setValidateApplication(null);
  74.         $this->em->flush();
  75.     }
  76.     public function delete(ViewEvent $event): void
  77.     {
  78.         $jobPosting $event->getControllerResult();
  79.         $method $event->getRequest()->getMethod();
  80.         if (!$jobPosting instanceof JobPosting || Request::METHOD_DELETE !== $method) {
  81.             return;
  82.         }
  83.         $this->sendMailRemove($jobPosting->getPharmacy()->getEmail(), $jobPosting);
  84.     }
  85.     /**
  86.      * Utilisé pour enregistrer les vues des alertes.
  87.      *
  88.      * @param RequestEvent $event
  89.      */
  90.     public function VIEW(RequestEvent $event)
  91.     {
  92.         $request $event->getRequest();
  93.         $jobPosting $request->get('data');
  94.         $method $request->getMethod();
  95.         /**
  96.          * Si je suis sur un single
  97.          */
  98.         if (
  99.             !$jobPosting instanceof JobPosting
  100.             || Request::METHOD_GET !== $method
  101.             || !$this->security->isGranted('ROLE_USER')
  102.             || $this->security->isGranted('ROLE_PHARMACY')
  103.             || $this->security->isGranted('ROLE_ADMIN')
  104.             || $request->get('_api_normalization_context')['item_operation_name'] !== 'get'
  105.             || !isset($request->get('_api_normalization_context')['item_operation_name'])
  106.         ) {
  107.             return;
  108.         }
  109.         /** @var User $user */
  110.         $user $this->security->getUser();
  111.         /** @var JobPosting $jobPosting */
  112.         $jobPosting $request->get('data');
  113.         if ($viewJobPosting $this->em->getRepository(ViewJobPosting::class)->findOneBy(['userHaveView' => $user'jobPosting' => $jobPosting])) {
  114.             $viewJobPosting->setCount($viewJobPosting->getCount() + 1);
  115.         } else {
  116.             $viewJobPosting = (new ViewJobPosting())
  117.                 ->setUserHaveView($user)
  118.                 ->setJobPosting($jobPosting);
  119.             $this->em->persist($viewJobPosting);
  120.         }
  121.         $this->em->flush();
  122.     }
  123.     public function create(ViewEvent $event): void
  124.     {
  125.         $jobPosting $event->getControllerResult();
  126.         $request $event->getRequest();
  127.         $method $request->getMethod();
  128.         $admins $this->baseUserRepository->findByRole('ROLE_ADMIN');
  129.         if (
  130.             !$jobPosting instanceof JobPosting
  131.             || Request::METHOD_POST !== $method
  132.             || $request->get('_route') !== 'api_job_postings_post_collection'
  133.         ) {
  134.             return;
  135.         }
  136.         if ($this->security->isGranted("ROLE_ADMIN")) {
  137.             $this->sendMailCreateByAdmin($jobPosting->getPharmacy()->getEmail(), $jobPosting);
  138.         } else {
  139.             $this->sendMailCreate($jobPosting);
  140.             $this->sendMailCreateForAdmin(array_map(fn(BaseUser $baseUser) => $baseUser->getEmail(), $admins), $jobPosting);
  141.         }
  142.         $pushs = [];
  143.         $concernedUsers $this->userRepository->findByJobPosting($jobPosting);
  144.         foreach ($concernedUsers as $concernedUser) {
  145.             switch ($concernedUser->getNotificationType()) {
  146.                 case BaseUser::NOTIFICATION_PUSH_MAIL:
  147.                 case BaseUser::NOTIFICATION_PUSH:
  148.                     if ($concernedUser->getDevicesTokensExpo()) {
  149.                         $pushs[] = $concernedUser->getDevicesTokensExpo();
  150.                     }
  151.                     break;
  152.             }
  153.         }
  154.         if (!empty($pushs)) {
  155.             $this->sendPushNewJobPostingForYou(array_merge(...$pushs));
  156.         }
  157.     }
  158.     public function sevenDaysLeft(SevenDaysLeftEvent $event)
  159.     {
  160.         $jobPosting $event->getJobPosting();
  161.         $pharmacy $jobPosting->getPharmacy();
  162.         /**
  163.          * MAIL:Pharmacy
  164.          */
  165.         $email = (new TemplatedEmail())
  166.             ->from(new Address('noreply@ores.com''ORES'))
  167.             ->to($pharmacy->getEmail())
  168.             ->subject('Votre offre arrive à expiration - ORES BFC')
  169.             ->htmlTemplate('mails/job_posting/seven_days_left.html.twig')
  170.             ->context([
  171.                 'jobPosting' => $jobPosting
  172.             ]);
  173.         $this->mailer->send($email);
  174.         $this->sendPushRemindClosure($pharmacy->getDevicesTokensExpo());
  175.     }
  176.     public function sendMailApplicationRefused(Application $application)
  177.     {
  178.         /**
  179.          * MAIL:User
  180.          */
  181.         $email = (new TemplatedEmail())
  182.             ->from(new Address('noreply@ores.com''ORES'))
  183.             ->to($application->getUser()->getEmail())
  184.             ->subject('Candidature refusée - ORES BFC')
  185.             ->htmlTemplate('mails/application/refused.html.twig')
  186.             ->context([
  187.                 'application' => $application
  188.             ]);
  189.         $this->mailer->send($email);
  190.     }
  191.     public function sendPushApplicationRefused($tosJobPosting $jobPosting)
  192.     {
  193.         /**
  194.          * PUSH:BaseUser
  195.          */
  196.         $message = (new ExpoMessage([
  197.             'title' => 'Candidature refusé',
  198.             'body' => "Votre demande de candidature pour l’offre “".$jobPosting->getTitle()."” pour l’officine “".$jobPosting->getPharmacy()->getLegalname()."” a été refusée.",
  199.         ]))
  200.             ->setChannelId('default')
  201.             ->setBadge(0)
  202.             ->playSound();
  203.         $tos array_merge(...$tos);
  204.         if (!empty($tos)) {
  205.             return (new Expo)->send($message)->to($tos)->push();
  206.         }
  207.     }
  208.     public function sendMailApplicationAccepted(Application $application)
  209.     {
  210.         /**
  211.          * MAIL:User
  212.          */
  213.         $email = (new TemplatedEmail())
  214.             ->from(new Address('noreply@ores.com''ORES'))
  215.             ->to($application->getUser()->getEmail())
  216.             ->subject('Candidature retenue - ORES BFC')
  217.             ->htmlTemplate('mails/application/accepted.html.twig')
  218.             ->context([
  219.                 'application' => $application
  220.             ]);
  221.         $this->mailer->send($email);
  222.     }
  223.     public function sendPushApplicationAccepted($tosJobPosting $jobPosting)
  224.     {
  225.         /**
  226.          * PUSH:BaseUser
  227.          */
  228.         $message = (new ExpoMessage([
  229.             'title' => 'Candidature retenu',
  230.             'body' => "Votre demande de candidature pour l’offre “".$jobPosting->getTitle()."” pour l’officine “".$jobPosting->getPharmacy()->getLegalname()."” a été retenue.",
  231.         ]))
  232.             ->setChannelId('default')
  233.             ->setBadge(0)
  234.             ->playSound();
  235.         if (!empty($tos)) {
  236.             return (new Expo)->send($message)->to($tos)->push();
  237.         }
  238.     }
  239.     public function sendMailCreateByAdmin($toJobPosting $jobPosting)
  240.     {
  241.         /**
  242.          * MAIL:Pharmacy
  243.          */
  244.         $email = (new TemplatedEmail())
  245.             ->from(new Address('noreply@ores.com''ORES'))
  246.             ->to($to)
  247.             ->subject('Une nouvelle offre d\'emploi a été crée pour vous - ORES BFC')
  248.             ->htmlTemplate('mails/job_posting/create_by_admin.html.twig')
  249.             ->context([
  250.                 'jobPosting' => $jobPosting,
  251.             ]);
  252.         $this->mailer->send($email);
  253.     }
  254.     public function sendMailCreate(JobPosting $jobPosting)
  255.     {
  256.         /**
  257.          * MAIL:Pharmacy
  258.          */
  259.         $email = (new TemplatedEmail())
  260.             ->from(new Address('noreply@ores.com''ORES'))
  261.             ->to($jobPosting->getPharmacy()->getEmail())
  262.             ->subject('Confirmation de la création de votre offre')
  263.             ->htmlTemplate('mails/job_posting/create.html.twig')
  264.             ->context([
  265.                 'jobPosting' => $jobPosting
  266.             ]);
  267.         $this->mailer->send($email);
  268.     }
  269.     public function sendMailCreateForAdmin(array $adminsJobPosting $jobPosting)
  270.     {
  271.         $createdBy $jobPosting->getCreatedBy();
  272.         /**
  273.          * MAIL:Admin
  274.          */
  275.         $email = (new TemplatedEmail())
  276.             ->from(new Address('noreply@ores.com''ORES'))
  277.             ->to(...$admins)
  278.             ->subject('Nouvelle offre d\'emploi - ORES BFC')
  279.             ->htmlTemplate('mails/job_posting/create_admin.html.twig')
  280.             ->context([
  281.                 'jobPosting' => $jobPosting,
  282.                 'createdBy' => [
  283.                     'lastname' => $createdBy->getLastname(),
  284.                     'firstname' => $createdBy->getFirstname(),
  285.                 ]
  286.             ]);
  287.         $this->mailer->send($email);
  288.     }
  289.     public function sendMailRemove($toJobPosting $jobPosting)
  290.     {
  291.         /**
  292.          * MAIL:Pharmacy
  293.          */
  294.         $email = (new TemplatedEmail())
  295.             ->from(new Address('noreply@ores.com''ORES'))
  296.             ->to($to)
  297.             ->subject('Suppression de votre offre - ORES BFC')
  298.             ->htmlTemplate('mails/job_posting/remove.html.twig')
  299.             ->context([
  300.                 'jobPosting' => $jobPosting,
  301.             ]);
  302.         $this->mailer->send($email);
  303.     }
  304.     public function sendPushRemindClosure($tos)
  305.     {
  306.         /**
  307.          * PUSH:Pharmacy
  308.          */
  309.         $message = (new ExpoMessage([
  310.             'title' => "Pensez à clôturer votre offre sur l’application",
  311.             'body' => "Si votre offre a été pourvue en dehors de l’application, merci de la pourvoir sur l’application pour prévenir les candidats en attente",
  312.         ]))
  313.             ->setChannelId('default')
  314.             ->setBadge(0)
  315.             ->playSound();
  316.         if (!empty($tos)) {
  317.             return (new Expo)->send($message)->to($tos)->push();
  318.         }
  319.     }
  320.     public function sendPushNewJobPostingForYou($tos)
  321.     {
  322.         /**
  323.          * PUSH:User
  324.          */
  325.         $message = (new ExpoMessage([
  326.             'title' => "Nouvelle offre d'emploi !",
  327.             'body' => "Une nouvelle offre dans votre zone est en ligne sur ORES BFC.",
  328.         ]))
  329.             ->setChannelId('default')
  330.             ->setBadge(0)
  331.             ->playSound();
  332.         if (!empty($tos)) {
  333.             return (new Expo)->send($message)->to($tos)->push();
  334.         }
  335.     }
  336. }