1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Msi\Observers\InventorySourceItemObserver |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import-product-msi |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Msi\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Msi\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Product\Msi\Utils\MemberNames; |
25
|
|
|
use TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Observer that prepares the MSI source item information found in the CSV file. |
29
|
|
|
* |
30
|
|
|
* @author Tim Wagner <[email protected]> |
31
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
33
|
|
|
* @link https://github.com/techdivision/import-product-msi |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
class InventorySourceItemObserver extends AbstractMsiImportObserver |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The MSI bunch processor instance. |
41
|
|
|
* |
42
|
|
|
* @var \TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface |
43
|
|
|
*/ |
44
|
|
|
protected $msiBunchProcessor; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initialize the observer with the passed MSI bunch processor instance. |
48
|
|
|
* |
49
|
|
|
* @param \TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface $msiBunchProcessor The MSI bunch processor instance |
50
|
|
|
*/ |
51
|
|
|
public function __construct(MsiBunchProcessorInterface $msiBunchProcessor) |
52
|
|
|
{ |
53
|
|
|
$this->msiBunchProcessor = $msiBunchProcessor; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return's the MSI bunch processor instance. |
58
|
|
|
* |
59
|
|
|
* @return \TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface The MSI bunch processor instance |
60
|
|
|
*/ |
61
|
|
|
protected function getMsiBunchProcessor() |
62
|
|
|
{ |
63
|
|
|
return $this->msiBunchProcessor; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Process the observer's business logic. |
68
|
|
|
* |
69
|
|
|
* @return array The processed row |
70
|
|
|
*/ |
71
|
|
|
protected function process() |
72
|
|
|
{ |
73
|
|
|
|
74
|
|
|
// load SKU and inventory source code |
75
|
|
|
$sku = $this->getValue(ColumnKeys::SKU); |
76
|
|
|
$sourceCode = $this->getValue(ColumnKeys::SOURCE_CODE); |
77
|
|
|
|
78
|
|
|
// query whether or not, we've found a new SKU/source code combination => means we've found a inventory source item |
79
|
|
|
if ($this->hasBeenProcessed($sku, $sourceCode)) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// query whether or not the inventory source is with the given code is avaiable |
84
|
|
|
if ($this->hasInventorySource($sourceCode)) { |
85
|
|
|
// create the MSI inventory source item |
86
|
|
|
$inventorySourceItem = $this->initializeInventorySourceItem($this->prepareAttributes()); |
87
|
|
|
$this->persistInventorySourceItem($inventorySourceItem); |
88
|
|
|
|
89
|
|
|
// finaly, add the SKU + source code => source item ID mapping |
90
|
|
|
$this->addSkuSourceItemIdMapping($sku, $sourceCode); |
91
|
|
|
} else { |
92
|
|
|
// throw a new exception |
93
|
|
|
throw new \Exception( |
94
|
|
|
$this->appendExceptionSuffix( |
|
|
|
|
95
|
|
|
sprintf('Can\'t load inventory source with code "%s"', $sourceCode) |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
103
|
|
|
* |
104
|
|
|
* @return array The prepared attributes |
105
|
|
|
*/ |
106
|
|
|
protected function prepareAttributes() |
107
|
|
|
{ |
108
|
|
|
|
109
|
|
|
// load the MSI inventory source item values |
110
|
|
|
$sku = $this->getValue(ColumnKeys::SKU); |
111
|
|
|
$status = $this->getValue(ColumnKeys::STATUS); |
112
|
|
|
$sourceCode = $this->getValue(ColumnKeys::SOURCE_CODE); |
113
|
|
|
$quantity = $this->getValue(ColumnKeys::QUANTITY); |
114
|
|
|
|
115
|
|
|
// return the prepared MSI inventory source item |
116
|
|
|
return $this->initializeEntity( |
117
|
|
|
array( |
118
|
|
|
MemberNames::SKU => $sku, |
119
|
|
|
MemberNames::STATUS => $status, |
120
|
|
|
MemberNames::SOURCE_CODE => $sourceCode, |
121
|
|
|
MemberNames::QUANTITY => $quantity |
122
|
|
|
) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Initialize the MSI inventory source item with the passed attributes and returns an instance. |
128
|
|
|
* |
129
|
|
|
* @param array $attr The inventory source item attributes |
130
|
|
|
* |
131
|
|
|
* @return array The initialized inventory source item |
132
|
|
|
* @throws \RuntimeException Is thrown, if the attributes can not be initialized |
133
|
|
|
*/ |
134
|
|
|
protected function initializeInventorySourceItem(array $attr) |
135
|
|
|
{ |
136
|
|
|
return $attr; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Queries whether or not the inventory source with the passed code is available. |
141
|
|
|
* |
142
|
|
|
* @param string $sourceCode The code of the inventory source to query for |
143
|
|
|
* |
144
|
|
|
* @return boolean TRUE if the inventory source with the passed code is available, FALSE if not |
145
|
|
|
*/ |
146
|
|
|
protected function hasInventorySource($sourceCode) |
147
|
|
|
{ |
148
|
|
|
return $this->getSubject()->hasInventorySource($sourceCode); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Add the passed SKU/source code => source item ID mapping. |
153
|
|
|
* |
154
|
|
|
* @param string $sku The SKU |
155
|
|
|
* @param string $sourceCode The source code |
156
|
|
|
* |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
protected function addSkuSourceItemIdMapping($sku, $sourceCode) |
160
|
|
|
{ |
161
|
|
|
$this->getSubject()->addSkuSourceItemIdMapping($sku, $sourceCode); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Persist's the passed inventory source item data. |
166
|
|
|
* |
167
|
|
|
* @param array $inventorySourceItem The inventory source item data to persist |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
protected function persistInventorySourceItem($inventorySourceItem) |
172
|
|
|
{ |
173
|
|
|
$this->getMsiBunchProcessor()->persistInventorySourceItem($inventorySourceItem); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.