src/EventListener/AccountCreatedListener.php line 68

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