src/EventSubscriber/ApplicationSubscriber.php line 128

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\Pharmacy;
  7. use App\Event\Application\ApplicationMailRequest;
  8. use App\Event\JobPosting\ApplicationRefused;
  9. use App\Kernel;
  10. use App\Repository\ApplicationRepository;
  11. use App\Repository\BaseUserRepository;
  12. use App\Services\BaseUserService;
  13. use ExpoSDK\Expo;
  14. use ExpoSDK\ExpoMessage;
  15. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Event\ViewEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\Mime\Address;
  21. use Symfony\Component\Mailer\MailerInterface;
  22. use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
  23. final class ApplicationSubscriber implements EventSubscriberInterface
  24. {
  25.     private MailerInterface $mailer;
  26.     private UploaderHelper $helper;
  27.     private Kernel $kernel;
  28.     private BaseUserRepository $baseUserRepository;
  29.     private BaseUserService $baseUserService;
  30.     private ApplicationRepository $applicationRepository;
  31.     public function __construct(MailerInterface $mailerUploaderHelper $helperKernel $kernelBaseUserRepository $baseUserRepositoryBaseUserService $baseUserServiceApplicationRepository $applicationRepository)
  32.     {
  33.         $this->mailer $mailer;
  34.         $this->helper $helper;
  35.         $this->kernel $kernel;
  36.         $this->baseUserRepository $baseUserRepository;
  37.         $this->baseUserService $baseUserService;
  38.         $this->applicationRepository $applicationRepository;
  39.     }
  40.     public static function getSubscribedEvents()
  41.     {
  42.         return [
  43.             KernelEvents::VIEW => ['create'EventPriorities::POST_WRITE],
  44.             ApplicationMailRequest::class => ['sendMailInformation'],
  45.             ApplicationRefused::class => ['applicationRefused']
  46.         ];
  47.     }
  48.     public function create(ViewEvent $event): void
  49.     {
  50.         $application $event->getControllerResult();
  51.         $request $event->getRequest();
  52.         $method $request->getMethod();
  53.         if (
  54.             !$application instanceof Application
  55.             || Request::METHOD_POST !== $method
  56.             || $request->get('_route') !== 'api_applications_post_collection'
  57.         ) {
  58.             return;
  59.         }
  60.         if ($devices $this->baseUserService->getDevicesTokenFromRole('ROLE_ADMIN')) {
  61.             $this->sendPushCreate($devices);
  62.         }
  63.         /**
  64.          * MAIL:User
  65.          */
  66.         $email = (new TemplatedEmail())
  67.             ->from(new Address('noreply@ores.com''ORES'))
  68.             ->to($application->getUser()->getEmail())
  69.             ->subject('Merci pour votre candidature - ORES BFC')
  70.             ->htmlTemplate('mails/application/create.html.twig')
  71.             ->context([
  72.                 'application' => $application
  73.             ]);
  74.         $this->mailer->send($email);
  75.         /**
  76.          * Information d'une candidature pour le propriétaire d'officine
  77.          */
  78.         $pharmacy $application->getJobPosting()->getPharmacy();
  79.         $devices $pharmacy->getDevicesTokensExpo();
  80.         switch ($pharmacy->getNotificationType()) {
  81.             case BaseUser::NOTIFICATION_MAIL:
  82.                 $this->sendMailCreatePharmacy($pharmacy$application);
  83.                 break;
  84.             case BaseUser::NOTIFICATION_PUSH:
  85.                 if ($devices$this->sendPushCreate($devices);
  86.                 break;
  87.             case BaseUser::NOTIFICATION_PUSH_MAIL:
  88.                 $this->sendMailCreatePharmacy($pharmacy$application);
  89.                 if ($devices$this->sendPushCreate($devices);
  90.                 break;
  91.         }
  92.     }
  93.     public function sendMailCreatePharmacy(Pharmacy $pharmacyApplication $application)
  94.     {
  95.         /**
  96.          * MAIL:Pharmacy
  97.          */
  98.         $email = (new TemplatedEmail())
  99.             ->from(new Address('noreply@ores.com''ORES'))
  100.             ->to($pharmacy->getEmail())
  101.             ->subject('Nouvelle candidature sur votre offre - ORES BFC')
  102.             ->htmlTemplate('mails/application/create_pharmacy.html.twig')
  103.             ->context([
  104.                 'pharmacy' => $pharmacy,
  105.                 'application' => $application
  106.             ])
  107.             ->attachFromPath($this->kernel->getProjectDir().'/public'.$this->helper->asset($application"cvUploaded"));
  108.         if ($application->getMotivationLetter()) {
  109.             $email->attachFromPath($this->kernel->getProjectDir().'/public'.$this->helper->asset($application"motivationLetterUploaded"));
  110.         }
  111.         $this->mailer->send($email);
  112.     }
  113.     public function sendMailInformation(ApplicationMailRequest $request)
  114.     {
  115.         $baseUser $request->getUser();
  116.         $application $request->getApplication();
  117.         $userFromDB $application->getUser();
  118.         $user = [
  119.             'lastname' => $userFromDB->getLastname(),
  120.             'firstname' => $userFromDB->getFirstname(),
  121.             'phone' => $userFromDB->getPhone(),
  122.             'email' => $userFromDB->getEmail(),
  123.         ];
  124.         /**
  125.          * MAIL:Pharmacy
  126.          */
  127.         $email = (new TemplatedEmail())
  128.             ->from(new Address('noreply@ores.com''ORES'))
  129.             ->to($baseUser->getEmail())
  130.             ->subject('Informations sur une candidature - ORES BFC')
  131.             ->htmlTemplate('mails/application/information_request.html.twig')
  132.             ->context([
  133.                 'baseUser' => $baseUser,
  134.                 'application' => $application,
  135.                 'user' => $user,
  136.             ])
  137.             ->attachFromPath($this->kernel->getProjectDir().'/public'.$this->helper->asset($application"cvUploaded"));
  138.         if ($application->getMotivationLetter()) {
  139.             $email->attachFromPath($this->kernel->getProjectDir().'/public'.$this->helper->asset($application"motivationLetterUploaded"));
  140.         }
  141.         $this->mailer->send($email);
  142.     }
  143.     public function sendPushCreate(?array $devices)
  144.     {
  145.         /**
  146.          * PUSH:Pharmacy PUSH:Admin
  147.          */
  148.         $message = (new ExpoMessage([
  149.             'title' => 'Nouvelle candidature',
  150.             'body' => 'Une nouvelle demande de candidature.',
  151.         ]))
  152.             ->setChannelId('default')
  153.             ->setBadge(0)
  154.             ->playSound();
  155.         if (!empty($devices)) {
  156.             return (new Expo)->send($message)->to($devices)->push();
  157.         }
  158.     }
  159.     public function sendMailApplicationRefused(Application $application)
  160.     {
  161.         /**
  162.          * MAIL:User
  163.          */
  164.         $email = (new TemplatedEmail())
  165.             ->from(new Address('noreply@ores.com''ORES'))
  166.             ->to($application->getUser()->getEmail())
  167.             ->subject('Candidature refusée - ORES BFC')
  168.             ->htmlTemplate('mails/application/refused.html.twig')
  169.             ->context([
  170.                 'application' => $application
  171.             ]);
  172.         $this->mailer->send($email);
  173.         return $application;
  174.     }
  175.     public function sendPushApplicationRefused(?array $toApplication $application)
  176.     {
  177.         /**
  178.          * PUSH:BaseUser
  179.          */
  180.         $message = (new ExpoMessage([
  181.             'title' => 'Candidature refusé',
  182.             'body' => "Votre demande de candidature pour l’offre “".$application->getJobPosting()->getTitle()."” pour l’officine “".$application->getJobPosting()->getPharmacy()->getLegalname()."” a été refusée.",
  183.         ]))
  184.             ->setChannelId('default')
  185.             ->setBadge(0)
  186.             ->playSound();
  187.         if (!empty($to)) {
  188.             return (new Expo)->send($message)->to($to)->push();
  189.         }
  190.     }
  191.     public function applicationRefused(ApplicationRefused $application)
  192.     {
  193.         $devices $application->getUser()->getDevicesTokensExpo();
  194.         $applicationRepo $this->applicationRepository->findOneBy(['id' => $application->getId()]);
  195.         switch ($application->getUser()->getNotificationType()) {
  196.             case BaseUser::NOTIFICATION_MAIL:
  197.                 $this->sendMailApplicationRefused($applicationRepo);
  198.                 break;
  199.             case BaseUser::NOTIFICATION_PUSH:
  200.                 if ($devices$this->sendPushApplicationRefused($devices$applicationRepo);
  201.                 break;
  202.             case BaseUser::NOTIFICATION_PUSH_MAIL:
  203.                 $this->sendMailApplicationRefused($applicationRepo);
  204.                 if ($devices$this->sendPushApplicationRefused($devices$applicationRepo);
  205.                 break;
  206.         }
  207.         return $application;
  208.     }
  209. }