![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Ecombricks/InventoryCommon/Model/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ declare(strict_types=1); namespace Ecombricks\InventoryCommon\Model; /** * Get stock ID by source code */ class GetStockIdBySourceCode { /** * Search criteria builder * * @var \Magento\Framework\Api\SearchCriteriaBuilder */ private $searchCriteriaBuilder; /** * Get stock source links * * @var \Magento\InventoryApi\Api\GetStockSourceLinksInterface */ private $getStockSourceLinks; /** * Default stock provider * * @var \Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface */ private $defaultStockProvider; /** * Stock IDs * * @var array */ private $stockIds = []; /** * Constructor * * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder * @param \Magento\InventoryApi\Api\GetStockSourceLinksInterface $getStockSourceLinks * @param \Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface $defaultStockProvider * @return void */ public function __construct( \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\InventoryApi\Api\GetStockSourceLinksInterface $getStockSourceLinks, \Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface $defaultStockProvider ) { $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->getStockSourceLinks = $getStockSourceLinks; $this->defaultStockProvider = $defaultStockProvider; } /** * Execute * * @param string $sourceCode * @return int */ public function execute(string $sourceCode): int { if (array_key_exists($sourceCode, $this->stockIds)) { return $this->stockIds[$sourceCode]; } $searchCriteria = $this->searchCriteriaBuilder ->addFilter(\Magento\InventoryApi\Api\Data\StockSourceLinkInterface::SOURCE_CODE, $sourceCode) ->create(); $stockSourceLinks = $this->getStockSourceLinks->execute($searchCriteria)->getItems(); if (!empty($stockSourceLinks)) { $stockSourceLink = current($stockSourceLinks); $stockId = (int) $stockSourceLink->getStockId(); } else { $stockId = (int) $this->defaultStockProvider->getId(); } return $this->stockIds[$sourceCode] = $stockId; } }