Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SendNotificationTicketEventSubscriber.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Lc\SovBundle\EventSubscriber\Ticket;
  3. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  4. use Lc\SovBundle\Event\Ticket\TicketEvent;
  5. use Lc\SovBundle\Model\Ticket\TicketInterface;
  6. use Lc\SovBundle\Model\Ticket\TicketMessageInterface;
  7. use Lc\SovBundle\Notification\MailMailjetNotification;
  8. use Lc\SovBundle\Repository\User\UserStore;
  9. use Lc\SovBundle\Solver\Ticket\TicketSolver;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. class SendNotificationTicketEventSubscriber implements EventSubscriberInterface
  13. {
  14. protected MailMailjetNotification $mailMailjetNotification;
  15. protected UserStore $userStore;
  16. protected AuthorizationCheckerInterface $authorizationChecker;
  17. protected TicketSolver $ticketSolver;
  18. public function __construct(
  19. MailMailjetNotification $mailMailjetNotification,
  20. UserStore $userStore,
  21. AuthorizationCheckerInterface $authorizationChecker,
  22. TicketSolver $ticketSolver
  23. ) {
  24. $this->mailMailjetNotification = $mailMailjetNotification;
  25. $this->userStore = $userStore;
  26. $this->authorizationChecker = $authorizationChecker;
  27. $this->ticketSolver = $ticketSolver;
  28. }
  29. public static function getSubscribedEvents()
  30. {
  31. return [
  32. TicketEvent::NEW_MESSAGE_EVENT => ['sendNotificationAfterNewMessage'],
  33. TicketEvent::NEW_TICKET_EVENT => ['sendNotificationAfterNewTicket'],
  34. ];
  35. }
  36. public function sendNotificationAfterNewTicket(TicketEvent $ticketEvent)
  37. {
  38. $ticket = $ticketEvent->getTicket();
  39. $lastMessage = $this->ticketSolver->getLastMessage($ticket);
  40. if ($ticket->getUser()) {
  41. $firstname = $ticket->getUser()->getFirstname();
  42. $email = $ticket->getUser()->getEmail();
  43. } else {
  44. $firstname = $ticket->getVisitorFirstname();
  45. $email = $ticket->getVisitorEmail();
  46. }
  47. if ($lastMessage->getAnswerByAdmin()) {
  48. // envoi email au client
  49. $this->mailMailjetNotification->send(
  50. [
  51. MailMailjetNotification::SUBJECT => 'Vous avez reçu un nouveau message',
  52. MailMailjetNotification::TO_EMAIL => $email,
  53. MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-new-by-admin',
  54. MailMailjetNotification::CONTENT_DATA => [
  55. 'firstname' => $firstname,
  56. 'ticket' => $ticket,
  57. ],
  58. ]
  59. );
  60. } else {
  61. $this->mailMailjetNotification->send(
  62. [
  63. MailMailjetNotification::SUBJECT => 'Nouvelle demande',
  64. MailMailjetNotification::TO_EMAIL => $email,
  65. MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-new',
  66. MailMailjetNotification::CONTENT_DATA => [
  67. 'firstname' => $firstname,
  68. 'ticket' => $ticket,
  69. ],
  70. ]
  71. );
  72. }
  73. $this->notifyUser($ticket);
  74. }
  75. public function sendNotificationAfterNewMessage(TicketEvent $ticketEvent)
  76. {
  77. $ticket = $ticketEvent->getTicket();
  78. $lastMessage = $this->ticketSolver->getLastMessage($ticket);
  79. if ($ticket->getUser()) {
  80. $firstname = $ticket->getUser()->getFirstname();
  81. $email = $ticket->getUser()->getEmail();
  82. } else {
  83. $firstname = $ticket->getVisitorFirstname();
  84. $email = $ticket->getVisitorEmail();
  85. }
  86. if ($lastMessage->getAnswerByAdmin()) {
  87. // envoi email au client
  88. $this->mailMailjetNotification->send(
  89. [
  90. MailMailjetNotification::SUBJECT => 'Réponse à votre demande',
  91. MailMailjetNotification::TO_EMAIL => $email,
  92. MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-response',
  93. MailMailjetNotification::CONTENT_DATA => [
  94. 'firstname' => $firstname,
  95. 'ticket' => $ticket,
  96. ],
  97. ]
  98. );
  99. }
  100. $this->notifyUser($ticket);
  101. }
  102. protected function notifyUser(TicketInterface $ticket): void
  103. {
  104. // notifyAdmin
  105. $usersToNotify = $this->userStore->getByTicketTypesNotification($ticket->getType());
  106. foreach ($usersToNotify as $userToNotify) {
  107. if ($this->authorizationChecker->isGranted('ROLE_ADMIN', $userToNotify)) {
  108. $this->mailMailjetNotification->send(
  109. [
  110. MailMailjetNotification::SUBJECT => 'Nouveau ticket sur Place du Local',
  111. MailMailjetNotification::TO_EMAIL => $userToNotify->getEmail(),
  112. MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-notification',
  113. MailMailjetNotification::CONTENT_DATA => [
  114. 'firstname' => $userToNotify->getFirstname(),
  115. 'ticket' => $ticket,
  116. 'ticketMessage' => $ticket->getTicketMessages()[0],
  117. ],
  118. ]
  119. );
  120. }
  121. }
  122. }
  123. }