src/Aqarmap/Bundle/CustomerProfilingBundle/EventListener/CustomerProfilingListener.php line 49

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\CustomerProfilingBundle\EventListener;
  3. use Aqarmap\Bundle\CustomerProfilingBundle\Entity\CustomerProfilingQuestionQueue;
  4. use Aqarmap\Bundle\CustomerProfilingBundle\Services\CustomerProfilingService;
  5. use Aqarmap\Bundle\MainBundle\Service\CacheManager;
  6. use Aqarmap\Bundle\UserBundle\Entity\User;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use FOS\UserBundle\Event\UserEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. /**
  12.  * Class CustomerProfilingListener.
  13.  */
  14. class CustomerProfilingListener implements EventSubscriberInterface
  15. {
  16.     public const CACHE_KEY_PREFIX 'customer_profiling';
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     protected $em;
  21.     protected $twig;
  22.     protected $translator;
  23.     /** @var CustomerProfilingService */
  24.     protected $profilingService;
  25.     public function __construct(
  26.         EntityManagerInterface $em,
  27.         \Twig_Environment $twig_Environment,
  28.         TranslatorInterface $translator,
  29.         CustomerProfilingService $profilingService,
  30.         CacheManager $cacheManager
  31.     ) {
  32.         $this->em $em;
  33.         $this->twig $twig_Environment;
  34.         $this->translator $translator;
  35.         $this->profilingService $profilingService;
  36.         $this->cacheManager $cacheManager;
  37.     }
  38.     /**
  39.      * @throws \Exception
  40.      */
  41.     public function onSearch(UserEvent $event): void
  42.     {
  43.         $user $event->getUser();
  44.         /** @var CustomerProfilingQuestionQueue $currentSet */
  45.         $currentSet $this->em
  46.             ->getRepository('AqarmapCustomerProfilingBundle:CustomerProfilingQuestionQueue')
  47.             ->getCurrentCustomerSet($user);
  48.         if ($currentSet && $this->canDisplaySet($currentSet$user)) {
  49.             $questions $this->profilingService->processQueueSet($currentSet);
  50.             $this->twig->addGlobal('customerProfilingSet'$questions);
  51.             $this->twig->addGlobal('currentSurveySet'$currentSet);
  52.             $currentDisplayDate = new \DateTime();
  53.             $currentSet->setLastDisplayTime($currentDisplayDate);
  54.             $this->em->persist($currentSet);
  55.             $this->em->flush($currentSet);
  56.             if (== $currentSet->getSkipNumber()) {
  57.                 $this->cacheManager->set($this->getCacheKey($user->getId()), serialize([
  58.                     'set_id' => $currentSet->getId(),
  59.                     'last_display_time' => $currentDisplayDate->getTimestamp(),
  60.                 ]));
  61.             }
  62.         }
  63.     }
  64.     /**
  65.      * @return bool
  66.      */
  67.     private function canDisplaySet(CustomerProfilingQuestionQueue $currentSetUser $user)
  68.     {
  69.         $oneWeek = new \DateTime('-1 week');
  70.         $threeDays = new \DateTime('-3 days');
  71.         $cacheKey $this->getCacheKey($user->getId());
  72.         if (null == $currentSet->getLastDisplayTime() && == $currentSet->getSkipNumber() && !$this->cacheManager->has($cacheKey)) {
  73.             return true;
  74.         } elseif ($this->cacheManager->has($cacheKey)) {
  75.             $userSetData $this->cacheManager->get($cacheKey);
  76.             $userSetData unserialize($userSetData);
  77.             if ($userSetData['set_id'] == $currentSet->getId()) {
  78.                 if (== $currentSet->getSkipNumber()) {
  79.                     return true;
  80.                 } elseif ($currentSet->getSkipNumber() > 0
  81.                     && $userSetData['last_display_time'] <= $oneWeek->getTimestamp()
  82.                 ) {
  83.                     return true;
  84.                 }
  85.             } elseif ($userSetData['set_id'] != $currentSet->getId()
  86.             ) {
  87.                 if ($userSetData['last_display_time'] < $threeDays->getTimestamp()) {
  88.                     return true;
  89.                 }
  90.             }
  91.         }
  92.         return false;
  93.     }
  94.     /**
  95.      * @return string
  96.      */
  97.     private function getCacheKey(int $userId)
  98.     {
  99.         return sprintf('%s_%s'self::CACHE_KEY_PREFIX$userId);
  100.     }
  101.     public static function getSubscribedEvents()
  102.     {
  103.         return [
  104.             'aqarmap.listing.user.search' => 'onSearch',
  105.         ];
  106.     }
  107. }