src/Mm/Escmid/StudyGroupElectionBundle/EventSubscriber/MailServiceEventSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace Mm\Escmid\StudyGroupElectionBundle\EventSubscriber;
  3. use Mm\Escmid\StudyGroupElectionBundle\Dao\CandidateDao;
  4. use Mm\Escmid\StudyGroupElectionBundle\Dao\EmailLogDao;
  5. use Mm\Escmid\StudyGroupElectionBundle\Entity\EmailLog;
  6. use Mm\Escmid\StudyGroupElectionBundle\Event\MailSendEvent;
  7. use Mm\Escmid\StudyGroupElectionBundle\Service\HistoryService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class MailServiceEventSubscriber implements EventSubscriberInterface
  10. {
  11. public function __construct(
  12. private readonly EmailLogDao $emailLogDao,
  13. private readonly HistoryService $historyService,
  14. private readonly CandidateDao $candidateDao
  15. ) {
  16. }
  17. public static function getSubscribedEvents(): array
  18. {
  19. return [
  20. MailSendEvent::NAME => 'onMailSend',
  21. ];
  22. }
  23. public function onMailSend(MailSendEvent $event): void
  24. {
  25. $addEmailLog = true;
  26. switch ($event->getAction()) {
  27. case 'confirm_candidate':
  28. $entryType = 'election_details_log';
  29. $action = 'confirmation email send';
  30. break;
  31. case 'reject_candidate':
  32. $entryType = 'election_details_log';
  33. $action = 'reject mail send';
  34. break;
  35. case 'cv_biog_reminder':
  36. $entryType = 'election_details_log';
  37. $action = 'CV/biography reminder email send';
  38. break;
  39. case 'approval_candidate':
  40. $entryType = 'election_result_log';
  41. $action = 'approval email candidate send';
  42. break;
  43. case 'approval_committee':
  44. $entryType = 'election_result_log';
  45. $action = 'approval mail SG committee member send';
  46. break;
  47. case 'committee_notification':
  48. $addEmailLog = false;
  49. $action = sprintf('Committee notification sent. Election id: %d', $event->getElectionId());
  50. $entryType = $event->getAction();
  51. break;
  52. case 'member_notification':
  53. $addEmailLog = false;
  54. $action = sprintf('Member notification sent. Election id: %d', $event->getElectionId());
  55. $entryType = $event->getAction();
  56. break;
  57. case 'support_notification':
  58. $addEmailLog = false;
  59. $action = sprintf('Support member mail sent. Election id: %d, supported: %s',
  60. $event->getElectionId(), $event->getSentTo());
  61. $entryType = $event->getAction();
  62. break;
  63. case 'supporter_notification':
  64. $addEmailLog = false;
  65. $action = sprintf('Supporter notification sent. Election id: %d, supporter email: %s',
  66. $event->getElectionId(), $event->getSentTo());
  67. $entryType = $event->getAction();
  68. break;
  69. case 'reject_nomination':
  70. $entryType = 'election_details_log';
  71. $action = 'reject nomination email send';
  72. break;
  73. default:
  74. return;
  75. }
  76. if ($addEmailLog) {
  77. $memberId = $event->getLogEntryId();
  78. $electionId = $event->getElectionId();
  79. $candidate = $this->candidateDao->getCandidateByWebId($memberId, $electionId);
  80. // create e-mail log entry
  81. $emailLog = new EmailLog();
  82. $emailLog->setAction($action);
  83. $emailLog->setActionTime(new \DateTime());
  84. $emailLog->setEntryId($candidate->getId());
  85. $emailLog->setEntryType($entryType);
  86. $this->emailLogDao->save($emailLog);
  87. }
  88. // create history entry
  89. $this->historyService->saveMailSendHistoryEntry($event, $action, $entryType);
  90. }
  91. }