|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogInventory\Test\Fixture; |
| 9 | + |
| 10 | +use Magento\Framework\DataObject; |
| 11 | +use Magento\InventoryApi\Api\Data\SourceItemInterface; |
| 12 | +use Magento\InventoryApi\Api\SourceItemsDeleteInterface; |
| 13 | +use Magento\InventoryApi\Api\SourceItemsSaveInterface; |
| 14 | +use Magento\TestFramework\Fixture\Api\DataMerger; |
| 15 | +use Magento\TestFramework\Fixture\Api\ServiceFactory; |
| 16 | +use Magento\TestFramework\Fixture\Data\ProcessorInterface; |
| 17 | +use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface; |
| 18 | + |
| 19 | +class SourceItem implements RevertibleDataFixtureInterface |
| 20 | +{ |
| 21 | + private const DEFAULT_DATA = [ |
| 22 | + 'sku' => 'SKU-%uniqid%', |
| 23 | + 'source_code' => 'Source%uniqid%', |
| 24 | + 'quantity' => 5, |
| 25 | + 'status' => SourceItemInterface::STATUS_IN_STOCK, |
| 26 | + ]; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ServiceFactory |
| 30 | + */ |
| 31 | + private ServiceFactory $serviceFactory; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ProcessorInterface |
| 35 | + */ |
| 36 | + private ProcessorInterface $dataProcessor; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var DataMerger |
| 40 | + */ |
| 41 | + private DataMerger $dataMerger; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param ServiceFactory $serviceFactory |
| 45 | + * @param ProcessorInterface $dataProcessor |
| 46 | + * @param DataMerger $dataMerger |
| 47 | + */ |
| 48 | + public function __construct( |
| 49 | + ServiceFactory $serviceFactory, |
| 50 | + ProcessorInterface $dataProcessor, |
| 51 | + DataMerger $dataMerger |
| 52 | + ) { |
| 53 | + $this->serviceFactory = $serviceFactory; |
| 54 | + $this->dataProcessor = $dataProcessor; |
| 55 | + $this->dataMerger = $dataMerger; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param array $data |
| 60 | + * @return DataObject|null |
| 61 | + */ |
| 62 | + public function apply(array $data = []): ?DataObject |
| 63 | + { |
| 64 | + $service = $this->serviceFactory->create(SourceItemsSaveInterface::class, 'execute'); |
| 65 | + |
| 66 | + return $service->execute(['sourceItems' => [$this->prepareData($data)]]); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @inheritdoc |
| 71 | + */ |
| 72 | + public function revert(DataObject $data): void |
| 73 | + { |
| 74 | + $service = $this->serviceFactory->create(SourceItemsDeleteInterface::class, 'execute'); |
| 75 | + $service->execute( |
| 76 | + [ |
| 77 | + 'sourceItems' => [$this->prepareData($data->getData())] |
| 78 | + ] |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Prepare product data |
| 84 | + * |
| 85 | + * @param array $data |
| 86 | + * @return array |
| 87 | + */ |
| 88 | + private function prepareData(array $data): array |
| 89 | + { |
| 90 | + $data = $this->dataMerger->merge(self::DEFAULT_DATA, $data, false); |
| 91 | + |
| 92 | + return $this->dataProcessor->process($this, $data); |
| 93 | + } |
| 94 | +} |
0 commit comments