1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Customer\Address\Observers\CustomerAddressAttributeObserver |
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-customer-address |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Customer\Address\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Customer\Address\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Customer\Address\Utils\MemberNames; |
25
|
|
|
use TechDivision\Import\Observers\AbstractAttributeObserver; |
26
|
|
|
use TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Observer that creates/updates the customer address's attributes. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/techdivision/import-customer-address |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
class CustomerAddressAttributeObserver extends AbstractAttributeObserver |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The customer address bunch processor instance. |
42
|
|
|
* |
43
|
|
|
* @var \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface |
44
|
|
|
*/ |
45
|
|
|
protected $customerAddressBunchProcessor; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initialize the observer with the passed customer bunch processor instance. |
49
|
|
|
* |
50
|
|
|
* @param \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface $customerAddressBunchProcessor The customer address bunch processor instance |
51
|
|
|
*/ |
52
|
|
|
public function __construct(CustomerAddressBunchProcessorInterface $customerAddressBunchProcessor) |
53
|
|
|
{ |
54
|
|
|
$this->customerAddressBunchProcessor = $customerAddressBunchProcessor; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return's the customer address bunch processor instance. |
59
|
|
|
* |
60
|
|
|
* @return \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface The customer address bunch processor instance |
61
|
|
|
*/ |
62
|
|
|
protected function getCustomerAddressBunchProcessor() |
63
|
|
|
{ |
64
|
|
|
return $this->customerAddressBunchProcessor; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Intializes the existing attributes for the entity with the passed primary key. |
69
|
|
|
* |
70
|
|
|
* @param string $pk The primary key of the entity to load the attributes for |
71
|
|
|
* @param integer $storeId The ID of the store view to load the attributes for |
72
|
|
|
* |
73
|
|
|
* @return array The entity attributes |
74
|
|
|
*/ |
75
|
|
|
protected function getAttributesByPrimaryKeyAndStoreId($pk, $storeId) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$this->attributes = $this->getCustomerAddressBunchProcessor()->getCustomerAddressAttributesByEntityId($pk); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the value(s) of the primary key column(s). As the primary key column can |
82
|
|
|
* also consist of two columns, the return value can be an array also. |
83
|
|
|
* |
84
|
|
|
* @return mixed The primary key value(s) |
85
|
|
|
*/ |
86
|
|
|
protected function getPrimaryKeyValue() |
87
|
|
|
{ |
88
|
|
|
|
89
|
|
|
// initialize the array for the PK values |
90
|
|
|
$pk = array(); |
91
|
|
|
|
92
|
|
|
// prepare the array with the PK values |
93
|
|
|
foreach ($this->getPrimaryKeyColumnName() as $columName) { |
|
|
|
|
94
|
|
|
$pk[] = $this->getValue($columName); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// return the array with the PK values |
98
|
|
|
return $pk; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
103
|
|
|
* |
104
|
|
|
* @return array|null The prepared attributes |
105
|
|
|
*/ |
106
|
|
|
protected function prepareAttributes() |
107
|
|
|
{ |
108
|
|
|
|
109
|
|
|
// laod the callbacks for the actual attribute code |
110
|
|
|
$callbacks = $this->getCallbacksByType($this->attributeCode); |
111
|
|
|
|
112
|
|
|
// invoke the pre-cast callbacks |
113
|
|
|
foreach ($callbacks as $callback) { |
114
|
|
|
$this->attributeValue = $callback->handle($this); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// load the ID of the product that has been created recently |
118
|
|
|
$lastEntityId = $this->getPrimaryKey(); |
119
|
|
|
|
120
|
|
|
// cast the value based on the backend type |
121
|
|
|
$castedValue = $this->castValueByBackendType($this->backendType, $this->attributeValue); |
122
|
|
|
|
123
|
|
|
// prepare the attribute values |
124
|
|
|
return $this->initializeEntity( |
125
|
|
|
array( |
126
|
|
|
$this->getPrimaryKeyMemberName() => $lastEntityId, |
127
|
|
|
MemberNames::ATTRIBUTE_ID => $this->attributeId, |
128
|
|
|
MemberNames::VALUE => $castedValue |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Return's the PK to create the customer => attribute relation. |
135
|
|
|
* |
136
|
|
|
* @return integer The PK to create the relation with |
137
|
|
|
*/ |
138
|
|
|
protected function getPrimaryKey() |
139
|
|
|
{ |
140
|
|
|
return $this->getSubject()->getLastEntityId(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Return's the PK column name to create the customer => attribute relation. |
145
|
|
|
* |
146
|
|
|
* @return string The PK column name |
147
|
|
|
*/ |
148
|
|
|
protected function getPrimaryKeyMemberName() |
149
|
|
|
{ |
150
|
|
|
return MemberNames::ENTITY_ID; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return's the column name that contains the primary key. |
155
|
|
|
* |
156
|
|
|
* @return string the column name that contains the primary key |
157
|
|
|
*/ |
158
|
|
|
protected function getPrimaryKeyColumnName() |
159
|
|
|
{ |
160
|
|
|
return array(ColumnKeys::EMAIL, ColumnKeys::WEBSITE); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Queries whether or not the passed PK and store view code has already been processed. |
165
|
|
|
* |
166
|
|
|
* @param string $pk The PK to check been processed |
167
|
|
|
* @param string $storeViewCode The store view code to check been processed |
168
|
|
|
* |
169
|
|
|
* @return boolean TRUE if the PK and store view code has been processed, else FALSE |
170
|
|
|
*/ |
171
|
|
|
protected function storeViewHasBeenProcessed($pk, $storeViewCode) |
172
|
|
|
{ |
173
|
|
|
return $this->getSubject()->storeViewHasBeenProcessed($pk, $storeViewCode); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Persist's the passed varchar attribute. |
178
|
|
|
* |
179
|
|
|
* @param array $attribute The attribute to persist |
180
|
|
|
* |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
protected function persistVarcharAttribute($attribute) |
184
|
|
|
{ |
185
|
|
|
$this->getCustomerAddressBunchProcessor()->persistCustomerVarcharAttribute($attribute); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Persist's the passed integer attribute. |
190
|
|
|
* |
191
|
|
|
* @param array $attribute The attribute to persist |
192
|
|
|
* |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
|
|
protected function persistIntAttribute($attribute) |
196
|
|
|
{ |
197
|
|
|
$this->getCustomerAddressBunchProcessor()->persistCustomerIntAttribute($attribute); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Persist's the passed decimal attribute. |
202
|
|
|
* |
203
|
|
|
* @param array $attribute The attribute to persist |
204
|
|
|
* |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
protected function persistDecimalAttribute($attribute) |
208
|
|
|
{ |
209
|
|
|
$this->getCustomerAddressBunchProcessor()->persistCustomerDecimalAttribute($attribute); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Persist's the passed datetime attribute. |
214
|
|
|
* |
215
|
|
|
* @param array $attribute The attribute to persist |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
protected function persistDatetimeAttribute($attribute) |
220
|
|
|
{ |
221
|
|
|
$this->getCustomerAddressBunchProcessor()->persistCustomerDatetimeAttribute($attribute); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Persist's the passed text attribute. |
226
|
|
|
* |
227
|
|
|
* @param array $attribute The attribute to persist |
228
|
|
|
* |
229
|
|
|
* @return void |
230
|
|
|
*/ |
231
|
|
|
protected function persistTextAttribute($attribute) |
232
|
|
|
{ |
233
|
|
|
$this->getCustomerAddressBunchProcessor()->persistCustomerTextAttribute($attribute); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Delete's the datetime attribute with the passed value ID. |
238
|
|
|
* |
239
|
|
|
* @param array $row The attributes of the entity to delete |
240
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
241
|
|
|
* |
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
|
|
protected function deleteDatetimeAttribute(array $row, $name = null) |
245
|
|
|
{ |
246
|
|
|
$this->getCustomerAddressBunchProcessor()->deleteCustomerDatetimeAttribute($row, $name); |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Delete's the decimal attribute with the passed value ID. |
251
|
|
|
* |
252
|
|
|
* @param array $row The attributes of the entity to delete |
253
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
protected function deleteDecimalAttribute(array $row, $name = null) |
258
|
|
|
{ |
259
|
|
|
$this->getCustomerAddressBunchProcessor()->deleteCustomerDecimalAttribute($row, $name); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Delete's the integer attribute with the passed value ID. |
264
|
|
|
* |
265
|
|
|
* @param array $row The attributes of the entity to delete |
266
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
267
|
|
|
* |
268
|
|
|
* @return void |
269
|
|
|
*/ |
270
|
|
|
protected function deleteIntAttribute(array $row, $name = null) |
271
|
|
|
{ |
272
|
|
|
$this->getCustomerAddressBunchProcessor()->deleteCustomerIntAttribute($row, $name); |
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Delete's the text attribute with the passed value ID. |
277
|
|
|
* |
278
|
|
|
* @param array $row The attributes of the entity to delete |
279
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
280
|
|
|
* |
281
|
|
|
* @return void |
282
|
|
|
*/ |
283
|
|
|
protected function deleteTextAttribute(array $row, $name = null) |
284
|
|
|
{ |
285
|
|
|
$this->getCustomerAddressBunchProcessor()->deleteCustomerTextAttribute($row, $name); |
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Delete's the varchar attribute with the passed value ID. |
290
|
|
|
* |
291
|
|
|
* @param array $row The attributes of the entity to delete |
292
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
293
|
|
|
* |
294
|
|
|
* @return void |
295
|
|
|
*/ |
296
|
|
|
protected function deleteVarcharAttribute(array $row, $name = null) |
297
|
|
|
{ |
298
|
|
|
$this->getCustomerAddressBunchProcessor()->deleteCustomerVarcharAttribute($row, $name); |
|
|
|
|
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.