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
|
|
|
* Process the observer's business logic. |
55
|
|
|
* |
56
|
|
|
* @return array The processed row |
57
|
|
|
*/ |
58
|
|
|
protected function process() |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
// load and map the parent + child ID |
62
|
|
|
$this->parentId = $this->mapParentSku($parentSku = $this->getValue(ColumnKeys::VARIANT_PARENT_SKU)); |
63
|
|
|
$this->childId = $this->mapChildSku($childSku = $this->getValue(ColumnKeys::VARIANT_CHILD_SKU)); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
// prepare and persist the product relation |
67
|
|
|
if ($productRelation = $this->initializeProductRelation($this->prepareProductRelationAttributes())) { |
68
|
|
|
$this->persistProductRelation($productRelation); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// prepare and persist the product super link |
72
|
|
|
if ($productSuperLink = $this->initializeProductSuperLink($this->prepareProductSuperLinkAttributes())) { |
73
|
|
|
$this->persistProductSuperLink($productSuperLink); |
74
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
} catch (\Exception $e) { |
77
|
|
|
// prepare a more detailed error message |
78
|
|
|
$message = sprintf( |
79
|
|
|
'Product relation with SKUs %s => %s can\'t be created in file %s on line %d', |
80
|
|
|
$parentSku, |
81
|
|
|
$childSku, |
82
|
|
|
$this->getFilename(), |
83
|
|
|
$this->getLineNumber() |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
// query whether or not, debug mode is enabled |
87
|
|
|
if ($this->isDebugMode()) { |
88
|
|
|
// log a warning and return immediately |
89
|
|
|
$this->getSystemLogger()->warning($message); |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// if we're NOT in debug mode, re-throw a more detailed exception |
94
|
|
|
throw new \Exception($message, null, $e); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Prepare the product relation attributes that has to be persisted. |
100
|
|
|
* |
101
|
|
|
* @return array The prepared product relation attributes |
102
|
|
|
*/ |
103
|
|
|
protected function prepareProductRelationAttributes() |
104
|
|
|
{ |
105
|
|
|
|
106
|
|
|
// initialize and return the entity |
107
|
|
|
return $this->initializeEntity( |
108
|
|
|
array( |
109
|
|
|
MemberNames::PARENT_ID => $this->parentId, |
110
|
|
|
MemberNames::CHILD_ID => $this->childId |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Prepare the product super link attributes that has to be persisted. |
117
|
|
|
* |
118
|
|
|
* @return array The prepared product super link attributes |
119
|
|
|
*/ |
120
|
|
|
protected function prepareProductSuperLinkAttributes() |
121
|
|
|
{ |
122
|
|
|
|
123
|
|
|
// initialize and return the entity |
124
|
|
|
return $this->initializeEntity( |
125
|
|
|
array( |
126
|
|
|
MemberNames::PRODUCT_ID => $this->childId, |
127
|
|
|
MemberNames::PARENT_ID => $this->parentId |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Initialize the product relation with the passed attributes and returns an instance. |
134
|
|
|
* |
135
|
|
|
* @param array $attr The product relation attributes |
136
|
|
|
* |
137
|
|
|
* @return array|null The initialized product relation, or null if the relation already exsist |
138
|
|
|
*/ |
139
|
|
|
protected function initializeProductRelation(array $attr) |
140
|
|
|
{ |
141
|
|
|
return $attr; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Initialize the product super link with the passed attributes and returns an instance. |
146
|
|
|
* |
147
|
|
|
* @param array $attr The product super link attributes |
148
|
|
|
* |
149
|
|
|
* @return array|null The initialized product super link, or null if the super link already exsist |
150
|
|
|
*/ |
151
|
|
|
protected function initializeProductSuperLink(array $attr) |
152
|
|
|
{ |
153
|
|
|
return $attr; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Map's the passed SKU of the parent product to it's PK. |
158
|
|
|
* |
159
|
|
|
* @param string $parentSku The SKU of the parent product |
160
|
|
|
* |
161
|
|
|
* @return integer The primary key used to create relations |
162
|
|
|
*/ |
163
|
|
|
protected function mapParentSku($parentSku) |
164
|
|
|
{ |
165
|
|
|
return $this->mapSkuToEntityId($parentSku); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Map's the passed SKU of the child product to it's PK. |
170
|
|
|
* |
171
|
|
|
* @param string $childSku The SKU of the child product |
172
|
|
|
* |
173
|
|
|
* @return integer The primary key used to create relations |
174
|
|
|
*/ |
175
|
|
|
protected function mapChildSku($childSku) |
176
|
|
|
{ |
177
|
|
|
return $this->mapSkuToEntityId($childSku); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Return the entity ID for the passed SKU. |
182
|
|
|
* |
183
|
|
|
* @param string $sku The SKU to return the entity ID for |
184
|
|
|
* |
185
|
|
|
* @return integer The mapped entity ID |
186
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
187
|
|
|
*/ |
188
|
|
|
protected function mapSkuToEntityId($sku) |
189
|
|
|
{ |
190
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Persist's the passed product relation data and return's the ID. |
195
|
|
|
* |
196
|
|
|
* @param array $productRelation The product relation data to persist |
197
|
|
|
* |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
protected function persistProductRelation($productRelation) |
201
|
|
|
{ |
202
|
|
|
return $this->getSubject()->persistProductRelation($productRelation); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Persist's the passed product super link data and return's the ID. |
207
|
|
|
* |
208
|
|
|
* @param array $productSuperLink The product super link data to persist |
209
|
|
|
* |
210
|
|
|
* @return void |
211
|
|
|
*/ |
212
|
|
|
protected function persistProductSuperLink($productSuperLink) |
213
|
|
|
{ |
214
|
|
|
return $this->getSubject()->persistProductSuperLink($productSuperLink); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|