PopularSearchTerms.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Model;
  7. /**
  8. * Popular search terms
  9. */
  10. class PopularSearchTerms
  11. {
  12. const XML_PATH_MAX_COUNT_CACHEABLE_SEARCH_TERMS = 'catalog/search/max_count_cacheable_search_terms';
  13. /**
  14. * Scope configuration
  15. *
  16. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  17. */
  18. private $scopeConfig;
  19. /**
  20. * Catalog search data
  21. *
  22. * @var \Magento\Search\Model\ResourceModel\Query\Collection
  23. */
  24. private $queryCollection;
  25. /**
  26. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  27. * @param \Magento\Search\Model\ResourceModel\Query\Collection
  28. */
  29. public function __construct(
  30. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  31. \Magento\Search\Model\ResourceModel\Query\Collection $queryCollection
  32. ) {
  33. $this->scopeConfig = $scopeConfig;
  34. $this->queryCollection = $queryCollection;
  35. }
  36. /**
  37. * Check if is cacheable search term
  38. *
  39. * @param string $term
  40. * @param int $storeId
  41. * @return bool
  42. */
  43. public function isCacheable(string $term, int $storeId)
  44. {
  45. $terms = $this->queryCollection
  46. ->setPopularQueryFilter($storeId)
  47. ->setPageSize($this->getMaxCountCacheableSearchTerms($storeId))
  48. ->load()
  49. ->getColumnValues('query_text');
  50. return in_array($term, $terms);
  51. }
  52. /**
  53. * Retrieve maximum count cacheable search terms
  54. *
  55. * @param int $storeId
  56. * @return int
  57. */
  58. private function getMaxCountCacheableSearchTerms(int $storeId)
  59. {
  60. return $this->scopeConfig->getValue(
  61. self::XML_PATH_MAX_COUNT_CACHEABLE_SEARCH_TERMS,
  62. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  63. $storeId
  64. );
  65. }
  66. }