src/EventSubscriber/OSAdminEventSubscriberSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ApplicantReview;
  4. use App\Entity\Applicants;
  5. use App\Entity\Jobs;
  6. use App\Features\Applicant\ApplicantFilter;
  7. use App\Features\Mailer\Exception\IMailerException;
  8. use App\Features\Mailer\IMailer;
  9. use App\Repository\ApplicantsRepository;
  10. use OSAdmin\Event\OSAdminPersistEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Security\Core\Security;
  14. class OSAdminEventSubscriberSubscriber implements EventSubscriberInterface
  15. {
  16.     public function __construct(private readonly ApplicantsRepository $repository,
  17.                                 private IMailer $applicantMailer,
  18.                                 private Security $security,
  19.                                 private UrlGeneratorInterface $generator){}
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             OSAdminPersistEvent::NAME => 'onPersist',
  24.         ];
  25.     }
  26.     public function onPersist(OSAdminPersistEvent $event): void
  27.     {
  28.         $object $event->getEntity();
  29.         if($object instanceof Jobs){
  30.             $job $object;
  31.             $activitySectors$job->getSectors();
  32.             // send mail to all applicant whose sector is in the $activitySectors
  33.             $filter= (new ApplicantFilter())
  34.                 ->setSectors($activitySectors->toArray())
  35.                 ->setEnabled(true);
  36.             $applicants $this->repository->filter($filter)->getQuery()->getResult();
  37.             // filter applicant who when receive new job notification is true
  38.             $applicants array_filter($applicants, static fn(Applicants $applicant) => $applicant->getConfig() && $applicant->getConfig()->getReceiveNewJobAlert());
  39.             if($applicants){
  40.                 $jobLink$this->generator->generate('job-detail', ['uuid' => $job->getUuid()], UrlGeneratorInterface::ABSOLUTE_URL);
  41.                 // send mail to all applicant with message: "Une nouvelle offre d'emploi a été publiée dans votre secteur d'activité. Le lien vers la page de l'offre d'emploi est $jobLink."
  42.                 $text"Une nouvelle offre d'emploi a été publiée dans votre secteur d'activité. Le lien vers la page de l'offre d'emploi est $jobLink.";
  43.                 $html"<p>Une nouvelle offre d'emploi a été publiée dans votre secteur d'activité. Le lien vers la page de l'offre d'emploi est <a href='$jobLink'>$jobLink</a></p>";
  44.                 try {
  45.                     $this->applicantMailer->send($html$text, ...$applicants);
  46.                 } catch (IMailerException $e) {
  47.                 }
  48.             }
  49.         }
  50.         if($object instanceof ApplicantReview){
  51.             $object->setCreatedBy($this->security->getUser());
  52.         }
  53.     }
  54. }