<?php
namespace App\EventListener;
use App\Entity\AssessmentCycleReminder;
use App\Entity\AssessmentCycleReminderScheduled;
use App\Enum\Message;
use App\Enum\Status;
use App\Enum\UserRelation;
use App\Event\ScheduledCycleStarted;
use App\Service\MailerService;
use App\Service\NotificationService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
class ScheduledCycleStartedListener
{
/** @var EntityManagerInterface */
private $em;
/** @var RouterInterface */
private $router;
/** @var LoggerInterface */
private $logger;
/** @var MailerService */
private $mailer;
/** @var NotificationService */
private $notification;
/**
* ScheduledCycleStartedListener constructor.
*
* @param EntityManagerInterface $em
* @param RouterInterface $router
* @param LoggerInterface $logger
* @param MailerService $mailer
* @param NotificationService $notification
*/
public function __construct(
EntityManagerInterface $em,
RouterInterface $router,
LoggerInterface $logger,
MailerService $mailer,
NotificationService $notification
) {
$this->em = $em;
$this->router = $router;
$this->logger = $logger;
$this->mailer = $mailer;
$this->notification = $notification;
}
public function onScheduledCycleStarted(ScheduledCycleStarted $event)
{
$cycle = $event->getAssessmentCycle();
/**
* Create scheduled reminders
*/
if ($reminders = $this->em->getRepository(AssessmentCycleReminder::class)->findAll()) {
foreach ($reminders as $reminder) {
$duedate = strtotime("+ {$reminder->getDays()} days", $cycle->getTimecreated());
$scheduledReminder = new AssessmentCycleReminderScheduled();
$scheduledReminder->setCycle($cycle);
$scheduledReminder->setAssessmentCycleReminder($reminder);
$scheduledReminder->setDueDate($duedate);
$scheduledReminder->setStatus(Status::INT_NOT_SENT);
$this->em->persist($scheduledReminder);
}
$this->em->flush();
}
/**
* Send notifications/emails
*/
$requestAssessors = $cycle->getAssessors();
$userNotifying = $cycle->getUser();
foreach ($requestAssessors as $requestAssessor) {
$messageId = Message::ASSESSMENT_SCHEDULED_MANAGER;
if ($requestAssessor->getAssessorRelation() === UserRelation::TYPE_SELF) {
$messageId = Message::ASSESSMENT_SCHEDULED_SELF;
}
$link = "/user-assessment/{$userNotifying->getId()}/{$requestAssessor->getToken()}";
$this->notification->notify(
$requestAssessor->getAssessor(),
$messageId,
$link,
[
"fullName" => $userNotifying->getFullname(),
]
);
$linkParams = ['id' => $userNotifying->getId(), 'token' => $requestAssessor->getToken()];
$absoluteLink = $this->router->generate(
'user_assessment_link',
$linkParams,
UrlGeneratorInterface::ABSOLUTE_URL
);
try {
$this->mailer->send(
$requestAssessor->getAssessor()->getEmail(),
$messageId,
[
'recipientfullname' => $requestAssessor->getAssessor()->getFullname(),
'userfullname' => $userNotifying->getFullname(),
'absolutelink' => $absoluteLink,
]
);
} catch (\Exception $e) {
$this->logger->error("Error sending scheduled cycle started notification email", [
"assessorId" => $requestAssessor->getId(),
"message" => $e->getMessage()
]);
}
}
}
}