<?php
namespace App\EventListener;
use App\Domain\DTO\ProductSelectionDTO;
use App\Domain\Event\AccountCreated;
use App\Domain\Exception\DomainException;
use App\Services\DomainValuesService;
use App\Services\Aml\ProductPriceEngine;
use App\Services\Transaction\Fee\FeeDestination;
use App\Services\Transaction\Fee\ManualFee\ManualFeeService;
use App\Entity\Account;
use App\Entity\Product;
use App\Entity\ProductAction;
use App\Repository\ProductActionRepository;
use BrokerAction\DTO\TransactionMessage\MessageComponent\Payload;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AccountCreatedListener implements EventSubscriberInterface
{
/**
* @var ProductActionRepository
*/
private $productActionRepository;
/**
* @var ProductPriceEngine
*/
private $productEngine;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var ManualFeeService
*/
private $feeService;
/**
* @var DomainValuesService
*/
private $domainValuesService;
/**
* @var FeeDestination
*/
private $feeDestination;
public function __construct(
ProductActionRepository $productActionRepository,
ProductPriceEngine $productEngine,
LoggerInterface $logger,
ManualFeeService $feeService,
DomainValuesService $domainValuesService,
FeeDestination $feeDestination
) {
$this->productActionRepository = $productActionRepository;
$this->productEngine = $productEngine;
$this->logger = $logger;
$this->feeService = $feeService;
$this->domainValuesService = $domainValuesService;
$this->feeDestination = $feeDestination;
}
public function createFee(AccountCreated $event)
{
try {
$account = $event->getAccount();
$productSelectionDTO = new ProductSelectionDTO(
ProductAction::BANK_ACCOUNT,
0,
$account->getClient()->getId()
);
// if it's first account created, then product_instance with value 1 must be added to DTO.
$event->isFirst() && $productSelectionDTO->setProductInstance(1);
$engineResponse = $this->productEngine->run($productSelectionDTO);
/** @var Product $product */
$product = $engineResponse->getContent();
if (!$product) {
throw new DomainException('Product was not found');
return;
}
$this->feeService->sendMessage(new Payload([
'product_id' => $product->getId(),
'debtor_account_id' => $account->getId(),
'currency' => $event->getCurrency(),
]));
} catch (DomainException $exception) {
$this->logger->notice($exception->getMessage());
}
}
/**
* 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:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
Account::CREATE_EVENT => 'createFee',
];
}
}