From 2d8474d649b3975f21c58c249de0baf798f8a8ff Mon Sep 17 00:00:00 2001 From: Romain Ruaud Date: Fri, 17 Sep 2021 10:57:41 +0200 Subject: [PATCH] Fix #71 by injecting proper store scope in explain. --- .../Controller/Adminhtml/Explain/Results.php | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/module-elasticsuite-explain/Controller/Adminhtml/Explain/Results.php b/src/module-elasticsuite-explain/Controller/Adminhtml/Explain/Results.php index 9764e374e..9e18232a7 100644 --- a/src/module-elasticsuite-explain/Controller/Adminhtml/Explain/Results.php +++ b/src/module-elasticsuite-explain/Controller/Adminhtml/Explain/Results.php @@ -21,6 +21,7 @@ use Magento\Catalog\Api\Data\CategoryInterface; use Magento\Framework\Json\Helper\Data as JsonHelper; use Magento\Search\Model\QueryFactory; +use Magento\Store\Model\StoreManagerInterface; use Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterfaceFactory; use Smile\ElasticsuiteExplain\Model\ResultFactory as ResultModelFactory; use Smile\ElasticsuiteCore\Api\Search\ContextInterface; @@ -30,6 +31,8 @@ /** * Explain Adminhtml Index controller. * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * * @category Smile * @package Smile\ElasticsuiteExplain * @author Romain Ruaud @@ -71,6 +74,11 @@ class Results extends Action */ private $searchContext; + /** + * @var \Magento\Store\Model\StoreManagerInterface + */ + private $storeManager; + /** * Constructor. * @@ -82,6 +90,7 @@ class Results extends Action * @param JsonHelper $jsonHelper JSON Helper. * @param QueryFactory $queryFactory Query Factory. * @param ContextInterface $searchContext Search context. + * @param StoreManagerInterface $storeManager Store Manager. */ public function __construct( Context $context, @@ -91,7 +100,8 @@ public function __construct( ContainerConfigurationFactory $containerConfigFactory, JsonHelper $jsonHelper, QueryFactory $queryFactory, - ContextInterface $searchContext + ContextInterface $searchContext, + StoreManagerInterface $storeManager ) { parent::__construct($context); @@ -102,6 +112,7 @@ public function __construct( $this->jsonHelper = $jsonHelper; $this->queryFactory = $queryFactory; $this->searchContext = $searchContext; + $this->storeManager = $storeManager; } /** @@ -139,6 +150,7 @@ private function getResultObject() $containerConfig = $this->getContainerConfiguration(); $this->updateSearchContext($this->getStoreId(), $category, $queryText); + $this->updateStoreContext($this->getStoreId()); return $this->resultModelFactory->create( [ @@ -259,4 +271,19 @@ private function updateSearchContext($storeId, $category, $queryText) $this->searchContext->setCurrentCategory($category); } } + + /** + * Update the store manager current store. + * This is due to the fact that some components might want to rely on $storeManager->getStore() later. + * + * @param integer $storeId Store id. + * + * @return void + */ + private function updateStoreContext($storeId) + { + $store = $this->storeManager->getStore($storeId); + + $this->storeManager->setCurrentStore($store); + } }