src/EventListener/SponsorshipListener.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Repository\OrderRepository;
  4. use App\Service\SponsorshipService;
  5. use Doctrine\ORM\NoResultException;
  6. use FOS\UserBundle\Event\FormEvent;
  7. use FOS\UserBundle\Event\GetResponseUserEvent;
  8. use FOS\UserBundle\FOSUserEvents;
  9. use Psr\Log\LoggerInterface;
  10. use Stripe\Event;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Wits\PaymentBundle\Event\PaymentEvent;
  16. /**
  17.  * Class SponsorshipListener listens to different events during the process of sponsorship
  18.  * - kernel.controller: to search for param in request and store it in the session
  19.  * - fos_user.registration.success: link a sponsorship to a user
  20.  * - fos_user.registration.confirm: offer credit to new user
  21.  * - payement_intent.succeeded: offer credit to the sponsor
  22.  * @package App\EventListener
  23.  */
  24. class SponsorshipListener implements EventSubscriberInterface
  25. {
  26.     const TOKEN_KEY 'sponsor';
  27.     private $requestStack;
  28.     private $sponsorshipService;
  29.     private $orderRepository;
  30.     private $logger;
  31.     public function __construct(RequestStack $requestStackSponsorshipService $sponsorshipServiceOrderRepository $orderRepositoryLoggerInterface $logger)
  32.     {
  33.         $this->requestStack $requestStack;
  34.         $this->sponsorshipService $sponsorshipService;
  35.         $this->orderRepository $orderRepository;
  36.         $this->logger $logger;
  37.     }
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             KernelEvents::CONTROLLER => 'onKernelController',
  42.             FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
  43.             FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm',
  44.             Event::PAYMENT_INTENT_SUCCEEDED => 'onPaymentConfirmed'
  45.         ];
  46.     }
  47.     /**
  48.      * search for param in request and store it in the session
  49.      * @param ControllerEvent $event
  50.      */
  51.     public function onKernelController(ControllerEvent $event)
  52.     {
  53.         $request $event->getRequest();
  54.         if (!$event->isMasterRequest()) {
  55.             return;
  56.         }
  57.         if ($request->query->has(self::TOKEN_KEY)) {
  58.             $session $request->getSession();
  59.             // stores an attribute in the session for later reuse
  60.             $session->set(self::TOKEN_KEY$request->get(self::TOKEN_KEY));
  61.             $this->logger->info('sponsorship_token has been found ' $request->get(self::TOKEN_KEY));
  62.         }
  63.     }
  64.     /**
  65.      * link a sponsorship to a user
  66.      * cf https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php
  67.      * @param FormEvent $event
  68.      * @throws \Doctrine\ORM\NonUniqueResultException
  69.      */
  70.     public function onRegistrationSuccess(FormEvent $event)
  71.     {
  72.         $this->logger->info('onRegistrationSuccess');
  73.         //search session for sponsorship token
  74.         $session $event->getRequest()->getSession();
  75.         if ($session->has(self::TOKEN_KEY)) {
  76.             //associate sponsorship and user
  77.             $this->sponsorshipService->associateGuestAndSponsorship($session->get(self::TOKEN_KEY), $event->getForm()->getData());
  78.         }
  79.     }
  80.     /**
  81.      * offer credit to new user
  82.      * cf https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php
  83.      * @param GetResponseUserEvent $event
  84.      * @throws \Doctrine\ORM\NonUniqueResultException
  85.      */
  86.     public function onRegistrationConfirm(GetResponseUserEvent $event)
  87.     {
  88.         $this->logger->info('onRegistrationConfirm');
  89.         $this->sponsorshipService->confirmAccountCreation($event->getUser());
  90.     }
  91.     /**
  92.      * Method triggered when a payment confirmation is received
  93.      * offer credit to the sponsor
  94.      * @param PaymentEvent $event
  95.      * @throws \Doctrine\ORM\NoResultException
  96.      * @throws \Doctrine\ORM\NonUniqueResultException
  97.      */
  98.     public function onPaymentConfirmed(PaymentEvent $event)
  99.     {
  100.         $this->logger->info('SponsorshipTokenListener onPaymentConfirmed');
  101.         $payment $event->getPayment();
  102.         if ($payment) {
  103.             $this->logger->debug('Payment ' $payment->getId() . ' has been confirmed');
  104.             $order $this->orderRepository->findOrderAssociatedAtPayment($payment);
  105.             if($order){
  106.                 $this->logger->debug('associated order is ' $order->getId());
  107.                 $this->sponsorshipService->confirmFirstOrder($order);
  108.             }else{
  109.                 $this->logger->debug('No Order found (associated with payment '.$payment->getId().' )');
  110.             }
  111.         } else {
  112.             $this->logger->warning('No payment associated with event onPaymentConfirmed');
  113.         }
  114.     }
  115. }