-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathGetInStockSourceItemsBySkusAndSortedSource.php
72 lines (63 loc) · 2.17 KB
/
GetInStockSourceItemsBySkusAndSortedSource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventorySourceSelectionApi\Model;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
/**
* Retrieve source items for a defined set of skus and sorted source codes
*
* Useful for determining presence in stock and source selection
*
* @api
*/
class GetInStockSourceItemsBySkusAndSortedSource
{
/**
* @var SourceItemRepositoryInterface
*/
private $sourceItemRepository;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @param SourceItemRepositoryInterface $sourceItemRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function __construct(
SourceItemRepositoryInterface $sourceItemRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->sourceItemRepository = $sourceItemRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
/**
* Retrieve source items for a defined set of SKUs and sorted source codes
*
* @param array $skus
* @param array $sortedSourceCodes
* @return SourceItemInterface[]
*/
public function execute(array $skus, array $sortedSourceCodes): array
{
$skus = array_map('strval', $skus);
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(SourceItemInterface::SKU, $skus, 'in')
->addFilter(SourceItemInterface::SOURCE_CODE, $sortedSourceCodes, 'in')
->addFilter(SourceItemInterface::STATUS, SourceItemInterface::STATUS_IN_STOCK)
->create();
$items = $this->sourceItemRepository->getList($searchCriteria)->getItems();
$itemsSorting = [];
foreach ($items as $item) {
$itemsSorting[] = array_search($item->getSourceCode(), $sortedSourceCodes, true);
}
array_multisort($itemsSorting, SORT_NUMERIC, SORT_ASC, $items);
return $items;
}
}