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

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