src/Service/Simplypay/Subscribers/UserDeletedSubscriber.php line 71

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\Simplypay\Subscribers;
  3. use Harmonizely\Service\Simplypay\Contract\MerchantApiClientFactoryInterface;
  4. use Harmonizely\Service\Simplypay\Contract\MerchantApiClientInterface;
  5. use Harmonizely\Service\Simplypay\Contract\SubscriptionHelperServiceInterface;
  6. use Harmonizely\User\UserDeletedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class UserDeletedSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * Subscription helper
  12.      *
  13.      * @var SubscriptionHelperServiceInterface
  14.      */
  15.     private SubscriptionHelperServiceInterface $subscriptionHelperService;
  16.     /**
  17.      * Client factory
  18.      *
  19.      * @var MerchantApiClientFactoryInterface
  20.      */
  21.     private MerchantApiClientFactoryInterface $clientFactory;
  22.     /**
  23.      * Constructor
  24.      *
  25.      * @param SubscriptionHelperServiceInterface $subscriptionHelperService
  26.      * @param MerchantApiClientFactoryInterface $clientFactory
  27.      */
  28.     public function __construct(
  29.         SubscriptionHelperServiceInterface $subscriptionHelperService,
  30.         MerchantApiClientFactoryInterface $clientFactory
  31.     )
  32.     {
  33.         $this->subscriptionHelperService $subscriptionHelperService;
  34.         $this->clientFactory $clientFactory;
  35.     }
  36.     /**
  37.      * Returns an array of event names this subscriber wants to listen to.
  38.      *
  39.      * The array keys are event names and the value can be:
  40.      *
  41.      *  * The method name to call (priority defaults to 0)
  42.      *  * An array composed of the method name to call and the priority
  43.      *  * An array of arrays composed of the method names to call and respective
  44.      *    priorities, or 0 if unset
  45.      *
  46.      * For instance:
  47.      *
  48.      *  * ['eventName' => 'methodName']
  49.      *  * ['eventName' => ['methodName', $priority]]
  50.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  51.      *
  52.      * The code must not depend on runtime state as it will only be called at compile time.
  53.      * All logic depending on runtime state must be put into the individual methods handling the events.
  54.      *
  55.      * @return array The event names to listen to
  56.      */
  57.     public static function getSubscribedEvents(): array
  58.     {
  59.         return [
  60.             UserDeletedEvent::NAME => 'onUserDeleted',
  61.         ];
  62.     }
  63.     public function onUserDeleted(UserDeletedEvent $event): void
  64.     {
  65.         $userId $this->subscriptionHelperService->encodeUserId(strval($event->getUserId()));
  66.         $client $this->buildClient();
  67.         $client->cancelCustomerRecurringProfiles(
  68.             $userId'User deleted account. ' $event->getReason()
  69.         );
  70.     }
  71.     /**
  72.      * Build client
  73.      *
  74.      * @return MerchantApiClientInterface
  75.      */
  76.     private function buildClient(): MerchantApiClientInterface
  77.     {
  78.         // @todo it is better to move this to separate service
  79.         return $this->clientFactory->build(
  80.             $this->subscriptionHelperService->getToken(),
  81.             $this->subscriptionHelperService->getSecret(),
  82.             $this->subscriptionHelperService->getMerchant()
  83.         );
  84.     }
  85. }