src/EventListener/MassPaymentInitiatedListener.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Domain\DTO\ProductSelectionDTO;
  4. use App\Domain\Event\MassPaymentInitiated;
  5. use App\Domain\Exception\DomainException;
  6. use App\Domain\Exception\RequiredException;
  7. use App\Services\DomainValuesService;
  8. use App\Services\Aml\ProductPriceEngine;
  9. use App\Services\Transaction\Fee\FeeDestination;
  10. use App\Services\Transaction\Fee\ManualFee\ManualFeeService;
  11. use App\Entity\Product;
  12. use App\Entity\ProductAction;
  13. use App\Repository\AccountRepository;
  14. use BrokerAction\DTO\TransactionMessage\MessageComponent\Payload;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class MassPaymentInitiatedListener implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var ProductPriceEngine
  21.      */
  22.     private $productEngine;
  23.     /**
  24.      * @var LoggerInterface
  25.      */
  26.     private $logger;
  27.     /**
  28.      * @var ManualFeeService
  29.      */
  30.     private $feeService;
  31.     /**
  32.      * @var DomainValuesService
  33.      */
  34.     private $domainValuesService;
  35.     /**
  36.      * @var FeeDestination
  37.      */
  38.     private $feeDestination;
  39.     /**
  40.      * @var AccountRepository
  41.      */
  42.     private $accountRepository;
  43.     public function __construct(
  44.         ProductPriceEngine $productEngine,
  45.         LoggerInterface $logger,
  46.         ManualFeeService $feeService,
  47.         DomainValuesService $domainValuesService,
  48.         FeeDestination $feeDestination,
  49.         AccountRepository $accountRepository
  50.     ) {
  51.         $this->productEngine $productEngine;
  52.         $this->logger $logger;
  53.         $this->feeService $feeService;
  54.         $this->domainValuesService $domainValuesService;
  55.         $this->feeDestination $feeDestination;
  56.         $this->accountRepository $accountRepository;
  57.     }
  58.     public function createFee(MassPaymentInitiated $event)
  59.     {
  60.         try {
  61.             $productSelectionDTO = new ProductSelectionDTO(
  62.                 ProductAction::MASS_PAYMENTS,
  63.                 0,
  64.                 $event->getClient()->getId()
  65.             );
  66.             $engineResponse $this->productEngine->run($productSelectionDTO);
  67.             /** @var Product $product */
  68.             $product $engineResponse->getContent();
  69.             if (!$product) {
  70.                 throw new DomainException('Product was not found');
  71.             }
  72.             $account $this->accountRepository->getByClientAndCurrency(
  73.                 (int) $event->getClient()->getId(),
  74.                 $this->domainValuesService->getSystemCurrency()
  75.             );
  76.             if ($account === null) {
  77.                 throw new RequiredException(sprintf(RequiredException::PARAMETERS_NOT_SET'iban'));
  78.             }
  79.             $this->feeService->sendMessage(new Payload([
  80.                 'debtor_account_id' => $account->getId(),
  81.                 'creditor_account_id' => $this->feeDestination->getFeeCollectionAccount()->getId(),
  82.                 'currency' => $this->domainValuesService->getSystemCurrency(),
  83.                 'amount' => $product->getPrice(),
  84.                 'product_id' => $product->getId(),
  85.                 'parent_id' => null,
  86.                 'trx_purpose' => $product->getPriceName(),
  87.             ]));
  88.         } catch (DomainException $exception) {
  89.             $this->logger->notice($exception->getMessage());
  90.         }
  91.     }
  92.     /**
  93.      * Returns an array of event names this subscriber wants to listen to.
  94.      *
  95.      * The array keys are event names and the value can be:
  96.      *
  97.      *  * The method name to call (priority defaults to 0)
  98.      *  * An array composed of the method name to call and the priority
  99.      *  * An array of arrays composed of the method names to call and respective
  100.      *    priorities, or 0 if unset
  101.      *
  102.      * For instance:
  103.      *
  104.      *  * array('eventName' => 'methodName')
  105.      *  * array('eventName' => array('methodName', $priority))
  106.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  107.      *
  108.      * @return array The event names to listen to
  109.      */
  110.     public static function getSubscribedEvents()
  111.     {
  112.         return [
  113.             MassPaymentInitiated::MASS_PAYMENT_INITIATED => 'createFee',
  114.         ];
  115.     }
  116. }