<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use Aqarmap\Bundle\ListingBundle\Constant\ListingActivityType;
use Aqarmap\Bundle\ListingBundle\Constant\ListingStatus;
use Aqarmap\Bundle\ListingBundle\Document\ListingActivityLog;
use Aqarmap\Bundle\ListingBundle\Entity\Listing;
use Aqarmap\Bundle\ListingBundle\Event\BumpListingEvent;
use Aqarmap\Bundle\ListingBundle\Event\ListingEvent;
use Aqarmap\Bundle\ListingBundle\Event\ListingEventInterface;
use Aqarmap\Bundle\ListingBundle\Service\ListingActivityLogService;
use Aqarmap\Bundle\UserBundle\Entity\User;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Listing Activity Log listener.
*/
class ListingActivityLogListener implements EventSubscriberInterface
{
/** @var UserInterface */
protected $user;
/** @var TokenStorageInterface */
private $tokenStorage;
/** @var ListingActivityLogService */
protected $listingActivity;
/**
* @var Serializer
*/
protected $serializer;
/**
* @var EventDispatcherInterface
*/
protected $dispatcher;
public function __construct(EventDispatcherInterface $dispatcher, TokenStorageInterface $tokenStorage, ListingActivityLogService $listingActivity, SerializerInterface $serializer)
{
$this->dispatcher = $dispatcher;
$this->tokenStorage = $tokenStorage;
$this->user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
$this->listingActivity = $listingActivity;
$this->serializer = $serializer;
}
protected function serialize($object)
{
return $this->serializer->serialize($object, 'json', SerializationContext::create()->setGroups(['Default', 'Activity']));
}
/**
* @param Listing $listing
* @param UserInterface $user
* @param array $rejectionReasons
*/
protected function log($listing, $user, $actionType, $rejectionReasons = []): void
{
$user = $user instanceof User && $user->getId() ? $user : $listing->getUser();
if ($user && $listing) {
$listingActivityLog = new ListingActivityLog();
$listingActivityLog->setUserId($user->getId());
$listingActivityLog->setUser($this->serialize($user));
$listingActivityLog->setListingId($listing->getId());
$listingActivityLog->setListing($this->serialize($listing));
$listingActivityLog->setRejectionReasons($rejectionReasons);
$listingActivityLog->setActionType($actionType);
$listingActivityLog->setTeamId($user->getTeam() ? $user->getTeam()->getId() : null);
$listingActivityLog->setTeam($user->getTeam() ? $this->serialize($user->getTeam()) : null);
// Workaround to avoid Fire created event after any other events
$listingActivityLog->setCreatedAt(new \DateTime());
if (ListingActivityType::CREATED == $actionType) {
$listingActivityLog->setCreatedAt(new \DateTime('now -2 seconds'));
}
$this->listingActivity->execute($listingActivityLog);
}
}
public function listingCreatedEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::CREATED;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingPendingApprovalEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::PENDING_APPROVAL;
$this->log($event->getlisting(), $event->getListing()->getUser(), $actionType);
}
public function listingPublishEvent(ListingEventInterface $event): void
{
$actionType = ListingActivityType::PUBLISH;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingDeleteEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::DELETE;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingDeleteByUserEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::DELETE_BY_USER;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingResetTitleEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::RESET_TITLE;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingRemoveNumbersEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::REMOVE_NUMBERS;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingRemoveVideoEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::REMOVE_VIDEO;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingRemoveMapEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::REMOVE_MAP;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingRemovePriceEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::CLEAR_PRICE;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingFeaturedEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::FEATURED;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingSuperFeaturedEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::SUPER_FEATURED;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingPremiumFeaturedEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::PREMIUM;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingSponsoredFeaturedEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::SPONSORED;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingSpotlightFeaturedEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::SPOTLIGHT;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingUnFeaturedEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::UNFEATURED;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingExpiredEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::EXPIRED;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingRejectedEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::REJECTED;
$this->log($event->getlisting(), $this->user, $actionType, $event->getRejectionReasons());
}
public function listingPendingPhotoEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::PENDING_PHOTOS;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingPendingPaymentEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::PENDING_PAYMENT;
if (ListingStatus::PENDING_PAYMENT != $event->getListing()->getStatus()) {
$this->log($event->getlisting(), $this->user, $actionType);
}
}
public function listingPublishFreeEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::FREE_PUBLISH;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingPublishWithOutPhotoEvent(ListingEvent $event): void
{
$actionType = ListingActivityType::PUBLISH_WITHOUT_PHOTOS;
$this->log($event->getlisting(), $this->user, $actionType);
}
public function listingAutoBumpUpStartedEvent(BumpListingEvent $event): void
{
$this->log(
$event->getListing(),
$this->user,
ListingActivityType::AUTO_BUMP_UP_STARTED
);
}
public function listingAutoBumpUpRevertedEvent(BumpListingEvent $event): void
{
$this->log(
$event->getListing(),
$this->user,
ListingActivityType::AUTO_BUMP_UP_REVERTED
);
}
public function listingAutoBumpUpStepEvent(BumpListingEvent $event): void
{
$this->log(
$event->getListing(),
$event->getBumpUpOwner(),
ListingActivityType::AUTO_BUMP_UP_STEP
);
}
public function listingAutoBumpUpExpiredEvent(BumpListingEvent $event): void
{
$this->log(
$event->getListing(),
$event->getBumpUpOwner(),
ListingActivityType::AUTO_BUMP_UP_EXPIRED
);
}
public function listingSoldByOwnerFeaturedEvent(ListingEvent $event): void
{
$this->log($event->getlisting(), $this->user, ListingActivityType::SOLD_BY_ADMIN);
}
public function listingSoldByOwnerSponsoredEvent(ListingEvent $event): void
{
$this->log($event->getlisting(), $this->user, ListingActivityType::SOLD_BY_ADMIN);
}
public function firstListingForFreeEvent(ListingEvent $event): void
{
$this->log($event->getlisting(), $this->user, ListingActivityType::FIRST_LISTING_FOR_FREE);
}
public static function getSubscribedEvents()
{
return [
'aqarmap.listing.publish' => ['listingPublishEvent'],
'aqarmap.listing.scrapped.publish' => ['listingPublishEvent'],
'aqarmap.listing.delete' => ['listingDeleteEvent'],
'aqarmap.listing.delete_by_user' => ['listingDeleteByUserEvent'],
'aqarmap.listing.reset_title' => ['listingResetTitleEvent'],
'aqarmap.listing.remove_numbers' => ['listingRemoveNumbersEvent'],
'aqarmap.listing.clear_video' => ['listingRemoveVideoEvent'],
'aqarmap.listing.clear_map' => ['listingRemoveMapEvent'],
'aqarmap.listing.clear_price' => ['listingRemovePriceEvent'],
'aqarmap.listing.featured' => ['listingFeaturedEvent'],
'aqarmap.listing.super_featured' => ['listingSuperFeaturedEvent'],
'aqarmap.listing.premium' => ['listingPremiumFeaturedEvent'],
'aqarmap.listing.sponsored' => ['listingSponsoredFeaturedEvent'],
'aqarmap.listing.spotlight' => ['listingSpotlightFeaturedEvent'],
'aqarmap.listing.bump_up_started' => ['listingAutoBumpUpStartedEvent'],
'aqarmap.listing.bump_up_reverted' => ['listingAutoBumpUpRevertedEvent'],
'aqarmap.listing.bump_up_step' => ['listingAutoBumpUpStepEvent'],
'aqarmap.listing.bump_up_expired' => ['listingAutoBumpUpExpiredEvent'],
'aqarmap.listing.unfeatured' => ['listingUnFeaturedEvent'],
'aqarmap.listing.expired' => ['listingExpiredEvent'],
'aqarmap.listing.rejected' => ['listingRejectedEvent'],
'aqarmap.listing.pending_photos' => ['listingPendingPhotoEvent'],
'aqarmap.listing.pending_payment' => ['listingPendingPaymentEvent'],
'aqarmap.listing.free_publish' => ['listingPublishFreeEvent'],
'aqarmap.listing.publish_without_photos' => ['listingPublishWithOutPhotoEvent'],
'aqarmap.listing.created' => ['listingCreatedEvent'],
'aqarmap.listing.pending_approval' => ['listingPendingApprovalEvent'],
'aqarmap.listing.sold_by_owner' => ['listingSoldByOwnerFeaturedEvent'],
'aqarmap.listing.sold_by_owner_sponsored' => ['listingSoldByOwnerSponsoredEvent'],
'aqarmap.listing.first_listing_for_free' => ['firstListingForFreeEvent'],
];
}
}