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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryLowQuantityNotificationAdminUi\Model;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Exception\InputException;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryLowQuantityNotificationApi\Api\Data\SourceItemConfigurationInterfaceFactory;
use Magento\InventoryLowQuantityNotificationApi\Api\Data\SourceItemConfigurationInterface;
use Magento\InventoryLowQuantityNotificationApi\Api\DeleteSourceItemsConfigurationInterface;
use Magento\InventoryLowQuantityNotificationApi\Api\SourceItemConfigurationsSaveInterface;
/**
* Process source item configuration.
*/
class SourceItemsConfigurationProcessor
{
/**
* @var SourceItemConfigurationInterfaceFactory
*/
private $sourceItemConfigurationFactory;
/**
* @var SourceItemConfigurationsSaveInterface
*/
private $sourceItemConfigurationsSave;
/**
* @var DataObjectHelper
*/
private $dataObjectHelper;
/**
* @var DeleteSourceItemsConfigurationInterface
*/
private $sourceItemsConfigurationDelete;
/**
* SourceItemsConfigurationProcessor constructor.
*
* @param SourceItemConfigurationInterfaceFactory $sourceItemConfigurationFactory
* @param SourceItemConfigurationsSaveInterface $sourceItemConfigurationsSave
* @param DeleteSourceItemsConfigurationInterface $sourceItemsConfigurationDelete
* @param DataObjectHelper $dataObjectHelper
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function __construct(
SourceItemConfigurationInterfaceFactory $sourceItemConfigurationFactory,
SourceItemConfigurationsSaveInterface $sourceItemConfigurationsSave,
DeleteSourceItemsConfigurationInterface $sourceItemsConfigurationDelete,
DataObjectHelper $dataObjectHelper
) {
$this->sourceItemConfigurationsSave = $sourceItemConfigurationsSave;
$this->sourceItemConfigurationFactory = $sourceItemConfigurationFactory;
$this->dataObjectHelper = $dataObjectHelper;
$this->sourceItemsConfigurationDelete = $sourceItemsConfigurationDelete;
}
/**
* Process configuration
*
* @param string $sku
* @param array $sourceItemsData
* @return void
* @throws InputException
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function process($sku, array $sourceItemsData)
{
$sourceItemConfigurations = [];
foreach ($sourceItemsData as $sourceItemData) {
$this->validateSourceItemData($sourceItemData);
$sourceItemConfiguration = $this->sourceItemConfigurationFactory->create();
if ($sourceItemData['notify_stock_qty_use_default'] == 1) {
unset($sourceItemData['notify_stock_qty']);
}
$sourceItemData[SourceItemInterface::SKU] = $sku;
$this->dataObjectHelper->populateWithArray(
$sourceItemConfiguration,
$sourceItemData,
SourceItemConfigurationInterface::class
);
$sourceItemConfigurations[] = $sourceItemConfiguration;
}
if (count($sourceItemConfigurations) > 0) {
$this->deleteSourceItemsConfiguration($sourceItemConfigurations);
$this->sourceItemConfigurationsSave->execute($sourceItemConfigurations);
}
}
/**
* Perform validation on source item data
*
* @param array $sourceItemData
* @return void
* @throws InputException
*/
private function validateSourceItemData(array $sourceItemData)
{
if (!isset($sourceItemData[SourceItemInterface::SOURCE_CODE])) {
throw new InputException(__('Wrong Product to Source relation parameters given.'));
}
}
/**
* Delete source items configurations
*
* @param SourceItemInterface[] $sourceItemsConfigurations
* @return void
* @SuppressWarnings(PHPMD.LongVariable)
*/
private function deleteSourceItemsConfiguration(array $sourceItemsConfigurations)
{
/** @var SourceItemInterface $sourceItemConfiguration */
$this->sourceItemsConfigurationDelete->execute($sourceItemsConfigurations);
}
}