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