|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Notification\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\User; |
|
6
|
|
|
use App\Event\StudentAbsenceApprovalChangedEvent; |
|
7
|
|
|
use App\Notification\NotificationService; |
|
8
|
|
|
use App\Notification\StudentAbsenceNotification; |
|
9
|
|
|
use App\StudentAbsence\InvolvedUsersResolver; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
11
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
12
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
13
|
|
|
|
|
14
|
|
|
class StudentAbsenceApprovalChangedEventSubscriber implements EventSubscriberInterface { |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(private readonly InvolvedUsersResolver $involvedUsersResolver, private readonly NotificationService $notificationService, |
|
17
|
|
|
private readonly UrlGeneratorInterface $urlGenerator, private readonly TranslatorInterface $translator) { |
|
18
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function onStudentAbsenceApprovalChanged(StudentAbsenceApprovalChangedEvent $event): void { |
|
22
|
|
|
if($event->getAbsence()->getApprovedBy() === null) { |
|
23
|
|
|
return; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$recipients = [ ]; |
|
27
|
|
|
foreach($this->involvedUsersResolver->resolveUsers($event->getAbsence()) as $user) { |
|
28
|
|
|
if($event->getAbsence()->getApprovedBy()->getId() !== $user->getId()) { |
|
29
|
|
|
$recipients[] = $user; |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$emails = array_map(fn(User $user) => $user->getEmail(), $recipients); |
|
34
|
|
|
if(!in_array($event->getAbsence()->getEmail(), $emails)) { |
|
35
|
|
|
$recipients[] = (new User()) |
|
36
|
|
|
->setEmail($event->getAbsence()->getEmail()) |
|
37
|
|
|
->setUsername($event->getAbsence()->getEmail()); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
foreach($recipients as $recipient) { |
|
41
|
|
|
$notification = new StudentAbsenceNotification( |
|
42
|
|
|
$recipient, |
|
43
|
|
|
$this->translator->trans('student_absence.approval.title', [], 'email'), |
|
44
|
|
|
$this->translator->trans('student_absence.approval.content', ['%type%' => $event->getAbsence()->getType()->getName()], 'email'), |
|
45
|
|
|
$this->urlGenerator->generate('show_student_absence', [ 'uuid' => $event->getAbsence()->getUuid()->toString()], UrlGeneratorInterface::ABSOLUTE_URL), |
|
46
|
|
|
$this->translator->trans('student_absence.link', [], 'email'), |
|
47
|
|
|
$event->getAbsence() |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$this->notificationService->notify($notification); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function getSubscribedEvents(): array { |
|
55
|
|
|
return [ |
|
56
|
|
|
StudentAbsenceApprovalChangedEvent::class => 'onStudentAbsenceApprovalChanged' |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
} |