src/Aqarmap/Bundle/ListingBundle/EventListener/ListingActivityLogListener.php line 135

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\ListingBundle\Constant\ListingActivityType;
  4. use Aqarmap\Bundle\ListingBundle\Constant\ListingStatus;
  5. use Aqarmap\Bundle\ListingBundle\Document\ListingActivityLog;
  6. use Aqarmap\Bundle\ListingBundle\Entity\Listing;
  7. use Aqarmap\Bundle\ListingBundle\Event\BumpListingEvent;
  8. use Aqarmap\Bundle\ListingBundle\Event\ListingEvent;
  9. use Aqarmap\Bundle\ListingBundle\Event\ListingEventInterface;
  10. use Aqarmap\Bundle\ListingBundle\Service\ListingActivityLogService;
  11. use Aqarmap\Bundle\UserBundle\Entity\User;
  12. use JMS\Serializer\SerializationContext;
  13. use JMS\Serializer\Serializer;
  14. use JMS\Serializer\SerializerInterface;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Component\Security\Core\User\UserInterface;
  19. /**
  20.  * Listing Activity Log listener.
  21.  */
  22. class ListingActivityLogListener implements EventSubscriberInterface
  23. {
  24.     /** @var UserInterface */
  25.     protected $user;
  26.     /** @var TokenStorageInterface */
  27.     private $tokenStorage;
  28.     /** @var ListingActivityLogService */
  29.     protected $listingActivity;
  30.     /**
  31.      * @var Serializer
  32.      */
  33.     protected $serializer;
  34.     /**
  35.      * @var EventDispatcherInterface
  36.      */
  37.     protected $dispatcher;
  38.     public function __construct(EventDispatcherInterface $dispatcherTokenStorageInterface $tokenStorageListingActivityLogService $listingActivitySerializerInterface $serializer)
  39.     {
  40.         $this->dispatcher $dispatcher;
  41.         $this->tokenStorage $tokenStorage;
  42.         $this->user $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
  43.         $this->listingActivity $listingActivity;
  44.         $this->serializer $serializer;
  45.     }
  46.     protected function serialize($object)
  47.     {
  48.         return $this->serializer->serialize($object'json'SerializationContext::create()->setGroups(['Default''Activity']));
  49.     }
  50.     /**
  51.      * @param Listing $listing
  52.      * @param UserInterface $user
  53.      * @param array $rejectionReasons
  54.      */
  55.     protected function log($listing$user$actionType$rejectionReasons = []): void
  56.     {
  57.         $user $user instanceof User && $user->getId() ? $user $listing->getUser();
  58.         if ($user && $listing) {
  59.             $listingActivityLog = new ListingActivityLog();
  60.             $listingActivityLog->setUserId($user->getId());
  61.             $listingActivityLog->setUser($this->serialize($user));
  62.             $listingActivityLog->setListingId($listing->getId());
  63.             $listingActivityLog->setListing($this->serialize($listing));
  64.             $listingActivityLog->setRejectionReasons($rejectionReasons);
  65.             $listingActivityLog->setActionType($actionType);
  66.             $listingActivityLog->setTeamId($user->getTeam() ? $user->getTeam()->getId() : null);
  67.             $listingActivityLog->setTeam($user->getTeam() ? $this->serialize($user->getTeam()) : null);
  68.             // Workaround to avoid Fire created event after any other events
  69.             $listingActivityLog->setCreatedAt(new \DateTime());
  70.             if (ListingActivityType::CREATED == $actionType) {
  71.                 $listingActivityLog->setCreatedAt(new \DateTime('now -2 seconds'));
  72.             }
  73.             $this->listingActivity->execute($listingActivityLog);
  74.         }
  75.     }
  76.     public function listingCreatedEvent(ListingEvent $event): void
  77.     {
  78.         $actionType ListingActivityType::CREATED;
  79.         $this->log($event->getlisting(), $this->user$actionType);
  80.     }
  81.     public function listingPendingApprovalEvent(ListingEvent $event): void
  82.     {
  83.         $actionType ListingActivityType::PENDING_APPROVAL;
  84.         $this->log($event->getlisting(), $event->getListing()->getUser(), $actionType);
  85.     }
  86.     public function listingPublishEvent(ListingEventInterface $event): void
  87.     {
  88.         $actionType ListingActivityType::PUBLISH;
  89.         $this->log($event->getlisting(), $this->user$actionType);
  90.     }
  91.     public function listingDeleteEvent(ListingEvent $event): void
  92.     {
  93.         $actionType ListingActivityType::DELETE;
  94.         $this->log($event->getlisting(), $this->user$actionType);
  95.     }
  96.     public function listingDeleteByUserEvent(ListingEvent $event): void
  97.     {
  98.         $actionType ListingActivityType::DELETE_BY_USER;
  99.         $this->log($event->getlisting(), $this->user$actionType);
  100.     }
  101.     public function listingResetTitleEvent(ListingEvent $event): void
  102.     {
  103.         $actionType ListingActivityType::RESET_TITLE;
  104.         $this->log($event->getlisting(), $this->user$actionType);
  105.     }
  106.     public function listingRemoveNumbersEvent(ListingEvent $event): void
  107.     {
  108.         $actionType ListingActivityType::REMOVE_NUMBERS;
  109.         $this->log($event->getlisting(), $this->user$actionType);
  110.     }
  111.     public function listingRemoveVideoEvent(ListingEvent $event): void
  112.     {
  113.         $actionType ListingActivityType::REMOVE_VIDEO;
  114.         $this->log($event->getlisting(), $this->user$actionType);
  115.     }
  116.     public function listingRemoveMapEvent(ListingEvent $event): void
  117.     {
  118.         $actionType ListingActivityType::REMOVE_MAP;
  119.         $this->log($event->getlisting(), $this->user$actionType);
  120.     }
  121.     public function listingRemovePriceEvent(ListingEvent $event): void
  122.     {
  123.         $actionType ListingActivityType::CLEAR_PRICE;
  124.         $this->log($event->getlisting(), $this->user$actionType);
  125.     }
  126.     public function listingFeaturedEvent(ListingEvent $event): void
  127.     {
  128.         $actionType ListingActivityType::FEATURED;
  129.         $this->log($event->getlisting(), $this->user$actionType);
  130.     }
  131.     public function listingSuperFeaturedEvent(ListingEvent $event): void
  132.     {
  133.         $actionType ListingActivityType::SUPER_FEATURED;
  134.         $this->log($event->getlisting(), $this->user$actionType);
  135.     }
  136.     public function listingPremiumFeaturedEvent(ListingEvent $event): void
  137.     {
  138.         $actionType ListingActivityType::PREMIUM;
  139.         $this->log($event->getlisting(), $this->user$actionType);
  140.     }
  141.     public function listingSponsoredFeaturedEvent(ListingEvent $event): void
  142.     {
  143.         $actionType ListingActivityType::SPONSORED;
  144.         $this->log($event->getlisting(), $this->user$actionType);
  145.     }
  146.     public function listingSpotlightFeaturedEvent(ListingEvent $event): void
  147.     {
  148.         $actionType ListingActivityType::SPOTLIGHT;
  149.         $this->log($event->getlisting(), $this->user$actionType);
  150.     }
  151.     public function listingUnFeaturedEvent(ListingEvent $event): void
  152.     {
  153.         $actionType ListingActivityType::UNFEATURED;
  154.         $this->log($event->getlisting(), $this->user$actionType);
  155.     }
  156.     public function listingExpiredEvent(ListingEvent $event): void
  157.     {
  158.         $actionType ListingActivityType::EXPIRED;
  159.         $this->log($event->getlisting(), $this->user$actionType);
  160.     }
  161.     public function listingRejectedEvent(ListingEvent $event): void
  162.     {
  163.         $actionType ListingActivityType::REJECTED;
  164.         $this->log($event->getlisting(), $this->user$actionType$event->getRejectionReasons());
  165.     }
  166.     public function listingPendingPhotoEvent(ListingEvent $event): void
  167.     {
  168.         $actionType ListingActivityType::PENDING_PHOTOS;
  169.         $this->log($event->getlisting(), $this->user$actionType);
  170.     }
  171.     public function listingPendingPaymentEvent(ListingEvent $event): void
  172.     {
  173.         $actionType ListingActivityType::PENDING_PAYMENT;
  174.         if (ListingStatus::PENDING_PAYMENT != $event->getListing()->getStatus()) {
  175.             $this->log($event->getlisting(), $this->user$actionType);
  176.         }
  177.     }
  178.     public function listingPublishFreeEvent(ListingEvent $event): void
  179.     {
  180.         $actionType ListingActivityType::FREE_PUBLISH;
  181.         $this->log($event->getlisting(), $this->user$actionType);
  182.     }
  183.     public function listingPublishWithOutPhotoEvent(ListingEvent $event): void
  184.     {
  185.         $actionType ListingActivityType::PUBLISH_WITHOUT_PHOTOS;
  186.         $this->log($event->getlisting(), $this->user$actionType);
  187.     }
  188.     public function listingAutoBumpUpStartedEvent(BumpListingEvent $event): void
  189.     {
  190.         $this->log(
  191.             $event->getListing(),
  192.             $this->user,
  193.             ListingActivityType::AUTO_BUMP_UP_STARTED
  194.         );
  195.     }
  196.     public function listingAutoBumpUpRevertedEvent(BumpListingEvent $event): void
  197.     {
  198.         $this->log(
  199.             $event->getListing(),
  200.             $this->user,
  201.             ListingActivityType::AUTO_BUMP_UP_REVERTED
  202.         );
  203.     }
  204.     public function listingAutoBumpUpStepEvent(BumpListingEvent $event): void
  205.     {
  206.         $this->log(
  207.             $event->getListing(),
  208.             $event->getBumpUpOwner(),
  209.             ListingActivityType::AUTO_BUMP_UP_STEP
  210.         );
  211.     }
  212.     public function listingAutoBumpUpExpiredEvent(BumpListingEvent $event): void
  213.     {
  214.         $this->log(
  215.             $event->getListing(),
  216.             $event->getBumpUpOwner(),
  217.             ListingActivityType::AUTO_BUMP_UP_EXPIRED
  218.         );
  219.     }
  220.     public function listingSoldByOwnerFeaturedEvent(ListingEvent $event): void
  221.     {
  222.         $this->log($event->getlisting(), $this->userListingActivityType::SOLD_BY_ADMIN);
  223.     }
  224.     public function listingSoldByOwnerSponsoredEvent(ListingEvent $event): void
  225.     {
  226.         $this->log($event->getlisting(), $this->userListingActivityType::SOLD_BY_ADMIN);
  227.     }
  228.     public function firstListingForFreeEvent(ListingEvent $event): void
  229.     {
  230.         $this->log($event->getlisting(), $this->userListingActivityType::FIRST_LISTING_FOR_FREE);
  231.     }
  232.     public static function getSubscribedEvents()
  233.     {
  234.         return [
  235.             'aqarmap.listing.publish' => ['listingPublishEvent'],
  236.             'aqarmap.listing.scrapped.publish' => ['listingPublishEvent'],
  237.             'aqarmap.listing.delete' => ['listingDeleteEvent'],
  238.             'aqarmap.listing.delete_by_user' => ['listingDeleteByUserEvent'],
  239.             'aqarmap.listing.reset_title' => ['listingResetTitleEvent'],
  240.             'aqarmap.listing.remove_numbers' => ['listingRemoveNumbersEvent'],
  241.             'aqarmap.listing.clear_video' => ['listingRemoveVideoEvent'],
  242.             'aqarmap.listing.clear_map' => ['listingRemoveMapEvent'],
  243.             'aqarmap.listing.clear_price' => ['listingRemovePriceEvent'],
  244.             'aqarmap.listing.featured' => ['listingFeaturedEvent'],
  245.             'aqarmap.listing.super_featured' => ['listingSuperFeaturedEvent'],
  246.             'aqarmap.listing.premium' => ['listingPremiumFeaturedEvent'],
  247.             'aqarmap.listing.sponsored' => ['listingSponsoredFeaturedEvent'],
  248.             'aqarmap.listing.spotlight' => ['listingSpotlightFeaturedEvent'],
  249.             'aqarmap.listing.bump_up_started' => ['listingAutoBumpUpStartedEvent'],
  250.             'aqarmap.listing.bump_up_reverted' => ['listingAutoBumpUpRevertedEvent'],
  251.             'aqarmap.listing.bump_up_step' => ['listingAutoBumpUpStepEvent'],
  252.             'aqarmap.listing.bump_up_expired' => ['listingAutoBumpUpExpiredEvent'],
  253.             'aqarmap.listing.unfeatured' => ['listingUnFeaturedEvent'],
  254.             'aqarmap.listing.expired' => ['listingExpiredEvent'],
  255.             'aqarmap.listing.rejected' => ['listingRejectedEvent'],
  256.             'aqarmap.listing.pending_photos' => ['listingPendingPhotoEvent'],
  257.             'aqarmap.listing.pending_payment' => ['listingPendingPaymentEvent'],
  258.             'aqarmap.listing.free_publish' => ['listingPublishFreeEvent'],
  259.             'aqarmap.listing.publish_without_photos' => ['listingPublishWithOutPhotoEvent'],
  260.             'aqarmap.listing.created' => ['listingCreatedEvent'],
  261.             'aqarmap.listing.pending_approval' => ['listingPendingApprovalEvent'],
  262.             'aqarmap.listing.sold_by_owner' => ['listingSoldByOwnerFeaturedEvent'],
  263.             'aqarmap.listing.sold_by_owner_sponsored' => ['listingSoldByOwnerSponsoredEvent'],
  264.             'aqarmap.listing.first_listing_for_free' => ['firstListingForFreeEvent'],
  265.         ];
  266.     }
  267. }