src/EventListener/ApplyFreeSubscriptionListener.php line 76

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\EventListener;
  3. use FOS\UserBundle\Event\FilterUserResponseEvent;
  4. use FOS\UserBundle\FOSUserEvents;
  5. use Harmonizely\Model\User;
  6. use Harmonizely\Service\Simplypay\Contract\ApplyFreeSubscriptionInterface;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ApplyFreeSubscriptionListener implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * Logger interface
  14.      *
  15.      * @var LoggerInterface
  16.      */
  17.     private LoggerInterface $logger;
  18.     /**
  19.      * @var ApplyFreeSubscriptionInterface
  20.      */
  21.     private ApplyFreeSubscriptionInterface $applyFreeSubscription;
  22.     /**
  23.      * Constructor
  24.      *
  25.      * @param LoggerInterface $logger
  26.      * @param ApplyFreeSubscriptionInterface $applyFreeSubscription
  27.      */
  28.     public function __construct(
  29.         LoggerInterface $logger,
  30.         ApplyFreeSubscriptionInterface $applyFreeSubscription
  31.     )
  32.     {
  33.         $this->logger $logger;
  34.         $this->applyFreeSubscription $applyFreeSubscription;
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             FOSUserEvents::REGISTRATION_CONFIRMED => 'confirmed',
  43.         ];
  44.     }
  45.     /**
  46.      * On complete and confirm registration
  47.      *
  48.      * @param FilterUserResponseEvent $filterUserResponseEvent
  49.      * @param string $eventName
  50.      * @param EventDispatcherInterface $eventDispatcher
  51.      */
  52.     public function confirmed(
  53.         FilterUserResponseEvent $filterUserResponseEvent,
  54.         string $eventName,
  55.         EventDispatcherInterface $eventDispatcher
  56.     )
  57.     {
  58.         $user $filterUserResponseEvent->getUser();
  59.         if (!($user instanceof User)) {
  60.             $this->logger->error(
  61.                 'Could not apply default free subscription, because invalid user provided'
  62.             );
  63.             return;
  64.         }
  65.         $this->applyFreeSubscription->perform($user);
  66.     }
  67. }