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\MerchantApiClientFactoryInterface;
  7. use Harmonizely\Service\Simplypay\Contract\SubscriptionHelperServiceInterface;
  8. use Harmonizely\Service\Simplypay\DTO\Merchant\Request\ApplyFreeSubscriptionRequestDTO;
  9. use Harmonizely\Service\Simplypay\DTO\Merchant\Request\CustomerRequestDTO;
  10. use Harmonizely\Service\Simplypay\SimplypayClientException;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ApplyFreeSubscriptionListener implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * Subscription service
  18.      *
  19.      * @var SubscriptionHelperServiceInterface
  20.      */
  21.     private SubscriptionHelperServiceInterface $subscriptionService;
  22.     /**
  23.      * Client factory
  24.      *
  25.      * @var MerchantApiClientFactoryInterface
  26.      */
  27.     private MerchantApiClientFactoryInterface $clientFactory;
  28.     /**
  29.      * Logger interface
  30.      *
  31.      * @var LoggerInterface
  32.      */
  33.     private LoggerInterface $logger;
  34.     /**
  35.      * Constructor
  36.      *
  37.      * @param SubscriptionHelperServiceInterface $subscriptionService
  38.      * @param MerchantApiClientFactoryInterface $clientFactory
  39.      * @param LoggerInterface $logger
  40.      */
  41.     public function __construct(
  42.         SubscriptionHelperServiceInterface $subscriptionService,
  43.         MerchantApiClientFactoryInterface $clientFactory,
  44.         LoggerInterface $logger
  45.     )
  46.     {
  47.         $this->subscriptionService $subscriptionService;
  48.         $this->clientFactory $clientFactory;
  49.         $this->logger $logger;
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         return [
  57.             FOSUserEvents::REGISTRATION_CONFIRMED => 'confirmed'
  58.         ];
  59.     }
  60.     /**
  61.      * On complete and confirm registration
  62.      *
  63.      * @param FilterUserResponseEvent $filterUserResponseEvent
  64.      * @param string $eventName
  65.      * @param EventDispatcherInterface $eventDispatcher
  66.      */
  67.     public function confirmed(
  68.         FilterUserResponseEvent $filterUserResponseEventstring $eventNameEventDispatcherInterface $eventDispatcher
  69.     )
  70.     {
  71.         $client $this->clientFactory->build(
  72.             $this->subscriptionService->getToken(),
  73.             $this->subscriptionService->getSecret(),
  74.             $this->subscriptionService->getMerchant()
  75.         );
  76.         try {
  77.             $user $filterUserResponseEvent->getUser();
  78.             if (!($user instanceof User)) {
  79.                 $this->logger->error(
  80.                     'Could not apply default free subscription, because invalid user provided'
  81.                 );
  82.                 return;
  83.             }
  84.             $phone '';
  85.             if ($user->getPhoneNumber()) {
  86.                 $phone $user->getPhoneNumber()->getCountryCode() . $user->getPhoneNumber()->getNationalNumber();
  87.             }
  88.             $customer = new CustomerRequestDTO(
  89.                 $this->subscriptionService->encodeUserId($user->getSubscriptionUserId()),
  90.                 $user->getFullName(), $user->getEmail(), $phone$user->getCountryId(),
  91.                 ''$user->getAddress(), $user->getCity(), $user->getZip(),
  92.                 $user->getReferral(), \DateTimeImmutable::createFromMutable($user->getCreatedAt()),
  93.                 !$user->getUserEmailUnsubscribe()
  94.             );
  95.             $request = new ApplyFreeSubscriptionRequestDTO(
  96.                 $customer$this->subscriptionService->getFreePlanCode(), null
  97.             );
  98.             $client->applyFreeSubscription(
  99.                 $this->subscriptionService->getProject(), $request
  100.             );
  101.         } catch (SimplypayClientException $err) {
  102.             $this->logger->error('Could not apply default free subscription', [
  103.                 'error'  => $err->getMessage(),
  104.                 'errors' => $err->getErrors(),
  105.                 'user'   => $filterUserResponseEvent->getUser()->getId(),
  106.             ]);
  107.         }
  108.     }
  109. }