1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Variant\Observers\VariantObserver |
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-variant |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Variant\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Variant\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Product\Variant\Utils\MemberNames; |
25
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Oberserver that provides functionality for the product variant replace operation. |
29
|
|
|
* |
30
|
|
|
* @author Tim Wagner <[email protected]> |
31
|
|
|
* @copyright 2016 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-variant |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
class VariantObserver extends AbstractProductImportObserver |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The product relation's parent ID. |
41
|
|
|
* |
42
|
|
|
* @var integer |
43
|
|
|
*/ |
44
|
|
|
protected $parentId; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The product relation's child ID. |
48
|
|
|
* |
49
|
|
|
* @var integer |
50
|
|
|
*/ |
51
|
|
|
protected $childId; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Will be invoked by the action on the events the listener has been registered for. |
55
|
|
|
* |
56
|
|
|
* @param array $row The row to handle |
57
|
|
|
* |
58
|
|
|
* @return array The modified row |
59
|
|
|
* @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle() |
60
|
|
|
*/ |
61
|
|
|
public function handle(array $row) |
62
|
|
|
{ |
63
|
|
|
|
64
|
|
|
// initialize the row |
65
|
|
|
$this->setRow($row); |
|
|
|
|
66
|
|
|
|
67
|
|
|
// process the functionality and return the row |
68
|
|
|
$this->process(); |
69
|
|
|
|
70
|
|
|
// return the processed row |
71
|
|
|
return $this->getRow(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Process the observer's business logic. |
76
|
|
|
* |
77
|
|
|
* @return array The processed row |
78
|
|
|
*/ |
79
|
|
|
protected function process() |
80
|
|
|
{ |
81
|
|
|
|
82
|
|
|
// load and map the parent + child ID |
83
|
|
|
$this->parentId = $this->mapParentSku($this->getValue(ColumnKeys::VARIANT_PARENT_SKU)); |
|
|
|
|
84
|
|
|
$this->childId = $this->mapChildSku($this->getValue(ColumnKeys::VARIANT_CHILD_SKU)); |
|
|
|
|
85
|
|
|
|
86
|
|
|
// prepare and persist the product relation |
87
|
|
|
if ($productRelation = $this->initializeProductRelation($this->prepareProductRelationAttributes())) { |
88
|
|
|
$this->persistProductRelation($productRelation); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// prepare and persist the product super link |
92
|
|
|
if ($productSuperLink = $this->initializeProductSuperLink($this->prepareProductSuperLinkAttributes())) { |
93
|
|
|
$this->persistProductSuperLink($productSuperLink); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Prepare the product relation attributes that has to be persisted. |
99
|
|
|
* |
100
|
|
|
* @return array The prepared product relation attributes |
101
|
|
|
*/ |
102
|
|
|
protected function prepareProductRelationAttributes() |
103
|
|
|
{ |
104
|
|
|
|
105
|
|
|
// initialize and return the entity |
106
|
|
|
return $this->initializeEntity( |
|
|
|
|
107
|
|
|
array( |
108
|
|
|
MemberNames::PARENT_ID => $this->parentId, |
109
|
|
|
MemberNames::CHILD_ID => $this->childId |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Prepare the product super link attributes that has to be persisted. |
116
|
|
|
* |
117
|
|
|
* @return array The prepared product super link attributes |
118
|
|
|
*/ |
119
|
|
|
protected function prepareProductSuperLinkAttributes() |
120
|
|
|
{ |
121
|
|
|
|
122
|
|
|
// initialize and return the entity |
123
|
|
|
return $this->initializeEntity( |
|
|
|
|
124
|
|
|
array( |
125
|
|
|
MemberNames::PRODUCT_ID => $this->childId, |
126
|
|
|
MemberNames::PARENT_ID => $this->parentId |
127
|
|
|
) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Initialize the product relation with the passed attributes and returns an instance. |
133
|
|
|
* |
134
|
|
|
* @param array $attr The product relation attributes |
135
|
|
|
* |
136
|
|
|
* @return array|null The initialized product relation, or null if the relation already exsist |
137
|
|
|
*/ |
138
|
|
|
protected function initializeProductRelation(array $attr) |
139
|
|
|
{ |
140
|
|
|
return $attr; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Initialize the product super link with the passed attributes and returns an instance. |
145
|
|
|
* |
146
|
|
|
* @param array $attr The product super link attributes |
147
|
|
|
* |
148
|
|
|
* @return array|null The initialized product super link, or null if the super link already exsist |
149
|
|
|
*/ |
150
|
|
|
protected function initializeProductSuperLink(array $attr) |
151
|
|
|
{ |
152
|
|
|
return $attr; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Map's the passed SKU of the parent product to it's PK. |
157
|
|
|
* |
158
|
|
|
* @param string $parentSku The SKU of the parent product |
159
|
|
|
* |
160
|
|
|
* @return integer The primary key used to create relations |
161
|
|
|
*/ |
162
|
|
|
protected function mapParentSku($parentSku) |
163
|
|
|
{ |
164
|
|
|
return $this->mapSkuToEntityId($parentSku); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Map's the passed SKU of the child product to it's PK. |
169
|
|
|
* |
170
|
|
|
* @param string $childSku The SKU of the child product |
171
|
|
|
* |
172
|
|
|
* @return integer The primary key used to create relations |
173
|
|
|
*/ |
174
|
|
|
protected function mapChildSku($childSku) |
175
|
|
|
{ |
176
|
|
|
return $this->mapSkuToEntityId($childSku); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Return the entity ID for the passed SKU. |
181
|
|
|
* |
182
|
|
|
* @param string $sku The SKU to return the entity ID for |
183
|
|
|
* |
184
|
|
|
* @return integer The mapped entity ID |
185
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
186
|
|
|
*/ |
187
|
|
|
protected function mapSkuToEntityId($sku) |
188
|
|
|
{ |
189
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Persist's the passed product relation data and return's the ID. |
194
|
|
|
* |
195
|
|
|
* @param array $productRelation The product relation data to persist |
196
|
|
|
* |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
protected function persistProductRelation($productRelation) |
200
|
|
|
{ |
201
|
|
|
return $this->getSubject()->persistProductRelation($productRelation); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Persist's the passed product super link data and return's the ID. |
206
|
|
|
* |
207
|
|
|
* @param array $productSuperLink The product super link data to persist |
208
|
|
|
* |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
protected function persistProductSuperLink($productSuperLink) |
212
|
|
|
{ |
213
|
|
|
return $this->getSubject()->persistProductSuperLink($productSuperLink); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.