src/Modules/ActionsList/Event/Subscriber/ListingLogAction.php line 72

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\ListingAvailabilityChangedAction;
  5. use App\Modules\ActionsList\Action\NewListingCreatedAction;
  6. use App\Modules\ActionsList\Service\ActionsListService;
  7. use App\Modules\Contract\ValueObject\Asset;
  8. use App\Modules\TangibleAsset\Aircraft\Entity\Listing;
  9. use App\Modules\TangibleAsset\Entity\AssetDetails;
  10. use App\Modules\TangibleAsset\Event\ListingAvailabilityChanged;
  11. use App\Modules\TangibleAsset\Event\ListingCreated;
  12. use App\Modules\TangibleAsset\Service\AssetOwnerDetailsService;
  13. use App\Modules\User\Entity\User;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. class ListingLogAction 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.     public function __construct(
  32.         ActionsListService $actionsListService,
  33.         AssetOwnerDetailsService $assetOwnerDetailsService,
  34.         TokenStorageInterface $tokenStorage
  35.     )
  36.     {
  37.         $this->actionsListService $actionsListService;
  38.         $this->assetOwnerDetailsService $assetOwnerDetailsService;
  39.         $this->tokenStorage $tokenStorage;
  40.     }
  41.     /**
  42.      * Returns an array of event names this subscriber wants to listen to.
  43.      *
  44.      * The array keys are event names and the value can be:
  45.      *
  46.      *  * The method name to call (priority defaults to 0)
  47.      *  * An array composed of the method name to call and the priority
  48.      *  * An array of arrays composed of the method names to call and respective
  49.      *    priorities, or 0 if unset
  50.      *
  51.      * For instance:
  52.      *
  53.      *  * ['eventName' => 'methodName']
  54.      *  * ['eventName' => ['methodName', $priority]]
  55.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  56.      *
  57.      * @return array The event names to listen to
  58.      */
  59.     public static function getSubscribedEvents()
  60.     {
  61.         return [
  62.             ListingCreated::class => 'onListingCreated',
  63.             ListingAvailabilityChanged::class => 'onListingAvailabilityChanged',
  64.         ];
  65.     }
  66.     public function onListingCreated(ListingCreated $event)
  67.     {
  68.         $listing $event->getListing();
  69.         $listingTitle $listing->getTitle();
  70.         $listingType Asset::getTypeTitle($listing->getType());
  71.         $action = new NewListingCreatedAction($listingTitle$listingType);
  72.         $this->setInitiatorData($action$listing);
  73.         $this->actionsListService->logAction($action);
  74.     }
  75.     public function onListingAvailabilityChanged(ListingAvailabilityChanged $event)
  76.     {
  77.         $listing $event->getListing();
  78.         $listingTitle $listing->getTitle();
  79.         $listingType Asset::getTypeTitle($listing->getType());
  80.         $availability $listing->getActive();
  81.         $action = new ListingAvailabilityChangedAction($listingTitle$listingType$availability);
  82.         $this->setInitiatorData($action$listing);
  83.         $this->actionsListService->logAction($action);
  84.     }
  85.     private function setInitiatorData(ActionInterface $actionAssetDetails $listing)
  86.     {
  87.         $assetOwner $listing->getOwnerID();
  88.         $action->setInitiator($this->getInitiator());
  89.         if (!$assetOwner->isPrivateOwner()) {
  90.             $ownerDetails $this->assetOwnerDetailsService->get($assetOwner->getId(), $assetOwner->getOwnerType());
  91.             if ($assetOwner->isCircleMember()) {
  92.                 $action->setCircleMember($ownerDetails);
  93.             } elseif ($assetOwner->isPartner()) {
  94.                 $action->setPartner($ownerDetails);
  95.             }
  96.         }
  97.     }
  98.     private function getInitiator()
  99.     {
  100.         $token $this->tokenStorage->getToken();
  101.         if (!is_null($token)) {
  102.             return $token->getUser();
  103.         }
  104.     }
  105. }