GetAssignedStockIdsBySku.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySalesAdminUi\Model\ResourceModel;
  8. use Magento\InventoryApi\Api\GetSourceItemsBySkuInterface;
  9. use Magento\InventorySalesAdminUi\Model\GetStockSourceLinksBySourceCode;
  10. /**
  11. * Get all stocks Ids by sku
  12. */
  13. class GetAssignedStockIdsBySku
  14. {
  15. /**
  16. * @var GetSourceItemsBySkuInterface
  17. */
  18. private $getSourceItemsBySku;
  19. /**
  20. * @var GetStockSourceLinksBySourceCode
  21. */
  22. private $getStockSourceLinksBySourceCode;
  23. /**
  24. * @param GetSourceItemsBySkuInterface $getSourceItemsBySku
  25. * @param GetStockSourceLinksBySourceCode $getStockSourceLinksBySourceCode
  26. */
  27. public function __construct(
  28. GetSourceItemsBySkuInterface $getSourceItemsBySku,
  29. GetStockSourceLinksBySourceCode $getStockSourceLinksBySourceCode
  30. ) {
  31. $this->getSourceItemsBySku = $getSourceItemsBySku;
  32. $this->getStockSourceLinksBySourceCode = $getStockSourceLinksBySourceCode;
  33. }
  34. /**
  35. * @param string $sku
  36. * @return array
  37. */
  38. public function execute(string $sku): array
  39. {
  40. $sourceItems = $this->getSourceItemsBySku->execute($sku);
  41. $stocksIds = [];
  42. foreach ($sourceItems as $sourceItem) {
  43. $stockSourceLinks = $this->getStockSourceLinksBySourceCode->execute($sourceItem->getSourceCode());
  44. foreach ($stockSourceLinks as $stockSourceLink) {
  45. $stocksIds[] = (int)$stockSourceLink->getStockId();
  46. }
  47. }
  48. return array_unique($stocksIds);
  49. }
  50. }