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