Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PopularSearchTerms.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Search\Model;
8 
13 {
14  const XML_PATH_MAX_COUNT_CACHEABLE_SEARCH_TERMS = 'catalog/search/max_count_cacheable_search_terms';
15 
21  private $scopeConfig;
22 
28  private $queryCollection;
29 
34  public function __construct(
35  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
36  \Magento\Search\Model\ResourceModel\Query\Collection $queryCollection
37  ) {
38  $this->scopeConfig = $scopeConfig;
39  $this->queryCollection = $queryCollection;
40  }
41 
49  public function isCacheable(string $term, int $storeId)
50  {
51  $terms = $this->queryCollection
52  ->setPopularQueryFilter($storeId)
53  ->setPageSize($this->getMaxCountCacheableSearchTerms($storeId))
54  ->load()
55  ->getColumnValues('query_text');
56 
57  return in_array($term, $terms);
58  }
59 
66  private function getMaxCountCacheableSearchTerms(int $storeId)
67  {
68  return $this->scopeConfig->getValue(
69  self::XML_PATH_MAX_COUNT_CACHEABLE_SEARCH_TERMS,
70  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
71  $storeId
72  );
73  }
74 }