vendor/store.shopware.com/hatslogicswsearchkeywordhistory/src/Subscriber/SearchSubscriber.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * (c) 2Hats Logic Solutions <info@2hatslogic.com>
  4.  */
  5. namespace Hatslogic\Sw\SearchKeywordHistory\Subscriber;
  6. use Shopware\Core\Defaults;
  7. use Doctrine\DBAL\Connection;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Symfony\Component\Routing\RouterInterface;
  11. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. class SearchSubscriber implements EventSubscriberInterface
  17. {
  18.    /**
  19.      * @var ContainerInterface
  20.      */
  21.     protected $container;
  22.     /**
  23.      * @var Connection
  24.      */
  25.     private $connection;
  26.     /**
  27.      * @var RouterInterface
  28.      */
  29.     private $router;
  30.     public function __construct(
  31.         ContainerInterface $container
  32.         Connection $connection,
  33.         RouterInterface $router
  34.     ) {
  35.         $this->container $container;
  36.         $this->connection $connection;
  37.         $this->router $router;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             SearchPageLoadedEvent::class => 'onSearchPageLoaded'
  43.         ];
  44.     }
  45.     public function onSearchPageLoaded(SearchPageLoadedEvent $event)
  46.     {
  47.         $request $event->getRequest();
  48.         $searchKeyword $request->get('search');
  49.         if(empty($searchKeyword))
  50.         {
  51.             return;
  52.         }
  53.         $keywordAlreadyExist $this->getDetailsByKeyWord($searchKeyword);
  54.         if($keywordAlreadyExist)
  55.         {
  56.             //Update count and last search date
  57.             $params = [
  58.                 'id'           => $keywordAlreadyExist->getId(),
  59.                 'searchCount'  => $keywordAlreadyExist->getSearchCount() + 1,
  60.                 'lastSearchAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  61.             ];
  62.             $this->updateCountAndLastSearchDate($params);
  63.         } else {
  64.             // Save
  65.             $params = [
  66.                 'id'           => Uuid::randomHex(),
  67.                 'keyword'      => $searchKeyword,
  68.                 'searchCount'  => 1,
  69.                 'lastSearchAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  70.             ];
  71.             $this->saveSearchKeyWord($params);
  72.         }
  73.         return;
  74.     }
  75.     public function saveSearchKeyWord($params)
  76.     {
  77.         if(empty($params))
  78.         {
  79.             return;
  80.         }
  81.         $this->container->get('s_plugin_hatslogic_search_keywords.repository')
  82.             ->create([$params], Context::createDefaultContext());
  83.         return;
  84.     }
  85.     public function getDetailsByKeyWord($keyword)
  86.     {
  87.         $criteria = (new Criteria())
  88.             ->addFilter(new EqualsFilter(
  89.             's_plugin_hatslogic_search_keywords.keyword'$keyword));
  90.         $result $this->container->get('s_plugin_hatslogic_search_keywords.repository')->search($criteriaContext::createDefaultContext());
  91.         $searchKeyWordDetails $result->first();
  92.         return ($searchKeyWordDetails) ? $searchKeyWordDetailsnull;
  93.     }
  94.     public function updateCountAndLastSearchDate($params)
  95.     {
  96.         if(empty($params))
  97.         {
  98.             return;
  99.         }
  100.         $this->container->get('s_plugin_hatslogic_search_keywords.repository')->update(
  101.             [
  102.                 [
  103.                     'id'           => $params['id'],
  104.                     'searchCount'  => $params['searchCount'],
  105.                     'lastSearchAt' => $params['lastSearchAt']
  106.                 ],
  107.             ],
  108.             Context::createDefaultContext()
  109.         );
  110.         return true;
  111.     }
  112. }