_queryCollectionFactory = $queryCollectionFactory; $this->_searchCollectionFactory = $searchCollectionFactory; $this->_storeManager = $storeManager; $this->_scopeConfig = $scopeConfig; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } /** * Init resource model * * @return void */ protected function _construct() { $this->_init(\Magento\Search\Model\ResourceModel\Query::class); } /** * Retrieve search collection * * @return Collection */ public function getSearchCollection() { return $this->_searchCollectionFactory->create(); } /** * Retrieve collection of suggest queries * * @return QueryCollection * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getSuggestCollection() { $collection = $this->getData('suggest_collection'); if ($collection === null) { $collection = $this->_queryCollectionFactory->create()->setStoreId( $this->getStoreId() )->setQueryFilter( $this->getQueryText() ); $this->setData('suggest_collection', $collection); } return $collection; } /** * Load Query object by query string * * @param string $text * @return $this * @throws \Magento\Framework\Exception\LocalizedException * @deprecated 100.1.0 "synonym for" feature has been removed */ public function loadByQuery($text) { $this->loadByQueryText($text); return $this; } /** * Load Query object only by query text * * @param string $text * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function loadByQueryText($text) { $this->_getResource()->loadByQueryText($this, $text); $this->_afterLoad(); $this->setOrigData(); return $this; } /** * Set Store Id * * @param int $storeId * @return void */ public function setStoreId($storeId) { $this->setData('store_id', $storeId); } /** * Retrieve store Id * * @return int * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getStoreId() { if (!($storeId = $this->getData('store_id'))) { $storeId = $this->_storeManager->getStore()->getId(); } return $storeId; } /** * Prepare save query for result * * @return $this * @throws \Exception */ public function prepare() { if (!$this->getId()) { $this->setIsActive(0); $this->setIsProcessed(0); $this->save(); $this->setIsActive(1); } return $this; } /** * Save query with incremental popularity * * @return $this * * @throws \Magento\Framework\Exception\LocalizedException */ public function saveIncrementalPopularity() { $this->getResource()->saveIncrementalPopularity($this); return $this; } /** * Save query with number of results * * @param int $numResults * @return $this * * @throws \Magento\Framework\Exception\LocalizedException */ public function saveNumResults($numResults) { $this->setNumResults($numResults); $this->getResource()->saveNumResults($this); return $this; } /** * Retrieve minimum query length * * @return int * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getMinQueryLength() { return $this->_scopeConfig->getValue( self::XML_PATH_MIN_QUERY_LENGTH, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->getStoreId() ); } /** * Retrieve maximum query length * * @return int * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getMaxQueryLength() { return $this->_scopeConfig->getValue( self::XML_PATH_MAX_QUERY_LENGTH, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->getStoreId() ); } /** * @inheritdoc */ public function getQueryText() { return $this->getDataByKey('query_text'); } /** * Check if query maximum length exceeded. * * @return bool * @codeCoverageIgnore */ public function isQueryTextExceeded() { return $this->getData('is_query_text_exceeded'); } /** * Check if minimum query length reached. * * @return bool * @codeCoverageIgnore * @since 100.1.0 */ public function isQueryTextShort() { return $this->getData('is_query_text_short'); } }