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

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