src/EventListener/FeedbackRequestedListener.php line 50

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\FeedbackRequestedEvent;
  9. use App\Service\MailerService;
  10. use App\Service\NotificationService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Routing\RouterInterface;
  14. class FeedbackRequestedListener
  15. {
  16.     /** @var EntityManagerInterface */
  17.     private $em;
  18.     /** @var RouterInterface */
  19.     private $router;
  20.     /** @var MailerService */
  21.     private $mailer;
  22.     /** @var NotificationService */
  23.     private $notification;
  24.     /**
  25.      * FeedbackRequestedListener constructor.
  26.      *
  27.      * @param EntityManagerInterface    $em
  28.      * @param MailerService             $mailer
  29.      * @param NotificationService       $notification
  30.      */
  31.     public function __construct(
  32.         EntityManagerInterface $em,
  33.         RouterInterface $router,
  34.         MailerService $mailer,
  35.         NotificationService $notification
  36.     ) {
  37.         $this->em                   $em;
  38.         $this->router               $router;
  39.         $this->mailer               $mailer;
  40.         $this->notification         $notification;
  41.     }
  42.     public function onFeedbackRequested(FeedbackRequestedEvent $event)
  43.     {
  44.         $cycle $event->getAssessmentCycle();
  45.         /**
  46.          * Create scheduled reminders
  47.          */
  48.         if ($reminders $this->em->getRepository(AssessmentCycleReminder::class)->findAll()) {
  49.             foreach ($reminders as $reminder) {
  50.                 $duedate strtotime("+ {$reminder->getDays()} days"$cycle->getTimecreated());
  51.                 $scheduledReminder = new AssessmentCycleReminderScheduled();
  52.                 $scheduledReminder->setCycle($cycle);
  53.                 $scheduledReminder->setAssessmentCycleReminder($reminder);
  54.                 $scheduledReminder->setDueDate($duedate);
  55.                 $scheduledReminder->setStatus(Status::INT_NOT_SENT);
  56.                 $this->em->persist($scheduledReminder);
  57.             }
  58.             $this->em->flush();
  59.         }
  60.         /**
  61.          * Send notifications/emails
  62.          */
  63.         $requestAssessors $cycle->getAssessors();
  64.         $userNotifying $cycle->getUser();
  65.         // Send notifications before emails in case of email exception
  66.         foreach ($requestAssessors as $requestAssessor) {
  67.             if ($requestAssessor->getAssessorRelation() === UserRelation::TYPE_SELF) {
  68.                 continue;
  69.             }
  70.             $link "/user-assessment/{$userNotifying->getId()}/{$requestAssessor->getToken()}";
  71.             $this->notification->notify(
  72.                 $requestAssessor->getAssessor(),
  73.                 Message::ASSESSMENT_FEEDBACK_REQUESTED,
  74.                 $link,
  75.                 [
  76.                     "fullName" => $userNotifying->getFullname(),
  77.                 ]
  78.             );
  79.         }
  80.         foreach ($requestAssessors as $requestAssessor) {
  81.             if ($requestAssessor->getAssessorRelation() === UserRelation::TYPE_SELF) {
  82.                 continue;
  83.             }
  84.             $absoluteLink $this->router->generate(
  85.                 'user_assessment_link',
  86.                 ['id' => $userNotifying->getId(), 'token' => $requestAssessor->getToken()],
  87.                 UrlGeneratorInterface::ABSOLUTE_URL
  88.             );
  89.             $this->mailer->send(
  90.                 $requestAssessor->getAssessor()->getEmail(),
  91.                 Message::ASSESSMENT_FEEDBACK_REQUESTED,
  92.                 [
  93.                     'recipientfullname' => $requestAssessor->getAssessor()->getFullname(),
  94.                     'userfullname'      => $userNotifying->getFullname(),
  95.                     'absolutelink'      => $absoluteLink,
  96.                 ]
  97.             );
  98.         }
  99.     }
  100. }