<?php
namespace App\Modules\ActionsList\Event\Subscriber;
use App\Modules\ActionsList\Action\ActionInterface;
use App\Modules\ActionsList\Action\ListingAvailabilityChangedAction;
use App\Modules\ActionsList\Action\NewListingCreatedAction;
use App\Modules\ActionsList\Service\ActionsListService;
use App\Modules\Contract\ValueObject\Asset;
use App\Modules\TangibleAsset\Aircraft\Entity\Listing;
use App\Modules\TangibleAsset\Entity\AssetDetails;
use App\Modules\TangibleAsset\Event\ListingAvailabilityChanged;
use App\Modules\TangibleAsset\Event\ListingCreated;
use App\Modules\TangibleAsset\Service\AssetOwnerDetailsService;
use App\Modules\User\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class ListingLogAction implements EventSubscriberInterface
{
/**
* @var ActionsListService
*/
private $actionsListService;
/**
* @var AssetOwnerDetailsService
*/
private $assetOwnerDetailsService;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
public function __construct(
ActionsListService $actionsListService,
AssetOwnerDetailsService $assetOwnerDetailsService,
TokenStorageInterface $tokenStorage
)
{
$this->actionsListService = $actionsListService;
$this->assetOwnerDetailsService = $assetOwnerDetailsService;
$this->tokenStorage = $tokenStorage;
}
/**
* 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 [
ListingCreated::class => 'onListingCreated',
ListingAvailabilityChanged::class => 'onListingAvailabilityChanged',
];
}
public function onListingCreated(ListingCreated $event)
{
$listing = $event->getListing();
$listingTitle = $listing->getTitle();
$listingType = Asset::getTypeTitle($listing->getType());
$action = new NewListingCreatedAction($listingTitle, $listingType);
$this->setInitiatorData($action, $listing);
$this->actionsListService->logAction($action);
}
public function onListingAvailabilityChanged(ListingAvailabilityChanged $event)
{
$listing = $event->getListing();
$listingTitle = $listing->getTitle();
$listingType = Asset::getTypeTitle($listing->getType());
$availability = $listing->getActive();
$action = new ListingAvailabilityChangedAction($listingTitle, $listingType, $availability);
$this->setInitiatorData($action, $listing);
$this->actionsListService->logAction($action);
}
private function setInitiatorData(ActionInterface $action, AssetDetails $listing)
{
$assetOwner = $listing->getOwnerID();
$action->setInitiator($this->getInitiator());
if (!$assetOwner->isPrivateOwner()) {
$ownerDetails = $this->assetOwnerDetailsService->get($assetOwner->getId(), $assetOwner->getOwnerType());
if ($assetOwner->isCircleMember()) {
$action->setCircleMember($ownerDetails);
} elseif ($assetOwner->isPartner()) {
$action->setPartner($ownerDetails);
}
}
}
private function getInitiator()
{
$token = $this->tokenStorage->getToken();
if (!is_null($token)) {
return $token->getUser();
}
}
}