src/Aqarmap/Bundle/ListingBundle/Service/InteractionService.php line 138

  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\Service;
  3. use Aqarmap\Bundle\ListingBundle\Constant\Campaign;
  4. use Aqarmap\Bundle\ListingBundle\Constant\InteractionTypes;
  5. use Aqarmap\Bundle\ListingBundle\Entity\Interaction;
  6. use Aqarmap\Bundle\ListingBundle\Entity\Listing;
  7. use Aqarmap\Bundle\ListingBundle\Repository\InteractionRepository;
  8. use Aqarmap\Bundle\MainBundle\Contract\ProducerFactoryInterface;
  9. use Aqarmap\Bundle\UserBundle\Entity\User;
  10. use Aqarmap\Bundle\UserBundle\Services\UserInterestManager;
  11. use Aqarmap\Bundle\UserBundle\Services\UserManager;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\HttpFoundation\Cookie;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Contracts\Service\Attribute\Required;
  19. class InteractionService
  20. {
  21. /** @var RequestStack */
  22. private $request;
  23. /** @var ListingManager */
  24. private $listingManager;
  25. /**
  26. * @var EventDispatcherInterface
  27. */
  28. protected $dispatcher;
  29. /**
  30. * @var Storage
  31. */
  32. protected $tokenStorage;
  33. /**
  34. * @var ProducerFactoryInterface
  35. */
  36. protected $buyerAlertProducer;
  37. /**
  38. * @var UserInterestManager
  39. */
  40. protected $interestManager;
  41. /** @var InteractionRepository */
  42. protected $interactionRepository;
  43. /** @var ListingContactRateService */
  44. protected $listingContactRateService;
  45. public function __construct(
  46. private readonly EntityManagerInterface $em,
  47. RequestStack $request,
  48. EventDispatcherInterface $dispatcher,
  49. ProducerFactoryInterface $pokeProducerFactory,
  50. UserInterestManager $interestManager,
  51. protected UserManager $userManager,
  52. InteractionRepository $interactionRepository,
  53. ListingContactRateService $listingContactRateService,
  54. ) {
  55. $this->request = $request->getCurrentRequest();
  56. $this->dispatcher = $dispatcher;
  57. $this->interestManager = $interestManager;
  58. $this->interactionRepository = $interactionRepository;
  59. $this->listingContactRateService = $listingContactRateService;
  60. }
  61. /**
  62. * @param int $type
  63. * @param User $user
  64. * @param string|null $source
  65. * @param bool $flush
  66. */
  67. public function addInteraction($type, Listing $listing, $user = null, $source = null, $flush = true, $campaign = null): ?Interaction
  68. {
  69. $isValid = true;
  70. $interaction = new Interaction();
  71. $interaction
  72. ->setUser($user)
  73. ->setListing($listing)
  74. ->setType($type)
  75. ->setUserIp($this->request->getClientIp())
  76. ->setSource($source)
  77. ->setCampaign($campaign)
  78. ;
  79. // Check if the campaign exists in the cookie add it to listing
  80. $cookies = $this->request->cookies;
  81. if ($cookies->has('campaign')) {
  82. $interaction->setCampaign($cookies->get('campaign'));
  83. }
  84. if (\in_array($type, [InteractionTypes::LISTING_VIEWS])) {
  85. $isValid = $this->validateInteractionsDuplications($interaction);
  86. }
  87. if (true == $isValid) {
  88. $this->em->persist($interaction);
  89. if ($flush) {
  90. $this->em->flush([$interaction]);
  91. }
  92. $this->listingManager->incrementViewsCounters($listing);
  93. return $interaction;
  94. }
  95. return null;
  96. }
  97. /**
  98. * @return bool
  99. */
  100. private function validateInteractionsDuplications(Interaction $interaction)
  101. {
  102. $duplicates = $this->interactionRepository->checkDuplicateInteraction($interaction);
  103. return $duplicates ? false : true;
  104. }
  105. public function increaseViews(Listing $listing, ?UserInterface $user = null): void
  106. {
  107. if (
  108. $user instanceof User
  109. && $listing->getUser()->getId() === $user->getId()
  110. ) {
  111. return;
  112. }
  113. $response = new Response();
  114. $views = [];
  115. if ($this->request->cookies->has('viewed_listings')) {
  116. $views = unserialize($this->request->cookies->get('viewed_listings'));
  117. if (\count($views) > 100) {
  118. array_pop($views);
  119. }
  120. }
  121. if (!\in_array($listing->getId(), $views)) {
  122. $views[] = $listing->getId();
  123. $response->headers->setCookie(Cookie::create('viewed_listings', serialize($views), time() + (86400 * 7)));
  124. $response->sendHeaders();
  125. $user = $user instanceof User ? $user : null;
  126. $this->addInteraction(InteractionTypes::LISTING_VIEWS, $listing, $user, 'interaction-service');
  127. }
  128. }
  129. #[Required]
  130. public function setListingManager(ListingManager $listingManager): void
  131. {
  132. $this->listingManager = $listingManager;
  133. }
  134. }