1313 */
1414namespace Smile \ElasticsuiteCatalog \Model \ResourceModel \Product \Indexer \Fulltext \Datasource ;
1515
16+ use Magento \Framework \ObjectManagerInterface ;
1617use Smile \ElasticsuiteCatalog \Model \ResourceModel \Eav \Indexer \Indexer ;
1718use Smile \ElasticsuiteCatalog \Model \ResourceModel \Product \Indexer \Fulltext \Datasource \InventoryDataInterface ;
1819use Magento \Framework \App \ResourceConnection ;
1920use Magento \Framework \EntityManager \MetadataPool ;
2021use Magento \Store \Model \StoreManagerInterface ;
21- use Magento \InventorySalesApi \Api \Data \SalesChannelInterface ;
22- use Magento \InventorySalesApi \Api \StockResolverInterface ;
23- use Magento \InventoryIndexer \Model \StockIndexTableNameResolverInterface ;
24- use Magento \InventoryIndexer \Indexer \IndexStructure ;
2522
2623/**
2724 * Multi Source Inventory Catalog Inventory Data source resource model
3330class InventoryData extends Indexer implements InventoryDataInterface
3431{
3532 /**
36- * @var StockResolverInterface
33+ * @var \Magento\InventorySalesApi\Api\ StockResolverInterface
3734 */
3835 private $ stockResolver ;
3936
4037 /**
41- * @var StockIndexTableNameResolverInterface
38+ * @var \Magento\InventoryIndexer\Model\ StockIndexTableNameResolverInterface
4239 */
4340 private $ stockIndexTableProvider ;
4441
@@ -47,24 +44,26 @@ class InventoryData extends Indexer implements InventoryDataInterface
4744 */
4845 private $ stockIdByWebsite = [];
4946
47+ /**
48+ * @var \Magento\Framework\ObjectManagerInterface
49+ */
50+ private $ objectManager ;
51+
5052 /**
5153 * InventoryData constructor.
5254 *
53- * @param ResourceConnection $resource Database adapter.
54- * @param StoreManagerInterface $storeManager Store manager.
55- * @param MetadataPool $metadataPool Metadata Pool
56- * @param StockResolverInterface $stockResolver Stock resolver.
57- * @param StockIndexTableNameResolverInterface $stockIndexTableProvider Stock index table provider.
55+ * @param ResourceConnection $resource Database adapter.
56+ * @param StoreManagerInterface $storeManager Store manager.
57+ * @param MetadataPool $metadataPool Metadata Pool
58+ * @param ObjectManagerInterface $objectManager Object Manager.
5859 */
5960 public function __construct (
6061 ResourceConnection $ resource ,
6162 StoreManagerInterface $ storeManager ,
6263 MetadataPool $ metadataPool ,
63- StockResolverInterface $ stockResolver ,
64- StockIndexTableNameResolverInterface $ stockIndexTableProvider
64+ ObjectManagerInterface $ objectManager
6565 ) {
66- $ this ->stockResolver = $ stockResolver ;
67- $ this ->stockIndexTableProvider = $ stockIndexTableProvider ;
66+ $ this ->objectManager = $ objectManager ;
6867
6968 parent ::__construct ($ resource , $ storeManager , $ metadataPool );
7069 }
@@ -82,17 +81,17 @@ public function loadInventoryData($storeId, $productIds)
8281 {
8382 $ websiteId = $ this ->getWebsiteId ($ storeId );
8483 $ stockId = $ this ->getStockId ($ websiteId );
85- $ tableName = $ this ->stockIndexTableProvider ->execute ($ stockId );
84+ $ tableName = $ this ->getStockIndexTableProvider () ->execute ($ stockId );
8685
8786 $ select = $ this ->getConnection ()->select ()
8887 ->from (['product ' => $ this ->getTable ('catalog_product_entity ' )], [])
8988 ->join (
9089 ['stock_index ' => $ tableName ],
91- 'product.sku = stock_index. ' . IndexStructure::SKU ,
90+ 'product.sku = stock_index. ' . \ Magento \ InventoryIndexer \ Indexer \ IndexStructure::SKU ,
9291 [
9392 'product_id ' => 'product.entity_id ' ,
94- 'stock_status ' => 'stock_index. ' . IndexStructure::IS_SALABLE ,
95- 'qty ' => 'stock_index. ' . IndexStructure::QUANTITY ,
93+ 'stock_status ' => 'stock_index. ' . \ Magento \ InventoryIndexer \ Indexer \ IndexStructure::IS_SALABLE ,
94+ 'qty ' => 'stock_index. ' . \ Magento \ InventoryIndexer \ Indexer \ IndexStructure::QUANTITY ,
9695 ]
9796 )
9897 ->where ('product.entity_id IN (?) ' , $ productIds )
@@ -112,7 +111,10 @@ private function getStockId($websiteId)
112111 {
113112 if (!isset ($ this ->stockIdByWebsite [$ websiteId ])) {
114113 $ websiteCode = $ this ->storeManager ->getWebsite ($ websiteId )->getCode ();
115- $ stock = $ this ->stockResolver ->execute (SalesChannelInterface::TYPE_WEBSITE , $ websiteCode );
114+ $ stock = $ this ->getStockResolver ()->execute (
115+ \Magento \InventorySalesApi \Api \Data \SalesChannelInterface::TYPE_WEBSITE ,
116+ $ websiteCode
117+ );
116118 $ stockId = (int ) $ stock ->getStockId ();
117119 $ this ->stockIdByWebsite [$ websiteId ] = $ stockId ;
118120 }
@@ -131,4 +133,51 @@ private function getWebsiteId($storeId)
131133 {
132134 return $ this ->storeManager ->getStore ($ storeId )->getWebsiteId ();
133135 }
136+
137+ /**
138+ * Fetch the Stock Resolver from Object Manager instead of constructor to avoid compilation error when MSI modules are not there.
139+ * The fact that the class exists is normally already checked in the caller class.
140+ * @see Smile\ElasticsuiteCatalog\Model\Product\Indexer\Fulltext\Datasource\InventoryData
141+ *
142+ * @return \Magento\InventorySalesApi\Api\StockResolverInterface|null
143+ *
144+ * @throws \Magento\Framework\Exception\RuntimeException
145+ */
146+ private function getStockResolver ()
147+ {
148+ if (null === $ this ->stockResolver ) {
149+ try {
150+ $ this ->stockResolver = $ this ->objectManager ->get (\Magento \InventorySalesApi \Api \StockResolverInterface::class);
151+ } catch (\Exception $ exception ) {
152+ $ message = 'Failed to fetch the MSI stock resolver despite the fact MSI implementation was considered as usable. ' ;
153+ throw new \Magento \Framework \Exception \RuntimeException (__ ($ message . $ exception ->getMessage ()));
154+ }
155+ }
156+
157+ return $ this ->stockResolver ;
158+ }
159+
160+ /**
161+ * Fetch the Stock Index Table Provider from Object Manager instead of constructor
162+ * to avoid compilation error when MSI modules are not there.
163+ *
164+ * @return \Magento\InventorySalesApi\Api\StockResolverInterface|null
165+ *
166+ * @throws \Magento\Framework\Exception\RuntimeException
167+ */
168+ private function getStockIndexTableProvider ()
169+ {
170+ if (null === $ this ->stockIndexTableProvider ) {
171+ try {
172+ $ this ->stockIndexTableProvider = $ this ->objectManager ->get (
173+ \Magento \InventoryIndexer \Model \StockIndexTableNameResolverInterface::class
174+ );
175+ } catch (\Exception $ exception ) {
176+ $ message = 'Failed to fetch the MSI stock table resolver despite the fact MSI implementation was considered as usable. ' ;
177+ throw new \Magento \Framework \Exception \RuntimeException (__ ($ message . $ exception ->getMessage ()));
178+ }
179+ }
180+
181+ return $ this ->stockIndexTableProvider ;
182+ }
134183}
0 commit comments