<?php
namespace App\Service\App\Merchant\Affiliate\Listener;
use App\Background\Data\CalculateAffiliateCommissionData;
use App\Core\BackgroundWork\Contract\IClient;
use App\Core\MerchantSelector\Contract\IMerchantSelector;
use App\Service\App\Merchant\Payment\Event\ConfirmPaymentEvent;
use App\Service\App\Merchant\Payment\Event\RefundPaymentEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AffiliateListener implements EventSubscriberInterface
{
/**
* Client
*
* @var IClient
*/
private IClient $client;
/**
* Merchant selector
*
* @var IMerchantSelector
*/
private IMerchantSelector $merchantSelector;
/**
* Constructor
*
* @param IClient $client
* @param IMerchantSelector $merchantSelector
*/
public function __construct(IClient $client, IMerchantSelector $merchantSelector)
{
$this->client = $client;
$this->merchantSelector = $merchantSelector;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* The code must not depend on runtime state as it will only be called at compile time.
* All logic depending on runtime state must be put into the individual methods handling the events.
*
* @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
*/
public static function getSubscribedEvents(): array
{
return [
ConfirmPaymentEvent::NAME => 'onConfirmPayment',
RefundPaymentEvent::NAME => 'onRefundPayment'
];
}
/**
* On confirm payment event
*
* @param ConfirmPaymentEvent $event
*/
public function onConfirmPayment(ConfirmPaymentEvent $event): void
{
$this->client->runJob(new CalculateAffiliateCommissionData(
$this->merchantSelector->getMerchant(),
$event->getRequest()->getId()
));
}
/**
* On refund payment event
*
* @param RefundPaymentEvent $event
*/
public function onRefundPayment(RefundPaymentEvent $event): void
{
$this->client->runJob(new CalculateAffiliateCommissionData(
$this->merchantSelector->getMerchant(),
$event->getRequest()->getId()
));
}
}