<?php
namespace App\Modules\TangibleAsset\Event\Subscriber;
use App\Modules\Contract\Entity\Contract;
use App\Modules\Contract\Event\ContractCreated;
use App\Modules\Contract\Event\FormDeclined;
use App\Modules\Contract\Event\FormFinalized;
use App\Modules\Contract\ValueObject\Asset;
use App\Modules\TangibleAsset\Entity\ListingDetails;
use App\Modules\TangibleAsset\Factory\AssetAbstractFactory;
use App\Modules\TangibleAsset\Factory\ListingAbstractFactory;
use App\Modules\TangibleAsset\Service\AssetDetailsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ChangeListingOccupation implements EventSubscriberInterface
{
/**
* @var AssetDetailsService
*/
private $assetDetailsService;
/**
* @var ListingAbstractFactory
*/
private $listingAbstractFactory;
public function __construct(
AssetDetailsService $assetDetailsService,
ListingAbstractFactory $listingAbstractFactory
)
{;
$this->assetDetailsService = $assetDetailsService;
$this->listingAbstractFactory = $listingAbstractFactory;
}
/**
* 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',
FormDeclined::class => 'onFormDeclined',
FormFinalized::class => 'onFormFinalized'
];
}
public function onContractCreated(ContractCreated $event)
{
$contract = $event->getContract();
if (is_null($contract) ||
is_null($contract->getAsset()) ||
!in_array($contract->getAsset()->getType(), Asset::$listings)
) {
return;
}
$this->changeOccupation($contract, false);
}
public function onFormDeclined(FormDeclined $event)
{
$form = $event->getForm();
$contract = $form->getContract();
if (is_null($contract) ||
is_null($contract->getAsset()) ||
!in_array($contract->getAsset()->getType(), Asset::$listings)
) {
return;
}
$this->changeOccupation($contract, true);
}
public function onFormFinalized(FormFinalized $event)
{
$form = $event->getForm();
$contract = $form->getContract();
if (is_null($contract) ||
is_null($contract->getAsset()) ||
!in_array($contract->getAsset()->getType(), Asset::$listings)
) {
return;
}
$this->changeOccupation($contract, true);
}
private function changeOccupation(Contract $contract, bool $occupation)
{
$asset = $contract->getAsset();
/** @var ListingDetails $listing */
$listing = $this->assetDetailsService->get($asset->getId(), $asset->getType());
$listingService = $this->listingAbstractFactory->createListingService($asset->getType());
$listingService->changeListingOccupation($listing, $occupation);
}
}