11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2022 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
88namespace Magento \InventoryElasticsearch \Plugin \Model \Adapter \BatchDataMapper ;
99
1010use Magento \Elasticsearch \Model \Adapter \BatchDataMapper \ProductDataMapper ;
1111use Magento \Framework \Exception \NoSuchEntityException ;
12+ use Magento \InventoryCatalogApi \Model \GetSkusByProductIdsInterface ;
13+ use Magento \InventorySalesApi \Model \GetStockItemsDataInterface ;
1214use Magento \InventorySalesApi \Model \StockByWebsiteIdResolverInterface ;
1315use Magento \Store \Api \StoreRepositoryInterface ;
14- use Magento \InventorySalesApi \Model \GetStockItemDataInterface ;
1516
1617class ProductDataMapperPlugin
1718{
18- /**
19- * @var StockByWebsiteIdResolverInterface
20- */
21- private $ stockByWebsiteIdResolver ;
22-
23- /**
24- * @var StoreRepositoryInterface
25- */
26- private $ storeRepository ;
27-
28- /**
29- * @var GetStockItemDataInterface
30- */
31- private $ getStockItemData ;
32-
3319 /**
3420 * @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
3521 * @param StoreRepositoryInterface $storeRepository
36- * @param GetStockItemDataInterface $getStockItemData
22+ * @param GetStockItemsDataInterface $getStockItemsData
23+ * @param GetSkusByProductIdsInterface $getSkusByProductIds
3724 */
3825 public function __construct (
39- StockByWebsiteIdResolverInterface $ stockByWebsiteIdResolver ,
40- StoreRepositoryInterface $ storeRepository ,
41- GetStockItemDataInterface $ getStockItemData
26+ private readonly StockByWebsiteIdResolverInterface $ stockByWebsiteIdResolver ,
27+ private readonly StoreRepositoryInterface $ storeRepository ,
28+ private readonly GetStockItemsDataInterface $ getStockItemsData ,
29+ private readonly GetSkusByProductIdsInterface $ getSkusByProductIds
4230 ) {
43- $ this ->stockByWebsiteIdResolver = $ stockByWebsiteIdResolver ;
44- $ this ->storeRepository = $ storeRepository ;
45- $ this ->getStockItemData = $ getStockItemData ;
4631 }
4732
4833 /**
@@ -64,20 +49,11 @@ public function afterMap(
6449 ): array {
6550 $ store = $ this ->storeRepository ->getById ($ storeId );
6651 $ stock = $ this ->stockByWebsiteIdResolver ->execute ((int )$ store ->getWebsiteId ());
67-
52+ $ skus = $ this ->getSkusByProductIds ->execute (array_keys ($ documents ));
53+ $ stockItems = $ this ->getStockItemsData ->execute ($ skus , $ stock ->getStockId ());
6854 foreach ($ documents as $ productId => $ document ) {
69- $ sku = $ document ['sku ' ] ?? '' ;
70- if (!$ sku ) {
71- $ document ['is_out_of_stock ' ] = 1 ;
72- } else {
73- try {
74- $ stockItemData = $ this ->getStockItemData ->execute ($ sku , $ stock ->getStockId ());
75- } catch (NoSuchEntityException $ e ) {
76- $ stockItemData = null ;
77- }
78- $ document ['is_out_of_stock ' ] = null !== $ stockItemData
79- ? (int )$ stockItemData [GetStockItemDataInterface::IS_SALABLE ] : 1 ;
80- }
55+ $ sku = $ skus [$ productId ];
56+ $ document ['is_out_of_stock ' ] = (int )!($ stockItems [$ sku ][GetStockItemsDataInterface::IS_SALABLE ] ?? 0 );
8157 $ documents [$ productId ] = $ document ;
8258 }
8359
0 commit comments