src/Aqarmap/Bundle/ListingBundle/EventListener/SearchKeywordHistorySubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\ListingBundle\Event\SearchTriggerEvent;
  4. use Aqarmap\Bundle\MainBundle\Constant\ProducerQueues;
  5. use Aqarmap\Bundle\MainBundle\Contract\ProducerFactoryInterface;
  6. use Aqarmap\Bundle\MainBundle\Service\MobileDetectionService;
  7. use Aqarmap\Bundle\SearchBundle\Constant\SearchKeywordConstant;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. class SearchKeywordHistorySubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var ProducerFactoryInterface
  15.      */
  16.     private $producer;
  17.     private $source;
  18.     private $searchKeyword;
  19.     /**
  20.      * @var MobileDetectionService
  21.      */
  22.     private $mobileService;
  23.     /**
  24.      * @var LoggerInterface
  25.      */
  26.     private $logger;
  27.     /**
  28.      * SearchKeywordHistorySubscriber constructor.
  29.      */
  30.     public function __construct(
  31.         ProducerFactoryInterface $producerFactory,
  32.         MobileDetectionService $mobileService,
  33.         LoggerInterface $logger
  34.     ) {
  35.         $this->producer $producerFactory->create(ProducerQueues::RECORD_KEYWORD_SEARCH_HISTORY);
  36.         $this->mobileService $mobileService;
  37.         $this->logger $logger;
  38.     }
  39.     /**
  40.      * Producer.
  41.      */
  42.     public function onSearchKeyword(SearchTriggerEvent $searchTriggerEvent): void
  43.     {
  44.         try {
  45.             $this->setSource($searchTriggerEvent->getRequest());
  46.             $this->setSearchKeyword($searchTriggerEvent->getRequest());
  47.             if (!empty($this->getSearchKeyword())) {
  48.                 $this
  49.                     ->producer
  50.                     ->publish(serialize([
  51.                         'searchKeyword' => $this->getSearchKeyword(),
  52.                         'userAgentOS' => $this->getSource(),
  53.                     ]))
  54.                 ;
  55.             }
  56.         } catch (\Exception $e) {
  57.             $this->logger->info($e->getMessage());
  58.         }
  59.     }
  60.     /**
  61.      * @param int
  62.      */
  63.     public function getSource()
  64.     {
  65.         return $this->source;
  66.     }
  67.     /**
  68.      * @param string
  69.      */
  70.     public function setSource(Request $request): self
  71.     {
  72.         $userAgent $request->cookies->get('user-agent');
  73.         if (!$userAgent) {
  74.             $userAgent $request->headers->get('user-agent');
  75.         }
  76.         $userAgentInfo $this->mobileService->getInfo($userAgent);
  77.         switch ($userAgentInfo['os']) {
  78.             case SearchKeywordConstant::IOS_USER_AGENT_LABEL:
  79.                 $this->source SearchKeywordConstant::IOS_USER_AGENT;
  80.                 break;
  81.             case SearchKeywordConstant::ANDROID_USER_AGENT_LABEL:
  82.                 $this->source SearchKeywordConstant::ANDROID_USER_AGENT;
  83.                 break;
  84.             default:
  85.                 $this->source SearchKeywordConstant::WEB_USER_AGENT;
  86.                 break;
  87.         }
  88.         return $this;
  89.     }
  90.     /**
  91.      * @param string
  92.      */
  93.     public function getSearchKeyword()
  94.     {
  95.         return $this->searchKeyword;
  96.     }
  97.     public function setSearchKeyword(Request $request): self
  98.     {
  99.         $this->searchKeyword trim((string) $request->query->get('keywordSearch'));
  100.         return $this;
  101.     }
  102.     /**
  103.      * @return array
  104.      */
  105.     public static function getSubscribedEvents()
  106.     {
  107.         return [
  108.             SearchTriggerEvent::class => 'onSearchKeyword',
  109.         ];
  110.     }
  111. }