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 = $this->user instanceof UserInterface
  86. ? ListingActivityType::PUBLISH
  87. : ListingActivityType::APPROVED_BY_SYSTEM;
  88. $this->log($event->getlisting(), $this->user, $actionType);
  89. }
  90. public function listingDeleteEvent(ListingEvent $event): void
  91. {
  92. $actionType = ListingActivityType::DELETE;
  93. $this->log($event->getlisting(), $this->user, $actionType);
  94. }
  95. public function listingDeleteByUserEvent(ListingEvent $event): void
  96. {
  97. $actionType = ListingActivityType::DELETE_BY_USER;
  98. $this->log($event->getlisting(), $this->user, $actionType);
  99. }
  100. public function listingResetTitleEvent(ListingEvent $event): void
  101. {
  102. $actionType = ListingActivityType::RESET_TITLE;
  103. $this->log($event->getlisting(), $this->user, $actionType);
  104. }
  105. public function listingResetDescriptionEvent(ListingEvent $event): void
  106. {
  107. $actionType = ListingActivityType::RESET_DESCRIPTION;
  108. $this->log($event->getlisting(), $this->user, $actionType);
  109. }
  110. public function listingRemoveNumbersEvent(ListingEvent $event): void
  111. {
  112. $actionType = ListingActivityType::REMOVE_NUMBERS;
  113. $this->log($event->getlisting(), $this->user, $actionType);
  114. }
  115. public function listingRemoveVideoEvent(ListingEvent $event): void
  116. {
  117. $actionType = ListingActivityType::REMOVE_VIDEO;
  118. $this->log($event->getlisting(), $this->user, $actionType);
  119. }
  120. public function listingRemoveMapEvent(ListingEvent $event): void
  121. {
  122. $actionType = ListingActivityType::REMOVE_MAP;
  123. $this->log($event->getlisting(), $this->user, $actionType);
  124. }
  125. public function listingRemovePriceEvent(ListingEvent $event): void
  126. {
  127. $actionType = ListingActivityType::CLEAR_PRICE;
  128. $this->log($event->getlisting(), $this->user, $actionType);
  129. }
  130. public function listingFeaturedEvent(ListingEvent $event): void
  131. {
  132. $actionType = ListingActivityType::FEATURED;
  133. $this->log($event->getlisting(), $this->user, $actionType);
  134. }
  135. public function listingSuperFeaturedEvent(ListingEvent $event): void
  136. {
  137. $actionType = ListingActivityType::SUPER_FEATURED;
  138. $this->log($event->getlisting(), $this->user, $actionType);
  139. }
  140. public function listingPremiumFeaturedEvent(ListingEvent $event): void
  141. {
  142. $actionType = ListingActivityType::PREMIUM;
  143. $this->log($event->getlisting(), $this->user, $actionType);
  144. }
  145. public function listingSponsoredFeaturedEvent(ListingEvent $event): void
  146. {
  147. $actionType = ListingActivityType::SPONSORED;
  148. $this->log($event->getlisting(), $this->user, $actionType);
  149. }
  150. public function listingSpotlightFeaturedEvent(ListingEvent $event): void
  151. {
  152. $actionType = ListingActivityType::SPOTLIGHT;
  153. $this->log($event->getlisting(), $this->user, $actionType);
  154. }
  155. public function listingUnFeaturedEvent(ListingEvent $event): void
  156. {
  157. $actionType = ListingActivityType::UNFEATURED;
  158. $this->log($event->getlisting(), $this->user, $actionType);
  159. }
  160. public function listingExpiredEvent(ListingEvent $event): void
  161. {
  162. $actionType = ListingActivityType::EXPIRED;
  163. $this->log($event->getlisting(), $this->user, $actionType);
  164. }
  165. public function listingRejectedEvent(ListingEvent $event): void
  166. {
  167. $actionType = ListingActivityType::REJECTED;
  168. $this->log($event->getlisting(), $this->user, $actionType, $event->getRejectionReasons());
  169. }
  170. public function listingPendingPhotoEvent(ListingEvent $event): void
  171. {
  172. $actionType = ListingActivityType::PENDING_PHOTOS;
  173. $this->log($event->getlisting(), $this->user, $actionType);
  174. }
  175. public function listingPendingPaymentEvent(ListingEvent $event): void
  176. {
  177. $actionType = ListingActivityType::PENDING_PAYMENT;
  178. if (ListingStatus::PENDING_PAYMENT != $event->getListing()->getStatus()) {
  179. $this->log($event->getlisting(), $this->user, $actionType);
  180. }
  181. }
  182. public function listingPublishFreeEvent(ListingEvent $event): void
  183. {
  184. $actionType = ListingActivityType::FREE_PUBLISH;
  185. $this->log($event->getlisting(), $this->user, $actionType);
  186. }
  187. public function listingPublishWithOutPhotoEvent(ListingEvent $event): void
  188. {
  189. $actionType = $this->user instanceof UserInterface
  190. ? ListingActivityType::PUBLISH_WITHOUT_PHOTOS
  191. : ListingActivityType::PUBLISH_WITHOUT_PHOTOS_BY_SYSTEM;
  192. $this->log($event->getlisting(), $this->user, $actionType);
  193. }
  194. public function listingAutoBumpUpStartedEvent(BumpListingEvent $event): void
  195. {
  196. $this->log(
  197. $event->getListing(),
  198. $this->user,
  199. ListingActivityType::AUTO_BUMP_UP_STARTED
  200. );
  201. }
  202. public function listingAutoBumpUpRevertedEvent(BumpListingEvent $event): void
  203. {
  204. $this->log(
  205. $event->getListing(),
  206. $this->user,
  207. ListingActivityType::AUTO_BUMP_UP_REVERTED
  208. );
  209. }
  210. public function listingAutoBumpUpStepEvent(BumpListingEvent $event): void
  211. {
  212. $this->log(
  213. $event->getListing(),
  214. $event->getBumpUpOwner(),
  215. ListingActivityType::AUTO_BUMP_UP_STEP
  216. );
  217. }
  218. public function listingAutoBumpUpExpiredEvent(BumpListingEvent $event): void
  219. {
  220. $this->log(
  221. $event->getListing(),
  222. $event->getBumpUpOwner(),
  223. ListingActivityType::AUTO_BUMP_UP_EXPIRED
  224. );
  225. }
  226. public function listingSoldByOwnerFeaturedEvent(ListingEvent $event): void
  227. {
  228. $this->log($event->getlisting(), $this->user, ListingActivityType::SOLD_BY_ADMIN);
  229. }
  230. public function listingSoldByOwnerSponsoredEvent(ListingEvent $event): void
  231. {
  232. $this->log($event->getlisting(), $this->user, ListingActivityType::SOLD_BY_ADMIN);
  233. }
  234. public function firstListingForFreeEvent(ListingEvent $event): void
  235. {
  236. $this->log($event->getlisting(), $this->user, ListingActivityType::FIRST_LISTING_FOR_FREE);
  237. }
  238. public static function getSubscribedEvents(): array
  239. {
  240. return [
  241. 'aqarmap.listing.publish' => ['listingPublishEvent'],
  242. 'aqarmap.listing.scrapped.publish' => ['listingPublishEvent'],
  243. 'aqarmap.listing.delete' => ['listingDeleteEvent'],
  244. 'aqarmap.listing.delete_by_user' => ['listingDeleteByUserEvent'],
  245. 'aqarmap.listing.reset_title' => ['listingResetTitleEvent'],
  246. 'aqarmap.listing.reset_description' => ['listingResetDescriptionEvent'],
  247. 'aqarmap.listing.remove_numbers' => ['listingRemoveNumbersEvent'],
  248. 'aqarmap.listing.clear_video' => ['listingRemoveVideoEvent'],
  249. 'aqarmap.listing.clear_map' => ['listingRemoveMapEvent'],
  250. 'aqarmap.listing.clear_price' => ['listingRemovePriceEvent'],
  251. 'aqarmap.listing.featured' => ['listingFeaturedEvent'],
  252. 'aqarmap.listing.super_featured' => ['listingSuperFeaturedEvent'],
  253. 'aqarmap.listing.premium' => ['listingPremiumFeaturedEvent'],
  254. 'aqarmap.listing.sponsored' => ['listingSponsoredFeaturedEvent'],
  255. 'aqarmap.listing.spotlight' => ['listingSpotlightFeaturedEvent'],
  256. 'aqarmap.listing.bump_up_started' => ['listingAutoBumpUpStartedEvent'],
  257. 'aqarmap.listing.bump_up_reverted' => ['listingAutoBumpUpRevertedEvent'],
  258. 'aqarmap.listing.bump_up_step' => ['listingAutoBumpUpStepEvent'],
  259. 'aqarmap.listing.bump_up_expired' => ['listingAutoBumpUpExpiredEvent'],
  260. 'aqarmap.listing.unfeatured' => ['listingUnFeaturedEvent'],
  261. 'aqarmap.listing.expired' => ['listingExpiredEvent'],
  262. 'aqarmap.listing.rejected' => ['listingRejectedEvent'],
  263. 'aqarmap.listing.pending_photos' => ['listingPendingPhotoEvent'],
  264. 'aqarmap.listing.pending_payment' => ['listingPendingPaymentEvent'],
  265. 'aqarmap.listing.free_publish' => ['listingPublishFreeEvent'],
  266. 'aqarmap.listing.publish_without_photos' => ['listingPublishWithOutPhotoEvent'],
  267. 'aqarmap.listing.created' => ['listingCreatedEvent'],
  268. 'aqarmap.listing.pending_approval' => ['listingPendingApprovalEvent'],
  269. 'aqarmap.listing.sold_by_owner' => ['listingSoldByOwnerFeaturedEvent'],
  270. 'aqarmap.listing.sold_by_owner_sponsored' => ['listingSoldByOwnerSponsoredEvent'],
  271. 'aqarmap.listing.first_listing_for_free' => ['firstListingForFreeEvent'],
  272. ];
  273. }
  274. }