src/Modules/ActionsList/Event/Subscriber/ContractLogAction.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\Modules\ActionsList\Event\Subscriber;
  3. use App\Modules\ActionsList\Action\ActionInterface;
  4. use App\Modules\ActionsList\Action\ContractCreatedAction;
  5. use App\Modules\ActionsList\Action\ContractFinalizedAction;
  6. use App\Modules\ActionsList\Service\ActionsListService;
  7. use App\Modules\Contract\Entity\Contract;
  8. use App\Modules\Contract\Event\ContractCreated;
  9. use App\Modules\Contract\Event\FormFinalized;
  10. use App\Modules\Contract\ValueObject\Asset;
  11. use App\Modules\TangibleAsset\Service\AssetDetailsService;
  12. use App\Modules\TangibleAsset\Service\AssetOwnerDetailsService;
  13. use App\Modules\User\Service\CircleMemberService;
  14. use App\Modules\User\Service\PartnerService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. class ContractLogAction implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var ActionsListService
  21.      */
  22.     private $actionsListService;
  23.     /**
  24.      * @var AssetOwnerDetailsService
  25.      */
  26.     private $assetOwnerDetailsService;
  27.     /**
  28.      * @var TokenStorageInterface
  29.      */
  30.     private $tokenStorage;
  31.     /**
  32.      * @var CircleMemberService
  33.      */
  34.     private $circleMemberService;
  35.     /**
  36.      * @var PartnerService
  37.      */
  38.     private $partnerService;
  39.     /**
  40.      * @var AssetDetailsService
  41.      */
  42.     private $assetDetailsService;
  43.     public function __construct(
  44.         ActionsListService $actionsListService,
  45.         AssetOwnerDetailsService $assetOwnerDetailsService,
  46.         AssetDetailsService $assetDetailsService,
  47.         TokenStorageInterface $tokenStorage,
  48.         CircleMemberService $circleMemberService,
  49.         PartnerService $partnerService
  50.     )
  51.     {
  52.         $this->actionsListService $actionsListService;
  53.         $this->assetOwnerDetailsService $assetOwnerDetailsService;
  54.         $this->tokenStorage $tokenStorage;
  55.         $this->circleMemberService $circleMemberService;
  56.         $this->partnerService $partnerService;
  57.         $this->assetDetailsService $assetDetailsService;
  58.     }
  59.     /**
  60.      * Returns an array of event names this subscriber wants to listen to.
  61.      *
  62.      * The array keys are event names and the value can be:
  63.      *
  64.      *  * The method name to call (priority defaults to 0)
  65.      *  * An array composed of the method name to call and the priority
  66.      *  * An array of arrays composed of the method names to call and respective
  67.      *    priorities, or 0 if unset
  68.      *
  69.      * For instance:
  70.      *
  71.      *  * ['eventName' => 'methodName']
  72.      *  * ['eventName' => ['methodName', $priority]]
  73.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  74.      *
  75.      * @return array The event names to listen to
  76.      */
  77.     public static function getSubscribedEvents()
  78.     {
  79.         return [
  80.             ContractCreated::class => 'onContractCreated',
  81.             FormFinalized::class => 'onFormFinalized'
  82.         ];
  83.     }
  84.     public function onContractCreated(ContractCreated $event)
  85.     {
  86.         $assetType $assetTitle null;
  87.         $contract $event->getContract();
  88.         $service $contract->getService();
  89.         $asset $contract->getAsset();
  90.         if (!is_null($asset)) {
  91.             $assetDetails $this->assetDetailsService->get($asset->getId(), $asset->getType());
  92.             $assetTitle $assetDetails->getTitle();
  93.             $assetType Asset::getTypeTitle($assetDetails->getType());
  94.         }
  95.         $action = new ContractCreatedAction($assetTitle$assetType$service->getTitle());
  96.         $this->setInitiatorData($action$contract);
  97.         $this->actionsListService->logAction($action);
  98.     }
  99.     public function onFormFinalized(FormFinalized $event)
  100.     {
  101.         $contract $event->getForm()->getContract();
  102.         $action = new ContractFinalizedAction($contract->getRef());
  103.         $this->setInitiatorData($action$contract);
  104.         $this->actionsListService->logAction($action);
  105.     }
  106.     private function setInitiatorData(ActionInterface $actionContract $contract)
  107.     {
  108.         $contractInitiator $contract->getInitiator();
  109.         $action->setInitiator($this->getInitiator());
  110.         if (!$contractInitiator->isPrivateOwner()) {
  111.             if ($contractInitiator->isCircleMember()) {
  112.                 $circleMember $this->circleMemberService->get($contractInitiator->getId());
  113.                 $action->setCircleMember($circleMember);
  114.             } elseif ($contractInitiator->isPartner()) {
  115.                 $partner $this->partnerService->get($contractInitiator->getId());
  116.                 $action->setPartner($partner);
  117.             }
  118.         }
  119.     }
  120.     private function getInitiator()
  121.     {
  122.         $token $this->tokenStorage->getToken();
  123.         if (!is_null($token)) {
  124.             return $token->getUser();
  125.         }
  126.     }
  127. }