<?php
namespace App\Modules\TangibleAsset\Event\Subscriber;
use App\Modules\Contract\Entity\Form\BuySell;
use App\Modules\Contract\Event\FormFinalized;
use App\Modules\Contract\Service\ParticipantDetailsService;
use App\Modules\TangibleAsset\Entity\AssetListing;
use App\Modules\TangibleAsset\Entity\AssetOwner;
use App\Modules\TangibleAsset\Factory\AssetAbstractFactory;
use App\Modules\TangibleAsset\Service\AssetDetailsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ChangeAssetOwner implements EventSubscriberInterface
{
/**
* @var ParticipantDetailsService
*/
private $participantDetailsService;
/**
* @var AssetDetailsService
*/
private $assetDetailsService;
/**
* @var AssetAbstractFactory
*/
private $assetAbstractFactory;
public function __construct(
ParticipantDetailsService $participantDetailsService,
AssetDetailsService $assetDetailsService,
AssetAbstractFactory $assetAbstractFactory
)
{
$this->participantDetailsService = $participantDetailsService;
$this->assetDetailsService = $assetDetailsService;
$this->assetAbstractFactory = $assetAbstractFactory;
}
/**
* 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 [
FormFinalized::class => 'onFormFinalized',
];
}
public function onFormFinalized(FormFinalized $event)
{
$form = $event->getForm();
$contract = $form->getContract();
$asset = $contract->getAsset();
if ($form->getType() != BuySell::TYPE || is_null($asset)) {
return;
}
$executor = $contract->getExecutor();
/** @var AssetOwner $executorDetails */
$executorDetails = $this->participantDetailsService->get($executor->getId(), $executor->getType());
$assetDetails = $this->assetDetailsService->get($asset->getId(), $asset->getType());
if ($assetDetails->isListing()) {
/** @var $assetDetails AssetListing*/
$assetDetails = $assetDetails->getAsset();
}
$assetService = $this->assetAbstractFactory->createAssetService($assetDetails->getType());
$assetService->changeAssetOwner($assetDetails, $executorDetails->getAssetOwnerID());
}
}