|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace StorefrontX\CatalogGraphQlExtended\Model\Resolver\DataProvider; |
| 5 | + |
| 6 | +use Magento\InventoryApi\Api\GetSourceItemsBySkuInterface; |
| 7 | +use Magento\InventoryApi\Api\SourceRepositoryInterface; |
| 8 | +use Psr\Log\LoggerInterface; |
| 9 | + |
| 10 | +class MSI |
| 11 | +{ |
| 12 | + |
| 13 | + private GetSourceItemsBySkuInterface $sourceItemsBySku; |
| 14 | + protected LoggerInterface $logger; |
| 15 | + protected SourceRepositoryInterface $sourceRepository; |
| 16 | + |
| 17 | + public function __construct( |
| 18 | + GetSourceItemsBySkuInterface $sourceItemsBySku, |
| 19 | + SourceRepositoryInterface $sourceRepository, |
| 20 | + LoggerInterface $logger |
| 21 | + ) |
| 22 | + { |
| 23 | + $this->sourceItemsBySku = $sourceItemsBySku; |
| 24 | + $this->sourceRepository = $sourceRepository; |
| 25 | + $this->logger = $logger; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @params string $sku |
| 30 | + * this function return all the MSI Data for product sku |
| 31 | + * @returns array |
| 32 | + */ |
| 33 | + public function getMSI(string $sku): array |
| 34 | + { |
| 35 | + return $this->getSourceItemBySku($sku); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @params string $sku |
| 40 | + * this function return all the MSI Data for product sku |
| 41 | + * @returns array |
| 42 | + */ |
| 43 | + public function getSourceItemBySku($sku): array |
| 44 | + { |
| 45 | + |
| 46 | + $sources = []; |
| 47 | + $sourceData = $this->sourceRepository->getList(); |
| 48 | + $productSourceData = $this->sourceItemsBySku->execute($sku); |
| 49 | + |
| 50 | + foreach($productSourceData as $source) { |
| 51 | + |
| 52 | + $sourceName = null; |
| 53 | + $latitude = null; |
| 54 | + $longitude = null; |
| 55 | + $country_id = null; |
| 56 | + $street = null; |
| 57 | + $postcode = null; |
| 58 | + $enabled = null; |
| 59 | + |
| 60 | + foreach($sourceData->getItems() as $so) { |
| 61 | + $sourceCode = $so->getSourceCode(); |
| 62 | + if ($so->getSourceCode() === $source->getSourceCode()) { |
| 63 | + $sourceName = $so->getName(); |
| 64 | + $latitude = $so->getLatitude(); |
| 65 | + $longitude = $so->getLongitude(); |
| 66 | + $country_id = $so->getCountryId(); |
| 67 | + $street = $so->getStreet(); |
| 68 | + $postcode = $so->getPostcode(); |
| 69 | + $enabled = $so->isEnabled(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + $sources[] = ['source_code' => $source->getSourceCode(), |
| 74 | + 'quantity' => $source->getQuantity(), |
| 75 | + 'sku' => $source->getSku(), |
| 76 | + 'status' => $source->getStatus(), |
| 77 | + 'name' => $sourceName, |
| 78 | + 'latitude' => $latitude, |
| 79 | + 'longitude' => $longitude, |
| 80 | + 'country_id' => $country_id, |
| 81 | + 'street' => $street, |
| 82 | + 'postcode' => $postcode, |
| 83 | + 'enabled' => $enabled |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + return $sources; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments