<?php
namespace App\EventListener;
use App\Domain\DTO\ProductSelectionDTO;
use App\Domain\Event\MassPaymentInitiated;
use App\Domain\Exception\DomainException;
use App\Domain\Exception\RequiredException;
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\Product;
use App\Entity\ProductAction;
use App\Repository\AccountRepository;
use BrokerAction\DTO\TransactionMessage\MessageComponent\Payload;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MassPaymentInitiatedListener implements EventSubscriberInterface
{
/**
* @var ProductPriceEngine
*/
private $productEngine;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var ManualFeeService
*/
private $feeService;
/**
* @var DomainValuesService
*/
private $domainValuesService;
/**
* @var FeeDestination
*/
private $feeDestination;
/**
* @var AccountRepository
*/
private $accountRepository;
public function __construct(
ProductPriceEngine $productEngine,
LoggerInterface $logger,
ManualFeeService $feeService,
DomainValuesService $domainValuesService,
FeeDestination $feeDestination,
AccountRepository $accountRepository
) {
$this->productEngine = $productEngine;
$this->logger = $logger;
$this->feeService = $feeService;
$this->domainValuesService = $domainValuesService;
$this->feeDestination = $feeDestination;
$this->accountRepository = $accountRepository;
}
public function createFee(MassPaymentInitiated $event)
{
try {
$productSelectionDTO = new ProductSelectionDTO(
ProductAction::MASS_PAYMENTS,
0,
$event->getClient()->getId()
);
$engineResponse = $this->productEngine->run($productSelectionDTO);
/** @var Product $product */
$product = $engineResponse->getContent();
if (!$product) {
throw new DomainException('Product was not found');
}
$account = $this->accountRepository->getByClientAndCurrency(
(int) $event->getClient()->getId(),
$this->domainValuesService->getSystemCurrency()
);
if ($account === null) {
throw new RequiredException(sprintf(RequiredException::PARAMETERS_NOT_SET, 'iban'));
}
$this->feeService->sendMessage(new Payload([
'debtor_account_id' => $account->getId(),
'creditor_account_id' => $this->feeDestination->getFeeCollectionAccount()->getId(),
'currency' => $this->domainValuesService->getSystemCurrency(),
'amount' => $product->getPrice(),
'product_id' => $product->getId(),
'parent_id' => null,
'trx_purpose' => $product->getPriceName(),
]));
} 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 [
MassPaymentInitiated::MASS_PAYMENT_INITIATED => 'createFee',
];
}
}