|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Observers\ProductInventoryObserver |
|
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 2016 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 |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Utils\ColumnKeys; |
|
24
|
|
|
use TechDivision\Import\Product\Utils\MemberNames; |
|
25
|
|
|
use TechDivision\Import\Observers\AttributeLoaderInterface; |
|
26
|
|
|
use TechDivision\Import\Observers\DynamicAttributeObserverInterface; |
|
27
|
|
|
use TechDivision\Import\Product\Services\ProductBunchProcessorInterface; |
|
28
|
|
|
use TechDivision\Import\Utils\BackendTypeKeys; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Observer that creates/updates the product's inventory. |
|
32
|
|
|
* |
|
33
|
|
|
* @author Tim Wagner <[email protected]> |
|
34
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
36
|
|
|
* @link https://github.com/techdivision/import-product |
|
37
|
|
|
* @link http://www.techdivision.com |
|
38
|
|
|
*/ |
|
39
|
|
|
class ProductInventoryObserver extends AbstractProductImportObserver implements DynamicAttributeObserverInterface |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The product bunch processor instance. |
|
44
|
|
|
* |
|
45
|
|
|
* @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $productBunchProcessor; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The attribute loader instance. |
|
51
|
|
|
* |
|
52
|
|
|
* @var \TechDivision\Import\Observers\AttributeLoaderInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $attributeLoader; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Initialize the observer with the passed product bunch processor instance. |
|
58
|
|
|
* |
|
59
|
|
|
* @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance |
|
60
|
|
|
* @param \TechDivision\Import\Observers\AttributeLoaderInterface $attributeLoader The attribute loader instance |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function __construct( |
|
63
|
|
|
ProductBunchProcessorInterface $productBunchProcessor, |
|
64
|
|
|
AttributeLoaderInterface $attributeLoader |
|
65
|
|
|
) { |
|
66
|
2 |
|
$this->productBunchProcessor = $productBunchProcessor; |
|
67
|
2 |
|
$this->attributeLoader = $attributeLoader; |
|
68
|
2 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return's the product bunch processor instance. |
|
72
|
|
|
* |
|
73
|
|
|
* @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
|
74
|
|
|
*/ |
|
75
|
1 |
|
protected function getProductBunchProcessor() |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->productBunchProcessor; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Process the observer's business logic. |
|
82
|
|
|
* |
|
83
|
|
|
* @return array The processed row |
|
84
|
|
|
*/ |
|
85
|
2 |
|
protected function process() |
|
86
|
|
|
{ |
|
87
|
|
|
|
|
88
|
|
|
// query whether or not, we've found a new SKU => means we've found a new product |
|
89
|
2 |
|
if ($this->hasBeenProcessed($this->getValue(ColumnKeys::SKU))) { |
|
90
|
1 |
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// prepare, initialize and persist the stock status/item |
|
94
|
1 |
|
$this->persistStockItem($this->initializeStockItem($this->prepareStockItemAttributes())); |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Prepare the basic attributes of the stock status/item entity that has to be persisted. |
|
99
|
|
|
* |
|
100
|
|
|
* @return array The prepared stock status/item attributes |
|
101
|
|
|
*/ |
|
102
|
1 |
|
protected function prepareAttributes() |
|
103
|
|
|
{ |
|
104
|
|
|
|
|
105
|
|
|
// load the ID of the product that has been created recently |
|
106
|
1 |
|
$lastEntityId = $this->getSubject()->getLastEntityId(); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
// initialize the stock status data |
|
109
|
1 |
|
$websiteId = $this->getValue(ColumnKeys::WEBSITE_ID, 0); |
|
110
|
|
|
|
|
111
|
|
|
// return the prepared stock status |
|
112
|
1 |
|
return $this->initializeEntity( |
|
113
|
|
|
array( |
|
114
|
1 |
|
MemberNames::PRODUCT_ID => $lastEntityId, |
|
115
|
1 |
|
MemberNames::WEBSITE_ID => $websiteId, |
|
116
|
1 |
|
MemberNames::STOCK_ID => 1 |
|
117
|
|
|
) |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Prepare the stock item attributes of the entity that has to be persisted. |
|
123
|
|
|
* |
|
124
|
|
|
* @return array The prepared stock status item |
|
125
|
|
|
*/ |
|
126
|
1 |
|
protected function prepareStockItemAttributes() |
|
127
|
|
|
{ |
|
128
|
1 |
|
return array_merge($this->prepareAttributes(), $this->attributeLoader->load($this, $this->getHeaderStockMappings())); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Initialize the stock item with the passed attributes and returns an instance. |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $attr The stock item attributes |
|
135
|
|
|
* |
|
136
|
|
|
* @return array The initialized stock item |
|
137
|
|
|
*/ |
|
138
|
1 |
|
protected function initializeStockItem(array $attr) |
|
139
|
|
|
{ |
|
140
|
1 |
|
return $attr; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Return's the appings for the table column => CSV column header. |
|
145
|
|
|
* |
|
146
|
|
|
* @return array The header stock mappings |
|
147
|
|
|
*/ |
|
148
|
1 |
|
protected function getHeaderStockMappings() |
|
149
|
|
|
{ |
|
150
|
1 |
|
return $this->getSubject()->getHeaderStockMappings(); |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Persist's the passed stock item data and return's the ID. |
|
155
|
|
|
* |
|
156
|
|
|
* @param array $stockItem The stock item data to persist |
|
157
|
|
|
* |
|
158
|
|
|
* @return void |
|
159
|
|
|
*/ |
|
160
|
1 |
|
protected function persistStockItem($stockItem) |
|
161
|
|
|
{ |
|
162
|
1 |
|
$this->getProductBunchProcessor()->persistStockItem($stockItem); |
|
163
|
1 |
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: