QueryFactory.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Model;
  7. use Magento\Search\Helper\Data;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\Helper\Context;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\Stdlib\StringUtils as StdlibString;
  12. /**
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class QueryFactory implements QueryFactoryInterface
  17. {
  18. /**
  19. * Query variable
  20. */
  21. const QUERY_VAR_NAME = 'q';
  22. /**
  23. * @var \Magento\Framework\App\RequestInterface
  24. */
  25. private $request;
  26. /**
  27. * @var Query
  28. */
  29. private $query;
  30. /**
  31. * @var ObjectManagerInterface
  32. */
  33. private $objectManager;
  34. /**
  35. * @var StdlibString
  36. */
  37. private $string;
  38. /**
  39. * @var ScopeConfigInterface
  40. */
  41. private $scopeConfig;
  42. /**
  43. * @var Data
  44. */
  45. private $queryHelper;
  46. /**
  47. * @param Context $context
  48. * @param ObjectManagerInterface $objectManager
  49. * @param StdlibString $string
  50. * @param Data|null $queryHelper
  51. */
  52. public function __construct(
  53. Context $context,
  54. ObjectManagerInterface $objectManager,
  55. StdlibString $string,
  56. Data $queryHelper = null
  57. ) {
  58. $this->request = $context->getRequest();
  59. $this->objectManager = $objectManager;
  60. $this->string = $string;
  61. $this->scopeConfig = $context->getScopeConfig();
  62. $this->queryHelper = $queryHelper === null ? $this->objectManager->get(Data::class) : $queryHelper;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function get()
  68. {
  69. if (!$this->query) {
  70. $maxQueryLength = $this->queryHelper->getMaxQueryLength();
  71. $minQueryLength = $this->queryHelper->getMinQueryLength();
  72. $rawQueryText = $this->getRawQueryText();
  73. $preparedQueryText = $this->getPreparedQueryText($rawQueryText, $maxQueryLength);
  74. $query = $this->create()->loadByQueryText($preparedQueryText);
  75. if (!$query->getId()) {
  76. $query->setQueryText($preparedQueryText);
  77. }
  78. $query->setIsQueryTextExceeded($this->isQueryTooLong($rawQueryText, $maxQueryLength));
  79. $query->setIsQueryTextShort($this->isQueryTooShort($rawQueryText, $minQueryLength));
  80. $this->query = $query;
  81. }
  82. return $this->query;
  83. }
  84. /**
  85. * Create new instance
  86. *
  87. * @param array $data
  88. * @return Query
  89. */
  90. public function create(array $data = [])
  91. {
  92. return $this->objectManager->create(Query::class, $data);
  93. }
  94. /**
  95. * Retrieve search query text
  96. *
  97. * @return string
  98. */
  99. private function getRawQueryText()
  100. {
  101. $queryText = $this->request->getParam(self::QUERY_VAR_NAME);
  102. return ($queryText === null || is_array($queryText))
  103. ? ''
  104. : $this->string->cleanString(trim($queryText));
  105. }
  106. /**
  107. * @param string $queryText
  108. * @param int|string $maxQueryLength
  109. * @return string
  110. */
  111. private function getPreparedQueryText($queryText, $maxQueryLength)
  112. {
  113. if ($this->isQueryTooLong($queryText, $maxQueryLength)) {
  114. $queryText = $this->string->substr($queryText, 0, $maxQueryLength);
  115. }
  116. return $queryText;
  117. }
  118. /**
  119. * @param string $queryText
  120. * @param int|string $maxQueryLength
  121. * @return bool
  122. */
  123. private function isQueryTooLong($queryText, $maxQueryLength)
  124. {
  125. return ($maxQueryLength !== '' && $this->string->strlen($queryText) > $maxQueryLength);
  126. }
  127. /**
  128. * @param string $queryText
  129. * @param int|string $minQueryLength
  130. * @return bool
  131. */
  132. private function isQueryTooShort($queryText, $minQueryLength)
  133. {
  134. return ($this->string->strlen($queryText) < $minQueryLength);
  135. }
  136. }