<?phpnamespace App\EventListener;use App\Repository\OrderRepository;use App\Service\SponsorshipService;use Doctrine\ORM\NoResultException;use FOS\UserBundle\Event\FormEvent;use FOS\UserBundle\Event\GetResponseUserEvent;use FOS\UserBundle\FOSUserEvents;use Psr\Log\LoggerInterface;use Stripe\Event;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RequestStack;use Symfony\Component\HttpKernel\Event\ControllerEvent;use Symfony\Component\HttpKernel\KernelEvents;use Wits\PaymentBundle\Event\PaymentEvent;/** * Class SponsorshipListener listens to different events during the process of sponsorship * - kernel.controller: to search for param in request and store it in the session * - fos_user.registration.success: link a sponsorship to a user * - fos_user.registration.confirm: offer credit to new user * - payement_intent.succeeded: offer credit to the sponsor * @package App\EventListener */class SponsorshipListener implements EventSubscriberInterface{ const TOKEN_KEY = 'sponsor'; private $requestStack; private $sponsorshipService; private $orderRepository; private $logger; public function __construct(RequestStack $requestStack, SponsorshipService $sponsorshipService, OrderRepository $orderRepository, LoggerInterface $logger) { $this->requestStack = $requestStack; $this->sponsorshipService = $sponsorshipService; $this->orderRepository = $orderRepository; $this->logger = $logger; } public static function getSubscribedEvents() { return [ KernelEvents::CONTROLLER => 'onKernelController', FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess', FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm', Event::PAYMENT_INTENT_SUCCEEDED => 'onPaymentConfirmed' ]; } /** * search for param in request and store it in the session * @param ControllerEvent $event */ public function onKernelController(ControllerEvent $event) { $request = $event->getRequest(); if (!$event->isMasterRequest()) { return; } if ($request->query->has(self::TOKEN_KEY)) { $session = $request->getSession(); // stores an attribute in the session for later reuse $session->set(self::TOKEN_KEY, $request->get(self::TOKEN_KEY)); $this->logger->info('sponsorship_token has been found ' . $request->get(self::TOKEN_KEY)); } } /** * link a sponsorship to a user * cf https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php * @param FormEvent $event * @throws \Doctrine\ORM\NonUniqueResultException */ public function onRegistrationSuccess(FormEvent $event) { $this->logger->info('onRegistrationSuccess'); //search session for sponsorship token $session = $event->getRequest()->getSession(); if ($session->has(self::TOKEN_KEY)) { //associate sponsorship and user $this->sponsorshipService->associateGuestAndSponsorship($session->get(self::TOKEN_KEY), $event->getForm()->getData()); } } /** * offer credit to new user * cf https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php * @param GetResponseUserEvent $event * @throws \Doctrine\ORM\NonUniqueResultException */ public function onRegistrationConfirm(GetResponseUserEvent $event) { $this->logger->info('onRegistrationConfirm'); $this->sponsorshipService->confirmAccountCreation($event->getUser()); } /** * Method triggered when a payment confirmation is received * offer credit to the sponsor * @param PaymentEvent $event * @throws \Doctrine\ORM\NoResultException * @throws \Doctrine\ORM\NonUniqueResultException */ public function onPaymentConfirmed(PaymentEvent $event) { $this->logger->info('SponsorshipTokenListener onPaymentConfirmed'); $payment = $event->getPayment(); if ($payment) { $this->logger->debug('Payment ' . $payment->getId() . ' has been confirmed'); $order = $this->orderRepository->findOrderAssociatedAtPayment($payment); if($order){ $this->logger->debug('associated order is ' . $order->getId()); $this->sponsorshipService->confirmFirstOrder($order); }else{ $this->logger->debug('No Order found (associated with payment '.$payment->getId().' )'); } } else { $this->logger->warning('No payment associated with event onPaymentConfirmed'); } }}