<?php
namespace Mm\Escmid\StudyGroupElectionBundle\EventSubscriber;
use Mm\Escmid\StudyGroupElectionBundle\Dao\CandidateDao;
use Mm\Escmid\StudyGroupElectionBundle\Dao\EmailLogDao;
use Mm\Escmid\StudyGroupElectionBundle\Entity\EmailLog;
use Mm\Escmid\StudyGroupElectionBundle\Event\MailSendEvent;
use Mm\Escmid\StudyGroupElectionBundle\Service\HistoryService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MailServiceEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly EmailLogDao $emailLogDao,
private readonly HistoryService $historyService,
private readonly CandidateDao $candidateDao
) {
}
public static function getSubscribedEvents(): array
{
return [
MailSendEvent::NAME => 'onMailSend',
];
}
public function onMailSend(MailSendEvent $event): void
{
$addEmailLog = true;
switch ($event->getAction()) {
case 'confirm_candidate':
$entryType = 'election_details_log';
$action = 'confirmation email send';
break;
case 'reject_candidate':
$entryType = 'election_details_log';
$action = 'reject mail send';
break;
case 'cv_biog_reminder':
$entryType = 'election_details_log';
$action = 'CV/biography reminder email send';
break;
case 'approval_candidate':
$entryType = 'election_result_log';
$action = 'approval email candidate send';
break;
case 'approval_committee':
$entryType = 'election_result_log';
$action = 'approval mail SG committee member send';
break;
case 'committee_notification':
$addEmailLog = false;
$action = sprintf('Committee notification sent. Election id: %d', $event->getElectionId());
$entryType = $event->getAction();
break;
case 'member_notification':
$addEmailLog = false;
$action = sprintf('Member notification sent. Election id: %d', $event->getElectionId());
$entryType = $event->getAction();
break;
case 'support_notification':
$addEmailLog = false;
$action = sprintf('Support member mail sent. Election id: %d, supported: %s',
$event->getElectionId(), $event->getSentTo());
$entryType = $event->getAction();
break;
case 'supporter_notification':
$addEmailLog = false;
$action = sprintf('Supporter notification sent. Election id: %d, supporter email: %s',
$event->getElectionId(), $event->getSentTo());
$entryType = $event->getAction();
break;
case 'reject_nomination':
$entryType = 'election_details_log';
$action = 'reject nomination email send';
break;
default:
return;
}
if ($addEmailLog) {
$memberId = $event->getLogEntryId();
$electionId = $event->getElectionId();
$candidate = $this->candidateDao->getCandidateByWebId($memberId, $electionId);
// create e-mail log entry
$emailLog = new EmailLog();
$emailLog->setAction($action);
$emailLog->setActionTime(new \DateTime());
$emailLog->setEntryId($candidate->getId());
$emailLog->setEntryType($entryType);
$this->emailLogDao->save($emailLog);
}
// create history entry
$this->historyService->saveMailSendHistoryEntry($event, $action, $entryType);
}
}