src/Service/App/Merchant/Affiliate/Listener/AffiliateListener.php line 88

Open in your IDE?
  1. <?php
  2. namespace App\Service\App\Merchant\Affiliate\Listener;
  3. use App\Background\Data\CalculateAffiliateCommissionData;
  4. use App\Core\BackgroundWork\Contract\IClient;
  5. use App\Core\MerchantSelector\Contract\IMerchantSelector;
  6. use App\Service\App\Merchant\Payment\Event\ConfirmPaymentEvent;
  7. use App\Service\App\Merchant\Payment\Event\RefundPaymentEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class AffiliateListener implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * Client
  13.      *
  14.      * @var IClient
  15.      */
  16.     private IClient $client;
  17.     /**
  18.      * Merchant selector
  19.      *
  20.      * @var IMerchantSelector
  21.      */
  22.     private IMerchantSelector $merchantSelector;
  23.     /**
  24.      * Constructor
  25.      *
  26.      * @param IClient $client
  27.      * @param IMerchantSelector $merchantSelector
  28.      */
  29.     public function __construct(IClient $clientIMerchantSelector $merchantSelector)
  30.     {
  31.         $this->client $client;
  32.         $this->merchantSelector $merchantSelector;
  33.     }
  34.     /**
  35.      * Returns an array of event names this subscriber wants to listen to.
  36.      *
  37.      * The array keys are event names and the value can be:
  38.      *
  39.      *  * The method name to call (priority defaults to 0)
  40.      *  * An array composed of the method name to call and the priority
  41.      *  * An array of arrays composed of the method names to call and respective
  42.      *    priorities, or 0 if unset
  43.      *
  44.      * For instance:
  45.      *
  46.      *  * ['eventName' => 'methodName']
  47.      *  * ['eventName' => ['methodName', $priority]]
  48.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  49.      *
  50.      * The code must not depend on runtime state as it will only be called at compile time.
  51.      * All logic depending on runtime state must be put into the individual methods handling the events.
  52.      *
  53.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  54.      */
  55.     public static function getSubscribedEvents(): array
  56.     {
  57.         return [
  58.             ConfirmPaymentEvent::NAME => 'onConfirmPayment',
  59.             RefundPaymentEvent::NAME => 'onRefundPayment'
  60.         ];
  61.     }
  62.     /**
  63.      * On confirm payment event
  64.      *
  65.      * @param ConfirmPaymentEvent $event
  66.      */
  67.     public function onConfirmPayment(ConfirmPaymentEvent $event): void
  68.     {
  69.         $this->client->runJob(new CalculateAffiliateCommissionData(
  70.             $this->merchantSelector->getMerchant(),
  71.             $event->getRequest()->getId()
  72.         ));
  73.     }
  74.     /**
  75.      * On refund payment event
  76.      *
  77.      * @param RefundPaymentEvent $event
  78.      */
  79.     public function onRefundPayment(RefundPaymentEvent $event): void
  80.     {
  81.         $this->client->runJob(new CalculateAffiliateCommissionData(
  82.             $this->merchantSelector->getMerchant(),
  83.             $event->getRequest()->getId()
  84.         ));
  85.     }
  86. }