<?php
namespace Aqarmap\Bundle\CustomerProfilingBundle\EventListener;
use Aqarmap\Bundle\CustomerProfilingBundle\Entity\CustomerProfilingQuestionQueue;
use Aqarmap\Bundle\CustomerProfilingBundle\Services\CustomerProfilingService;
use Aqarmap\Bundle\MainBundle\Service\CacheManager;
use Aqarmap\Bundle\UserBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\UserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class CustomerProfilingListener.
*/
class CustomerProfilingListener implements EventSubscriberInterface
{
public const CACHE_KEY_PREFIX = 'customer_profiling';
/**
* @var EntityManagerInterface
*/
protected $em;
protected $twig;
protected $translator;
/** @var CustomerProfilingService */
protected $profilingService;
public function __construct(
EntityManagerInterface $em,
\Twig_Environment $twig_Environment,
TranslatorInterface $translator,
CustomerProfilingService $profilingService,
CacheManager $cacheManager
) {
$this->em = $em;
$this->twig = $twig_Environment;
$this->translator = $translator;
$this->profilingService = $profilingService;
$this->cacheManager = $cacheManager;
}
/**
* @throws \Exception
*/
public function onSearch(UserEvent $event): void
{
$user = $event->getUser();
/** @var CustomerProfilingQuestionQueue $currentSet */
$currentSet = $this->em
->getRepository('AqarmapCustomerProfilingBundle:CustomerProfilingQuestionQueue')
->getCurrentCustomerSet($user);
if ($currentSet && $this->canDisplaySet($currentSet, $user)) {
$questions = $this->profilingService->processQueueSet($currentSet);
$this->twig->addGlobal('customerProfilingSet', $questions);
$this->twig->addGlobal('currentSurveySet', $currentSet);
$currentDisplayDate = new \DateTime();
$currentSet->setLastDisplayTime($currentDisplayDate);
$this->em->persist($currentSet);
$this->em->flush($currentSet);
if (0 == $currentSet->getSkipNumber()) {
$this->cacheManager->set($this->getCacheKey($user->getId()), serialize([
'set_id' => $currentSet->getId(),
'last_display_time' => $currentDisplayDate->getTimestamp(),
]));
}
}
}
/**
* @return bool
*/
private function canDisplaySet(CustomerProfilingQuestionQueue $currentSet, User $user)
{
$oneWeek = new \DateTime('-1 week');
$threeDays = new \DateTime('-3 days');
$cacheKey = $this->getCacheKey($user->getId());
if (null == $currentSet->getLastDisplayTime() && 0 == $currentSet->getSkipNumber() && !$this->cacheManager->has($cacheKey)) {
return true;
} elseif ($this->cacheManager->has($cacheKey)) {
$userSetData = $this->cacheManager->get($cacheKey);
$userSetData = unserialize($userSetData);
if ($userSetData['set_id'] == $currentSet->getId()) {
if (0 == $currentSet->getSkipNumber()) {
return true;
} elseif ($currentSet->getSkipNumber() > 0
&& $userSetData['last_display_time'] <= $oneWeek->getTimestamp()
) {
return true;
}
} elseif ($userSetData['set_id'] != $currentSet->getId()
) {
if ($userSetData['last_display_time'] < $threeDays->getTimestamp()) {
return true;
}
}
}
return false;
}
/**
* @return string
*/
private function getCacheKey(int $userId)
{
return sprintf('%s_%s', self::CACHE_KEY_PREFIX, $userId);
}
public static function getSubscribedEvents()
{
return [
'aqarmap.listing.user.search' => 'onSearch',
];
}
}