From 2e7287cc3c9ba81a4e0036e56985026a5989adfe Mon Sep 17 00:00:00 2001 From: Fanny DECLERCK Date: Thu, 19 May 2016 15:13:28 +0200 Subject: [PATCH 1/2] Move file into Core module to use abstract class in other elasticsuite module --- .../Model/Category/Indexer/IndexerHandler.php | 2 +- .../Model/Product/Indexer/IndexerHandler.php | 2 +- .../Category/Indexer/Fulltext/Action/Full.php | 4 +- .../Datasource/AbstractAttributeData.php | 4 +- .../ResourceModel/Eav/Indexer/Indexer.php | 52 +++++++++++ .../Product/Indexer/Fulltext/Action/Full.php | 4 +- .../Fulltext/Datasource/CategoryData.php | 4 +- .../Fulltext/Datasource/InventoryData.php | 4 +- .../Indexer/Fulltext/Datasource/PriceData.php | 4 +- .../Model/Indexer/AbstractIndexerHandler.php} | 5 +- .../ResourceModel/Indexer/AbstractIndexer.php | 91 +++++++++++++++++++ 11 files changed, 160 insertions(+), 16 deletions(-) create mode 100644 src/module-elasticsuite-catalog/Model/ResourceModel/Eav/Indexer/Indexer.php rename src/{module-elasticsuite-catalog/Model/Eav/Indexer/IndexerHandler.php => module-elasticsuite-core/Model/Indexer/AbstractIndexerHandler.php} (96%) create mode 100644 src/module-elasticsuite-core/Model/ResourceModel/Indexer/AbstractIndexer.php diff --git a/src/module-elasticsuite-catalog/Model/Category/Indexer/IndexerHandler.php b/src/module-elasticsuite-catalog/Model/Category/Indexer/IndexerHandler.php index 7f0b4ea5f..3fb783464 100644 --- a/src/module-elasticsuite-catalog/Model/Category/Indexer/IndexerHandler.php +++ b/src/module-elasticsuite-catalog/Model/Category/Indexer/IndexerHandler.php @@ -14,7 +14,7 @@ namespace Smile\ElasticSuiteCatalog\Model\Category\Indexer; -use Smile\ElasticSuiteCatalog\Model\Eav\Indexer\IndexerHandler as AbstractIndexer; +use Smile\ElasticSuiteCore\Model\Indexer\AbstractIndexerHandler as AbstractIndexer; /** * Indexing operation handling for ElasticSearch engine. diff --git a/src/module-elasticsuite-catalog/Model/Product/Indexer/IndexerHandler.php b/src/module-elasticsuite-catalog/Model/Product/Indexer/IndexerHandler.php index 568cab1b7..eff893708 100644 --- a/src/module-elasticsuite-catalog/Model/Product/Indexer/IndexerHandler.php +++ b/src/module-elasticsuite-catalog/Model/Product/Indexer/IndexerHandler.php @@ -14,7 +14,7 @@ namespace Smile\ElasticSuiteCatalog\Model\Product\Indexer; -use Smile\ElasticSuiteCatalog\Model\Eav\Indexer\IndexerHandler as AbstractIndexer; +use Smile\ElasticSuiteCore\Model\Indexer\AbstractIndexerHandler as AbstractIndexer; /** * Indexing operation handling for ElasticSearch engine. diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Category/Indexer/Fulltext/Action/Full.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Category/Indexer/Fulltext/Action/Full.php index be924fd18..3ec9101f9 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Category/Indexer/Fulltext/Action/Full.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Category/Indexer/Fulltext/Action/Full.php @@ -13,7 +13,7 @@ namespace Smile\ElasticSuiteCatalog\Model\ResourceModel\Category\Indexer\Fulltext\Action; -use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\AbstractIndexer; +use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\Indexer; /** * ElasticSearch category full indexer resource model. @@ -22,7 +22,7 @@ * @package Smile_ElasticSuiteCatalog * @author Aurelien FOUCRET */ -class Full extends AbstractIndexer +class Full extends Indexer { /** * Load a bulk of category data. diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Eav/Indexer/Fulltext/Datasource/AbstractAttributeData.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Eav/Indexer/Fulltext/Datasource/AbstractAttributeData.php index 57673ca0f..73859d03d 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Eav/Indexer/Fulltext/Datasource/AbstractAttributeData.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Eav/Indexer/Fulltext/Datasource/AbstractAttributeData.php @@ -13,7 +13,7 @@ namespace Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\Fulltext\Datasource; -use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\AbstractIndexer; +use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\Indexer; use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection as AttributeCollection; /** @@ -23,7 +23,7 @@ * @package Smile_ElasticSuiteCatalog * @author Romain Ruaud */ -class AbstractAttributeData extends AbstractIndexer +class AbstractAttributeData extends Indexer { /** * @var array diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Eav/Indexer/Indexer.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Eav/Indexer/Indexer.php new file mode 100644 index 000000000..8f8003741 --- /dev/null +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Eav/Indexer/Indexer.php @@ -0,0 +1,52 @@ + + * @author Fanny DECLERCK + * @copyright 2016 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer; + +use Magento\Framework\App\ResourceConnection; +use Smile\ElasticSuiteCore\Model\ResourceModel\Indexer\AbstractIndexer; + +/** + * This class provides a lot of util methods used by Eav indexer related resource models. + * + * @category Smile + * @package Smile_ElasticSuiteCatalog + * @author Aurelien FOUCRET + * @author Fanny DECLERCK + */ +class Indexer extends AbstractIndexer +{ + /** + * @var \Magento\Store\Model\StoreManagerInterface + */ + protected $storeManager; + + /** + * Retrieve store root category id. + * + * @param \Magento\Store\Api\Data\StoreInterface|int|string $store Store id. + * + * @return integer + */ + protected function getRootCategoryId($store) + { + if (is_numeric($store) || is_string($store)) { + $store = $this->getStore($store); + } + + $storeGroupId = $store->getStoreGroupId(); + + return $this->storeManager->getGroup($storeGroupId)->getRootCategoryId(); + } +} diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Action/Full.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Action/Full.php index 64c8bd13e..81637aa59 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Action/Full.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Action/Full.php @@ -14,7 +14,7 @@ namespace Smile\ElasticSuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Action; -use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\AbstractIndexer; +use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\Indexer; /** * ElasticSearch product full indexer resource model. @@ -23,7 +23,7 @@ * @package Smile_ElasticSuiteCatalog * @author Aurelien FOUCRET */ -class Full extends AbstractIndexer +class Full extends Indexer { /** * Load a bulk of product data. diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/CategoryData.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/CategoryData.php index 8dfe9ec18..efe3605ee 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/CategoryData.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/CategoryData.php @@ -17,7 +17,7 @@ use Magento\Eav\Model\Config; use Magento\Framework\App\ResourceConnection; use Magento\Store\Model\StoreManagerInterface; -use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\AbstractIndexer; +use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\Indexer; /** * Categories data datasource resource model. @@ -26,7 +26,7 @@ * @package Smile_ElasticSuiteCatalog * @author Aurelien FOUCRET */ -class CategoryData extends AbstractIndexer +class CategoryData extends Indexer { /** * @var array Local cache for category names diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/InventoryData.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/InventoryData.php index 0146aa7dd..f0e4b000a 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/InventoryData.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/InventoryData.php @@ -15,7 +15,7 @@ use Magento\CatalogInventory\Api\StockRegistryInterface; use Magento\Framework\App\ResourceConnection; use Magento\Store\Model\StoreManagerInterface; -use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\AbstractIndexer; +use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\Indexer; /** * Catalog Inventory Data source resource model @@ -24,7 +24,7 @@ * @package Smile_ElasticSuiteCatalog * @author Romain Ruaud */ -class InventoryData extends AbstractIndexer +class InventoryData extends Indexer { /** * @var \Magento\CatalogInventory\Api\StockRegistryInterface diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/PriceData.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/PriceData.php index 0748fa17b..5c2904b40 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/PriceData.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/PriceData.php @@ -14,7 +14,7 @@ namespace Smile\ElasticSuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource; -use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\AbstractIndexer; +use Smile\ElasticSuiteCatalog\Model\ResourceModel\Eav\Indexer\Indexer; /** * Prices data datasource resource model. @@ -23,7 +23,7 @@ * @package Smile_ElasticSuiteCatalog * @author Aurelien FOUCRET */ -class PriceData extends AbstractIndexer +class PriceData extends Indexer { /** * Load prices data for a list of product ids and a given store. diff --git a/src/module-elasticsuite-catalog/Model/Eav/Indexer/IndexerHandler.php b/src/module-elasticsuite-core/Model/Indexer/AbstractIndexerHandler.php similarity index 96% rename from src/module-elasticsuite-catalog/Model/Eav/Indexer/IndexerHandler.php rename to src/module-elasticsuite-core/Model/Indexer/AbstractIndexerHandler.php index 4cfc1f1f2..ed4b840fd 100644 --- a/src/module-elasticsuite-catalog/Model/Eav/Indexer/IndexerHandler.php +++ b/src/module-elasticsuite-core/Model/Indexer/AbstractIndexerHandler.php @@ -11,7 +11,7 @@ * @license Open Software License ("OSL") v. 3.0 */ -namespace Smile\ElasticSuiteCatalog\Model\Eav\Indexer; +namespace Smile\ElasticSuiteCore\Model\Indexer; use Magento\Framework\Indexer\SaveHandler\IndexerInterface; use Smile\ElasticSuiteCore\Api\Index\IndexOperationInterface; @@ -24,8 +24,9 @@ * @package Smile_ElasticSuiteCatalog * @author Aurelien FOUCRET * @author Romain Ruaud + * @author Fanny DECLERCK */ -class IndexerHandler implements IndexerInterface +class AbstractIndexerHandler implements IndexerInterface { /** * @var \Smile\ElasticSuiteCore\Api\Index\IndexOperationInterface diff --git a/src/module-elasticsuite-core/Model/ResourceModel/Indexer/AbstractIndexer.php b/src/module-elasticsuite-core/Model/ResourceModel/Indexer/AbstractIndexer.php new file mode 100644 index 000000000..1fd24e868 --- /dev/null +++ b/src/module-elasticsuite-core/Model/ResourceModel/Indexer/AbstractIndexer.php @@ -0,0 +1,91 @@ + + * @author Fanny DECLERCK + * @copyright 2016 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticSuiteCore\Model\ResourceModel\Indexer; + +use Magento\Framework\App\ResourceConnection; +use Magento\Store\Model\StoreManagerInterface; + +/** + * This class provides a lot of util methods used by Eav indexer related resource models. + * + * @category Smile + * @package Smile_ElasticSuiteCore + * @author Fanny DECLERCK + */ +class AbstractIndexer +{ + /** + * @var \Magento\Framework\App\ResourceConnection + */ + protected $resource; + + /** + * @var \Magento\Framework\DB\Adapter\AdapterInterface + */ + protected $connection; + + /** + * @var \Magento\Store\Model\StoreManagerInterface + */ + protected $storeManager; + + /** + * Constructor. + * + * @param ResourceConnection $resource Database adpater. + * @param StoreManagerInterface $storeManager Store manager. + */ + public function __construct(ResourceConnection $resource, StoreManagerInterface $storeManager) + { + $this->resource = $resource; + $this->connection = $resource->getConnection(); + $this->storeManager = $storeManager; + } + + + /** + * Get table name using the adapter. + * + * @param string $tableName Table name. + * + * @return string + */ + protected function getTable($tableName) + { + return $this->resource->getTableName($tableName); + } + + /** + * Return database connection. + * + * @return \Magento\Framework\DB\Adapter\AdapterInterface + */ + protected function getConnection() + { + return $this->connection; + } + + /** + * Get store by id. + * + * @param integer $storeId Store id. + * + * @return \Magento\Store\Api\Data\StoreInterface + */ + protected function getStore($storeId) + { + return $this->storeManager->getStore($storeId); + } +} From 4d640c3f24375ab791ca9a8fdd173a1f97b10000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20FOUCRET?= Date: Fri, 20 May 2016 10:12:44 +0200 Subject: [PATCH 2/2] Index handler refactor. --- .../Model/Category/Indexer/Fulltext.php | 21 ++++------ .../Model/Category/Indexer/IndexerHandler.php | 42 ------------------- .../Model/Product/Indexer/IndexerHandler.php | 42 ------------------- src/module-elasticsuite-catalog/etc/di.xml | 22 +++++++++- .../GenericIndexerHandler.php} | 4 +- 5 files changed, 32 insertions(+), 99 deletions(-) delete mode 100644 src/module-elasticsuite-catalog/Model/Category/Indexer/IndexerHandler.php delete mode 100644 src/module-elasticsuite-catalog/Model/Product/Indexer/IndexerHandler.php rename src/module-elasticsuite-core/{Model/Indexer/AbstractIndexerHandler.php => Indexer/GenericIndexerHandler.php} (97%) diff --git a/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php b/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php index 51b28eeb7..ec09a829f 100644 --- a/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php +++ b/src/module-elasticsuite-catalog/Model/Category/Indexer/Fulltext.php @@ -15,6 +15,7 @@ use Magento\Framework\Search\Request\DimensionFactory; use Magento\Store\Model\StoreManagerInterface; use Smile\ElasticSuiteCatalog\Model\Category\Indexer\Fulltext\Action\Full; +use Magento\Framework\Indexer\SaveHandler\IndexerInterface; /** * Categories fulltext indexer @@ -34,7 +35,7 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F protected $data; /** - * @var IndexerHandler + * @var IndexerInterface */ private $indexerHandler; @@ -55,14 +56,14 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F /** * @param Full $fullAction The full index action - * @param IndexerHandler $indexerHandler The index handler + * @param IndexerInterface $indexerHandler The index handler * @param StoreManagerInterface $storeManager The Store Manager * @param DimensionFactory $dimensionFactory The dimension factory * @param array $data The data */ public function __construct( Full $fullAction, - IndexerHandler $indexerHandler, + IndexerInterface $indexerHandler, StoreManagerInterface $storeManager, DimensionFactory $dimensionFactory, array $data @@ -84,12 +85,11 @@ public function __construct( public function execute($ids) { $storeIds = array_keys($this->storeManager->getStores()); - /** @var IndexerHandler $saveHandler */ - $saveHandler = $this->indexerHandler; + foreach ($storeIds as $storeId) { $dimension = $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId]); - $saveHandler->deleteIndex([$dimension], new \ArrayObject($ids)); - $saveHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId, $ids)); + $this->indexerHandler->deleteIndex([$dimension], new \ArrayObject($ids)); + $this->indexerHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId, $ids)); } } @@ -102,13 +102,10 @@ public function executeFull() { $storeIds = array_keys($this->storeManager->getStores()); - /** @var IndexerHandler $saveHandler */ - $saveHandler = $this->indexerHandler; - foreach ($storeIds as $storeId) { $dimension = $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId]); - $saveHandler->cleanIndex([$dimension]); - $saveHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId)); + $this->indexerHandler->cleanIndex([$dimension]); + $this->indexerHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId)); } } diff --git a/src/module-elasticsuite-catalog/Model/Category/Indexer/IndexerHandler.php b/src/module-elasticsuite-catalog/Model/Category/Indexer/IndexerHandler.php deleted file mode 100644 index 3fb783464..000000000 --- a/src/module-elasticsuite-catalog/Model/Category/Indexer/IndexerHandler.php +++ /dev/null @@ -1,42 +0,0 @@ - - * @copyright 2016 Smile - * @license Open Software License ("OSL") v. 3.0 - */ - -namespace Smile\ElasticSuiteCatalog\Model\Category\Indexer; - -use Smile\ElasticSuiteCore\Model\Indexer\AbstractIndexerHandler as AbstractIndexer; - -/** - * Indexing operation handling for ElasticSearch engine. - * - * @category Smile - * @package Smile_ElasticSuiteCatalog - * @author Aurelien FOUCRET - */ -class IndexerHandler extends AbstractIndexer -{ - const INDEX_NAME = 'catalog_category'; - const TYPE_NAME = 'category'; - - /** - * {@inheritDoc} - */ - public function __construct( - \Smile\ElasticSuiteCore\Api\Index\IndexOperationInterface $indexOperation, - \Magento\Framework\Indexer\SaveHandler\Batch $batch, - $indexName = self::INDEX_NAME, - $typeName = self::TYPE_NAME - ) { - parent::__construct($indexOperation, $batch, $indexName, $typeName); - } -} diff --git a/src/module-elasticsuite-catalog/Model/Product/Indexer/IndexerHandler.php b/src/module-elasticsuite-catalog/Model/Product/Indexer/IndexerHandler.php deleted file mode 100644 index eff893708..000000000 --- a/src/module-elasticsuite-catalog/Model/Product/Indexer/IndexerHandler.php +++ /dev/null @@ -1,42 +0,0 @@ - - * @copyright 2016 Smile - * @license Open Software License ("OSL") v. 3.0 - */ - -namespace Smile\ElasticSuiteCatalog\Model\Product\Indexer; - -use Smile\ElasticSuiteCore\Model\Indexer\AbstractIndexerHandler as AbstractIndexer; - -/** - * Indexing operation handling for ElasticSearch engine. - * - * @category Smile - * @package Smile_ElasticSuiteCatalog - * @author Aurelien FOUCRET - */ -class IndexerHandler extends AbstractIndexer -{ - const INDEX_NAME = 'catalog_product'; - const TYPE_NAME = 'product'; - - /** - * {@inheritDoc} - */ - public function __construct( - \Smile\ElasticSuiteCore\Api\Index\IndexOperationInterface $indexOperation, - \Magento\Framework\Indexer\SaveHandler\Batch $batch, - $indexName = self::INDEX_NAME, - $typeName = self::TYPE_NAME - ) { - parent::__construct($indexOperation, $batch, $indexName, $typeName); - } -} diff --git a/src/module-elasticsuite-catalog/etc/di.xml b/src/module-elasticsuite-catalog/etc/di.xml index 5e0c21230..e375d439c 100644 --- a/src/module-elasticsuite-catalog/etc/di.xml +++ b/src/module-elasticsuite-catalog/etc/di.xml @@ -17,10 +17,17 @@ --> + + + catalog_product + product + + + - Smile\ElasticSuiteCatalog\Model\Product\Indexer\IndexerHandler + catalogProductSearchIndexHandler @@ -107,6 +114,19 @@ + + + catalog_category + category + + + + + + catalogCategorySearchIndexHandler + + + Smile\ElasticSuiteCatalog\Helper\CategoryAttribute diff --git a/src/module-elasticsuite-core/Model/Indexer/AbstractIndexerHandler.php b/src/module-elasticsuite-core/Indexer/GenericIndexerHandler.php similarity index 97% rename from src/module-elasticsuite-core/Model/Indexer/AbstractIndexerHandler.php rename to src/module-elasticsuite-core/Indexer/GenericIndexerHandler.php index ed4b840fd..d1e8b79e4 100644 --- a/src/module-elasticsuite-core/Model/Indexer/AbstractIndexerHandler.php +++ b/src/module-elasticsuite-core/Indexer/GenericIndexerHandler.php @@ -11,7 +11,7 @@ * @license Open Software License ("OSL") v. 3.0 */ -namespace Smile\ElasticSuiteCore\Model\Indexer; +namespace Smile\ElasticSuiteCore\Indexer; use Magento\Framework\Indexer\SaveHandler\IndexerInterface; use Smile\ElasticSuiteCore\Api\Index\IndexOperationInterface; @@ -26,7 +26,7 @@ * @author Romain Ruaud * @author Fanny DECLERCK */ -class AbstractIndexerHandler implements IndexerInterface +class GenericIndexerHandler implements IndexerInterface { /** * @var \Smile\ElasticSuiteCore\Api\Index\IndexOperationInterface