<?php
// src/EventListener/HolidayEventListener.php
namespace App\EventListener;
use App\Event\HolidayEvent;
use App\Services\GeneralService;
use App\Services\NotificationService;
class HolidayEventListener
{
private $notificationProvider;
private $generalProvider;
public function __construct(NotificationService $notificationProvider, GeneralService $generalProvider)
{
$this->notificationProvider = $notificationProvider;
$this->generalProvider = $generalProvider;
}
public function onRequestCreated(HolidayEvent $event)
{
$data = $event->getData();
$subject = "Holiday Request";
$this->sendNotification($data['recipients'], $data['msg'], $data['id']);
$this->sendEmail($data['recipients'], $data['msg'], $subject);
}
public function onRequestAccepted(HolidayEvent $event)
{
$data = $event->getData();
$subject = "Holiday Request";
$this->sendNotification($data['recipients'], $data['msg'], $data['id']);
$this->sendEmail($data['recipients'], $data['msg'], $subject);
}
public function onRequestRejected(HolidayEvent $event)
{
$data = $event->getData();
$subject = "Holiday Request";
$this->sendNotification($data['recipients'], $data['msg'], $data['id']);
$this->sendEmail($data['recipients'], $data['msg'], $subject);
}
public function onAmendmentCreated(HolidayEvent $event)
{
$data = $event->getData();
$subject = "Holiday Amendment";
$this->sendNotification($data['recipients'], $data['msg'], $data['id']);
$this->sendEmail($data['recipients'], $data['msg'], $subject);
}
protected function sendNotification($recipients, $msg, $userId)
{
foreach ($recipients as $recipient) {
$this->notificationProvider->createNotification(
$recipient,
$msg,
'holidays',
null,
$userId
);
}
}
protected function sendEmail($recipients, $msg, $subject)
{
foreach ($recipients as $recipient) {
$name = $this->generalProvider->getUserName($recipient);
$params = ['subject' => $subject, 'title' => '', 'greeting' => "Hi $name,", 'body' => $msg, 'header' => '', 'subtitle' => ''];
$this->generalProvider->sendEmail($recipient->getEmail(), '/en/mails/osgo-generic', $params);
}
}
}