src/Modules/TangibleAsset/Event/Subscriber/ChangeListingOccupation.php line 91

Open in your IDE?
  1. <?php
  2. namespace App\Modules\TangibleAsset\Event\Subscriber;
  3. use App\Modules\Contract\Entity\Contract;
  4. use App\Modules\Contract\Event\ContractCreated;
  5. use App\Modules\Contract\Event\FormDeclined;
  6. use App\Modules\Contract\Event\FormFinalized;
  7. use App\Modules\Contract\ValueObject\Asset;
  8. use App\Modules\TangibleAsset\Entity\ListingDetails;
  9. use App\Modules\TangibleAsset\Factory\AssetAbstractFactory;
  10. use App\Modules\TangibleAsset\Factory\ListingAbstractFactory;
  11. use App\Modules\TangibleAsset\Service\AssetDetailsService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class ChangeListingOccupation implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var AssetDetailsService
  17.      */
  18.     private $assetDetailsService;
  19.     /**
  20.      * @var ListingAbstractFactory
  21.      */
  22.     private $listingAbstractFactory;
  23.     public function __construct(
  24.         AssetDetailsService $assetDetailsService,
  25.         ListingAbstractFactory $listingAbstractFactory
  26.     )
  27.     {;
  28.         $this->assetDetailsService $assetDetailsService;
  29.         $this->listingAbstractFactory $listingAbstractFactory;
  30.     }
  31.     /**
  32.      * Returns an array of event names this subscriber wants to listen to.
  33.      *
  34.      * The array keys are event names and the value can be:
  35.      *
  36.      *  * The method name to call (priority defaults to 0)
  37.      *  * An array composed of the method name to call and the priority
  38.      *  * An array of arrays composed of the method names to call and respective
  39.      *    priorities, or 0 if unset
  40.      *
  41.      * For instance:
  42.      *
  43.      *  * ['eventName' => 'methodName']
  44.      *  * ['eventName' => ['methodName', $priority]]
  45.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  46.      *
  47.      * @return array The event names to listen to
  48.      */
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             ContractCreated::class => 'onContractCreated',
  53.             FormDeclined::class => 'onFormDeclined',
  54.             FormFinalized::class => 'onFormFinalized'
  55.         ];
  56.     }
  57.     public function onContractCreated(ContractCreated $event)
  58.     {
  59.         $contract $event->getContract();
  60.         if (is_null($contract) ||
  61.             is_null($contract->getAsset()) ||
  62.             !in_array($contract->getAsset()->getType(), Asset::$listings)
  63.         ) {
  64.             return;
  65.         }
  66.         $this->changeOccupation($contractfalse);
  67.     }
  68.     public function onFormDeclined(FormDeclined $event)
  69.     {
  70.         $form $event->getForm();
  71.         $contract $form->getContract();
  72.         if (is_null($contract) ||
  73.             is_null($contract->getAsset()) ||
  74.             !in_array($contract->getAsset()->getType(), Asset::$listings)
  75.         ) {
  76.             return;
  77.         }
  78.         $this->changeOccupation($contracttrue);
  79.     }
  80.     public function onFormFinalized(FormFinalized $event)
  81.     {
  82.         $form $event->getForm();
  83.         $contract $form->getContract();
  84.         if (is_null($contract) ||
  85.             is_null($contract->getAsset()) ||
  86.             !in_array($contract->getAsset()->getType(), Asset::$listings)
  87.         ) {
  88.             return;
  89.         }
  90.         $this->changeOccupation($contracttrue);
  91.     }
  92.     private function changeOccupation(Contract $contractbool $occupation)
  93.     {
  94.         $asset $contract->getAsset();
  95.         /** @var ListingDetails $listing */
  96.         $listing $this->assetDetailsService->get($asset->getId(), $asset->getType());
  97.         $listingService $this->listingAbstractFactory->createListingService($asset->getType());
  98.         $listingService->changeListingOccupation($listing$occupation);
  99.     }
  100. }