Passed
Push — master ( 5101bc...168c03 )
by Marcel
06:42
created

getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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());
0 ignored issues
show
Bug introduced by
It seems like $event->getAbsence()->getEmail() can also be of type null; however, parameter $username of App\Entity\User::setUsername() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
                ->setUsername(/** @scrutinizer ignore-type */ $event->getAbsence()->getEmail());
Loading history...
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
}