src/EventListener/ScheduledCycleStartedListener.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\AssessmentCycleReminder;
  4. use App\Entity\AssessmentCycleReminderScheduled;
  5. use App\Enum\Message;
  6. use App\Enum\Status;
  7. use App\Enum\UserRelation;
  8. use App\Event\ScheduledCycleStarted;
  9. use App\Service\MailerService;
  10. use App\Service\NotificationService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Routing\RouterInterface;
  15. class ScheduledCycleStartedListener
  16. {
  17.     /** @var EntityManagerInterface */
  18.     private $em;
  19.     /** @var RouterInterface */
  20.     private $router;
  21.     /** @var LoggerInterface */
  22.     private $logger;
  23.     /** @var MailerService */
  24.     private $mailer;
  25.     /** @var NotificationService */
  26.     private $notification;
  27.     /**
  28.      * ScheduledCycleStartedListener constructor.
  29.      *
  30.      * @param EntityManagerInterface  $em
  31.      * @param RouterInterface         $router
  32.      * @param LoggerInterface         $logger
  33.      * @param MailerService           $mailer
  34.      * @param NotificationService     $notification
  35.      */
  36.     public function __construct(
  37.         EntityManagerInterface $em,
  38.         RouterInterface $router,
  39.         LoggerInterface $logger,
  40.         MailerService $mailer,
  41.         NotificationService $notification
  42.     ) {
  43.         $this->em                   $em;
  44.         $this->router               $router;
  45.         $this->logger               $logger;
  46.         $this->mailer               $mailer;
  47.         $this->notification         $notification;
  48.     }
  49.     public function onScheduledCycleStarted(ScheduledCycleStarted $event)
  50.     {
  51.         $cycle $event->getAssessmentCycle();
  52.         /**
  53.          * Create scheduled reminders
  54.          */
  55.         if ($reminders $this->em->getRepository(AssessmentCycleReminder::class)->findAll()) {
  56.             foreach ($reminders as $reminder) {
  57.                 $duedate strtotime("+ {$reminder->getDays()} days"$cycle->getTimecreated());
  58.                 $scheduledReminder = new AssessmentCycleReminderScheduled();
  59.                 $scheduledReminder->setCycle($cycle);
  60.                 $scheduledReminder->setAssessmentCycleReminder($reminder);
  61.                 $scheduledReminder->setDueDate($duedate);
  62.                 $scheduledReminder->setStatus(Status::INT_NOT_SENT);
  63.                 $this->em->persist($scheduledReminder);
  64.             }
  65.             $this->em->flush();
  66.         }
  67.         /**
  68.          * Send notifications/emails
  69.          */
  70.         $requestAssessors $cycle->getAssessors();
  71.         $userNotifying $cycle->getUser();
  72.         foreach ($requestAssessors as $requestAssessor) {
  73.             $messageId Message::ASSESSMENT_SCHEDULED_MANAGER;
  74.             if ($requestAssessor->getAssessorRelation() === UserRelation::TYPE_SELF) {
  75.                 $messageId Message::ASSESSMENT_SCHEDULED_SELF;
  76.             }
  77.             $link "/user-assessment/{$userNotifying->getId()}/{$requestAssessor->getToken()}";
  78.             $this->notification->notify(
  79.                 $requestAssessor->getAssessor(),
  80.                 $messageId,
  81.                 $link,
  82.                 [
  83.                     "fullName" => $userNotifying->getFullname(),
  84.                 ]
  85.             );
  86.             $linkParams = ['id' => $userNotifying->getId(), 'token' => $requestAssessor->getToken()];
  87.             $absoluteLink $this->router->generate(
  88.                 'user_assessment_link',
  89.                 $linkParams,
  90.                 UrlGeneratorInterface::ABSOLUTE_URL
  91.             );
  92.             try {
  93.                 $this->mailer->send(
  94.                     $requestAssessor->getAssessor()->getEmail(),
  95.                     $messageId,
  96.                     [
  97.                         'recipientfullname' => $requestAssessor->getAssessor()->getFullname(),
  98.                         'userfullname' => $userNotifying->getFullname(),
  99.                         'absolutelink' => $absoluteLink,
  100.                     ]
  101.                 );
  102.             } catch (\Exception $e) {
  103.                 $this->logger->error("Error sending scheduled cycle started notification email", [
  104.                     "assessorId" => $requestAssessor->getId(),
  105.                     "message" => $e->getMessage()
  106.                 ]);
  107.             }
  108.         }
  109.     }
  110. }