SchulIT /
icc
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Notification\EventSubscriber; |
||
| 4 | |||
| 5 | use App\Entity\BookComment; |
||
| 6 | use App\Entity\Teacher; |
||
| 7 | use App\Event\BookCommentCreatedEvent; |
||
| 8 | use App\Event\BookCommentUpdatedEvent; |
||
| 9 | use App\Notification\Notification; |
||
| 10 | use App\Notification\NotificationService; |
||
| 11 | use App\Repository\UserRepositoryInterface; |
||
| 12 | use App\Section\SectionResolverInterface; |
||
| 13 | use App\Utils\ArrayUtils; |
||
| 14 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
| 15 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
| 16 | use Symfony\Contracts\Translation\TranslatorInterface; |
||
| 17 | |||
| 18 | class BookCommentEventSubscriber implements EventSubscriberInterface { |
||
| 19 | |||
| 20 | public function __construct(private readonly TranslatorInterface $translator, |
||
| 21 | private readonly UrlGeneratorInterface $urlGenerator, |
||
| 22 | private readonly NotificationService $notificationService, |
||
| 23 | private readonly SectionResolverInterface $sectionResolver, |
||
| 24 | private readonly UserRepositoryInterface $userRepository) { } |
||
| 25 | |||
| 26 | public function onBookCommentCreatedOrUpdate(BookCommentCreatedEvent|BookCommentUpdatedEvent $event): void { |
||
| 27 | $teachers = $this->resolveTeachers($event->getComment()); |
||
| 28 | $users = $this->userRepository->findAllTeachers($teachers); |
||
| 29 | |||
| 30 | $subjectKey = $event instanceof BookCommentCreatedEvent ? 'book_comment.create.title' : 'book_comment.update.title'; |
||
| 31 | $contentKey = $event instanceof BookCommentCreatedEvent ? 'book_comment.create.content' : 'book_comment.update.content'; |
||
| 32 | |||
| 33 | foreach($users as $recipient) { |
||
| 34 | $notification = new Notification( |
||
| 35 | $recipient, |
||
| 36 | $this->translator->trans($subjectKey, [], 'email'), |
||
| 37 | $this->translator->trans($contentKey, [], 'email'), |
||
| 38 | $this->urlGenerator->generate('show_book_comment', [ 'uuid' => $event->getComment()->getUuid() ]), |
||
| 39 | $this->translator->trans('book_comment.link', [], 'email'), |
||
| 40 | true |
||
| 41 | ); |
||
| 42 | |||
| 43 | $this->notificationService->notify($notification); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | private function resolveTeachers(BookComment $comment): array { |
||
| 48 | $teachers = [ ]; |
||
| 49 | |||
| 50 | $section = $this->sectionResolver->getSectionForDate($comment->getDate()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 51 | |||
| 52 | if($section === null) { |
||
| 53 | return $teachers; |
||
| 54 | } |
||
| 55 | |||
| 56 | foreach($comment->getStudents() as $student) { |
||
| 57 | $grade = $student->getGrade($section); |
||
| 58 | |||
| 59 | if($grade === null) { |
||
| 60 | continue; |
||
| 61 | } |
||
| 62 | |||
| 63 | foreach($grade->getTeachers() as $gradeTeacher) { |
||
| 64 | if($gradeTeacher->getTeacher()->getId() !== $comment->getTeacher()->getId()) { |
||
| 65 | $teachers[] = $gradeTeacher->getTeacher(); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | // Create unique list of teachers |
||
| 71 | return array_values( |
||
| 72 | ArrayUtils::createArrayWithKeys( |
||
| 73 | $teachers, |
||
| 74 | fn(Teacher $teacher) => $teacher->getId() |
||
| 75 | ) |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | public static function getSubscribedEvents(): array { |
||
| 80 | return [ |
||
| 81 | BookCommentCreatedEvent::class => 'onBookCommentCreatedOrUpdate', |
||
| 82 | BookCommentUpdatedEvent::class => 'onBookCommentCreatedOrUpdate' |
||
| 83 | ]; |
||
| 84 | } |
||
| 85 | } |