src/EventSubscriber/EnrollmentSubscriber.php line 40
<?php
namespace App\EventSubscriber;
use App\Entity\Notification;
use App\Event\EnrollmentAcceptedEvent;
use App\Repository\NotificationRepository;
use App\Services\PaymentRecordService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class EnrollmentSubscriber implements EventSubscriberInterface
{
public function __construct(
private UrlGeneratorInterface $urlGenerator,
private NotificationRepository $notificationRepository,
private PaymentRecordService $paymentService,
)
{
}
public static function getSubscribedEvents(): array
{
return [
EnrollmentAcceptedEvent::NAME =>
[
['createPayment', 1],
['createNotification', 0],
],
];
}
public function createPayment(EnrollmentAcceptedEvent $event): void
{
$this->paymentService->new($event->getEnrollment());
}
public function createNotification(EnrollmentAcceptedEvent $event): void
{
// User to notify
$user = $event->getEnrollment()->getGuardian()->getUser();
// Payment URL
$url = $this->urlGenerator->generate('app_payment_show',
['id' => $event->getEnrollment()->getPaymentRecords()->last()->getId()],
UrlGeneratorInterface::ABSOLUTE_URL);
// Create notification
$notification = new Notification(
$user,
'Enrollment accepted',
'Your enrollment for ' . $event->getEnrollment()->getStudent()->getName() . ' has been accepted.
Please proceed to payment by clicking the link below.',
$url,
);
// Save notification
$this->notificationRepository->save($notification, true);
}
}