src/EventSubscriber/EnrollmentSubscriber.php line 40

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Notification;
  4. use App\Event\EnrollmentAcceptedEvent;
  5. use App\Repository\NotificationRepository;
  6. use App\Services\PaymentRecordService;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. class EnrollmentSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private UrlGeneratorInterface  $urlGenerator,
  13.         private NotificationRepository $notificationRepository,
  14.         private PaymentRecordService $paymentService,
  15.     )
  16.     {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             EnrollmentAcceptedEvent::NAME =>
  22.                 [
  23.                     ['createPayment'1],
  24.                     ['createNotification'0],
  25.                 ],
  26.         ];
  27.     }
  28.     public function createPayment(EnrollmentAcceptedEvent $event): void
  29.     {
  30.         $this->paymentService->new($event->getEnrollment());
  31.     }
  32.     public function createNotification(EnrollmentAcceptedEvent $event): void
  33.     {
  34.         // User to notify
  35.         $user $event->getEnrollment()->getGuardian()->getUser();
  36.         // Payment URL
  37.         $url $this->urlGenerator->generate('app_payment_show',
  38.             ['id' => $event->getEnrollment()->getPaymentRecords()->last()->getId()],
  39.             UrlGeneratorInterface::ABSOLUTE_URL);
  40.         // Create notification
  41.         $notification = new Notification(
  42.             $user,
  43.             'Enrollment accepted',
  44.             'Your enrollment for ' $event->getEnrollment()->getStudent()->getName() . ' has been accepted.
  45.             Please proceed to payment by clicking the link below.',
  46.             $url,
  47.         );
  48.         // Save notification
  49.         $this->notificationRepository->save($notificationtrue);
  50.     }
  51. }