<?php
namespace App\Modules\ActionsList\Event\Subscriber;
use App\Modules\ActionsList\Action\ActionInterface;
use App\Modules\ActionsList\Action\ContractCreatedAction;
use App\Modules\ActionsList\Action\ContractFinalizedAction;
use App\Modules\ActionsList\Service\ActionsListService;
use App\Modules\Contract\Entity\Contract;
use App\Modules\Contract\Event\ContractCreated;
use App\Modules\Contract\Event\FormFinalized;
use App\Modules\Contract\ValueObject\Asset;
use App\Modules\TangibleAsset\Service\AssetDetailsService;
use App\Modules\TangibleAsset\Service\AssetOwnerDetailsService;
use App\Modules\User\Service\CircleMemberService;
use App\Modules\User\Service\PartnerService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ContractLogAction implements EventSubscriberInterface
{
/**
* @var ActionsListService
*/
private $actionsListService;
/**
* @var AssetOwnerDetailsService
*/
private $assetOwnerDetailsService;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @var CircleMemberService
*/
private $circleMemberService;
/**
* @var PartnerService
*/
private $partnerService;
/**
* @var AssetDetailsService
*/
private $assetDetailsService;
public function __construct(
ActionsListService $actionsListService,
AssetOwnerDetailsService $assetOwnerDetailsService,
AssetDetailsService $assetDetailsService,
TokenStorageInterface $tokenStorage,
CircleMemberService $circleMemberService,
PartnerService $partnerService
)
{
$this->actionsListService = $actionsListService;
$this->assetOwnerDetailsService = $assetOwnerDetailsService;
$this->tokenStorage = $tokenStorage;
$this->circleMemberService = $circleMemberService;
$this->partnerService = $partnerService;
$this->assetDetailsService = $assetDetailsService;
}
/**
* 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']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
ContractCreated::class => 'onContractCreated',
FormFinalized::class => 'onFormFinalized'
];
}
public function onContractCreated(ContractCreated $event)
{
$assetType = $assetTitle = null;
$contract = $event->getContract();
$service = $contract->getService();
$asset = $contract->getAsset();
if (!is_null($asset)) {
$assetDetails = $this->assetDetailsService->get($asset->getId(), $asset->getType());
$assetTitle = $assetDetails->getTitle();
$assetType = Asset::getTypeTitle($assetDetails->getType());
}
$action = new ContractCreatedAction($assetTitle, $assetType, $service->getTitle());
$this->setInitiatorData($action, $contract);
$this->actionsListService->logAction($action);
}
public function onFormFinalized(FormFinalized $event)
{
$contract = $event->getForm()->getContract();
$action = new ContractFinalizedAction($contract->getRef());
$this->setInitiatorData($action, $contract);
$this->actionsListService->logAction($action);
}
private function setInitiatorData(ActionInterface $action, Contract $contract)
{
$contractInitiator = $contract->getInitiator();
$action->setInitiator($this->getInitiator());
if (!$contractInitiator->isPrivateOwner()) {
if ($contractInitiator->isCircleMember()) {
$circleMember = $this->circleMemberService->get($contractInitiator->getId());
$action->setCircleMember($circleMember);
} elseif ($contractInitiator->isPartner()) {
$partner = $this->partnerService->get($contractInitiator->getId());
$action->setPartner($partner);
}
}
}
private function getInitiator()
{
$token = $this->tokenStorage->getToken();
if (!is_null($token)) {
return $token->getUser();
}
}
}