<?php
namespace App\EventSubscriber\TimeOff;
use App\Entity\TimeOff;
use App\Entity\Workflow\WorkflowTransitionLog;
use App\Mail\Mailer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
class StatusChangeNotifier implements EventSubscriberInterface
{
/**
* @var Mailer
*/
private $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
'workflow.timeoff_status.entered' => 'onEntered',
];
}
public function onEntered(Event $event): void
{
if ($event->getTransition() && in_array($event->getTransition()->getName(), ['approve', 'reject', 'cancel'])) {
/** @var TimeOff $timeOff */
$timeOff = $event->getSubject();
/** @var WorkflowTransitionLog $lastTransition */
$lastTransition = $timeOff->getWorkflowTransitionLogs()->last();
$actor = $lastTransition->getUser();
$user = $timeOff->getRequestedBy();
if ($user === $actor) {
// Don't alert ourselves that we canceled our time off request
return;
}
$status = $timeOff->getStatus();
$this->mailer->send('time-off/status-change', compact('user', 'actor', 'timeOff', 'status'), [$timeOff->getRequestedBy()->getEmail()], null,
sprintf('Time off request %s - %s %s',
null !== $timeOff->getDateFrom() ? $timeOff->getDateFrom()->format('M j, Y') : null,
null !== $timeOff->getDateTo() ? $timeOff->getDateTo()->format('M j, Y') : null,
$status)
);
}
}
}