1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Attribute\Observers\CatalogAttributeObserver |
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-attribute |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Attribute\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Attribute\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Attribute\Utils\MemberNames; |
25
|
|
|
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Observer that create's the EAV catalog attribute itself. |
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-attribute |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
class CatalogAttributeObserver extends AbstractAttributeImportObserver |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The attribute processor instance. |
41
|
|
|
* |
42
|
|
|
* @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
43
|
|
|
*/ |
44
|
|
|
protected $attributeBunchProcessor; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initializes the observer with the passed subject instance. |
48
|
|
|
* |
49
|
|
|
* @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
50
|
|
|
*/ |
51
|
|
|
public function __construct(AttributeBunchProcessorInterface $attributeBunchProcessor) |
52
|
|
|
{ |
53
|
|
|
$this->attributeBunchProcessor = $attributeBunchProcessor; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Process the observer's business logic. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
protected function process() |
62
|
|
|
{ |
63
|
|
|
|
64
|
|
|
// query whether or not, we've found a new attribute code => means we've found a new attribute |
65
|
|
|
if ($this->hasBeenProcessed($this->getValue(ColumnKeys::ATTRIBUTE_CODE))) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// initialize and persist the EAV catalog attribute |
70
|
|
|
$this->persistCatalogAttribute($this->initializeAttribute($this->prepareAttributes())); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function getDefaultValues() |
77
|
|
|
{ |
78
|
|
|
return array( |
79
|
|
|
MemberNames::IS_GLOBAL => 1, |
80
|
|
|
MemberNames::IS_VISIBLE => 1, |
81
|
|
|
MemberNames::IS_SEARCHABLE => 0, |
82
|
|
|
MemberNames::IS_FILTERABLE => 0, |
83
|
|
|
MemberNames::IS_COMPARABLE => 0, |
84
|
|
|
MemberNames::IS_VISIBLE_ON_FRONT => 0, |
85
|
|
|
MemberNames::IS_HTML_ALLOWED_ON_FRONT => 0, |
86
|
|
|
MemberNames::IS_USED_FOR_PRICE_RULES => 0, |
87
|
|
|
MemberNames::IS_FILTERABLE_IN_SEARCH => 0, |
88
|
|
|
MemberNames::USED_IN_PRODUCT_LISTING => 0, |
89
|
|
|
MemberNames::USED_FOR_SORT_BY => 0, |
90
|
|
|
MemberNames::IS_VISIBLE_IN_ADVANCED_SEARCH => 0, |
91
|
|
|
MemberNames::POSITION => 0, |
92
|
|
|
MemberNames::IS_WYSIWYG_ENABLED => 0, |
93
|
|
|
MemberNames::IS_USED_FOR_PROMO_RULES => 0, |
94
|
|
|
MemberNames::IS_REQUIRED_IN_ADMIN_STORE => 0, |
95
|
|
|
MemberNames::IS_USED_IN_GRID => 0, |
96
|
|
|
MemberNames::IS_VISIBLE_IN_GRID => 0, |
97
|
|
|
MemberNames::IS_FILTERABLE_IN_GRID => 0, |
98
|
|
|
MemberNames::SEARCH_WEIGHT => 1, |
99
|
|
|
MemberNames::ADDITIONAL_DATA => array(), |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
protected function getCallbacks() |
107
|
|
|
{ |
108
|
|
|
return array( |
109
|
|
|
MemberNames::ADDITIONAL_DATA => function($value) { |
110
|
|
|
$result = array(); |
111
|
|
|
$explodedAdditionalData = $this->explode($value); |
112
|
|
|
foreach ($explodedAdditionalData as $value) { |
113
|
|
|
list ($key, $val) = $this->explode($value, '='); |
114
|
|
|
$result[$key] = $val; |
115
|
|
|
} |
116
|
|
|
return $result; |
117
|
|
|
}, |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
123
|
|
|
* |
124
|
|
|
* @return array The prepared attributes |
125
|
|
|
*/ |
126
|
|
|
protected function prepareAttributes() |
127
|
|
|
{ |
128
|
|
|
// load the recently created EAV attribute ID |
129
|
|
|
$attributeId = $this->getLastAttributeId(); |
130
|
|
|
|
131
|
|
|
return $this->initializeEntity(array_merge( |
132
|
|
|
array( |
133
|
|
|
MemberNames::ATTRIBUTE_ID => $attributeId |
134
|
|
|
), |
135
|
|
|
$this->getPreparedAttributeData( |
136
|
|
|
array( |
137
|
|
|
MemberNames::FRONTEND_INPUT_RENDERER, |
138
|
|
|
MemberNames::IS_GLOBAL, |
139
|
|
|
MemberNames::IS_VISIBLE, |
140
|
|
|
MemberNames::IS_SEARCHABLE, |
141
|
|
|
MemberNames::IS_FILTERABLE, |
142
|
|
|
MemberNames::IS_COMPARABLE, |
143
|
|
|
MemberNames::IS_VISIBLE_ON_FRONT, |
144
|
|
|
MemberNames::IS_HTML_ALLOWED_ON_FRONT, |
145
|
|
|
MemberNames::IS_USED_FOR_PRICE_RULES, |
146
|
|
|
MemberNames::IS_FILTERABLE_IN_SEARCH, |
147
|
|
|
MemberNames::USED_IN_PRODUCT_LISTING, |
148
|
|
|
MemberNames::USED_FOR_SORT_BY, |
149
|
|
|
MemberNames::APPLY_TO, |
150
|
|
|
MemberNames::IS_VISIBLE_IN_ADVANCED_SEARCH, |
151
|
|
|
MemberNames::POSITION, |
152
|
|
|
MemberNames::IS_WYSIWYG_ENABLED, |
153
|
|
|
MemberNames::IS_USED_FOR_PROMO_RULES, |
154
|
|
|
MemberNames::IS_REQUIRED_IN_ADMIN_STORE, |
155
|
|
|
MemberNames::IS_USED_IN_GRID, |
156
|
|
|
MemberNames::IS_VISIBLE_IN_GRID, |
157
|
|
|
MemberNames::IS_FILTERABLE_IN_GRID, |
158
|
|
|
MemberNames::SEARCH_WEIGHT, |
159
|
|
|
MemberNames::ADDITIONAL_DATA, |
160
|
|
|
), |
161
|
|
|
$this->isForceDefaultValues() |
162
|
|
|
) |
163
|
|
|
)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Should default values be used for undefined columns |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
protected function isForceDefaultValues() |
171
|
|
|
{ |
172
|
|
|
return true; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Serialize the additional_data attribute of the passed array. |
177
|
|
|
* |
178
|
|
|
* @param array $attr The attribute with the data to serialize |
179
|
|
|
* |
180
|
|
|
* @return array The attribute with the serialized additional_data |
181
|
|
|
*/ |
182
|
|
|
protected function serializeAdditionalData(array $attr) |
183
|
|
|
{ |
184
|
|
|
|
185
|
|
|
// serialize the additional data value if available |
186
|
|
|
if (is_array($attr[MemberNames::ADDITIONAL_DATA])) { |
187
|
|
|
$attr[MemberNames::ADDITIONAL_DATA] = serialize($attr[MemberNames::ADDITIONAL_DATA]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// return the attribute |
191
|
|
|
return $attr; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Initialize the attribute with the passed attributes and returns an instance. |
196
|
|
|
* |
197
|
|
|
* @param array $attr The attribute attributes |
198
|
|
|
* |
199
|
|
|
* @return array The initialized attribute |
200
|
|
|
*/ |
201
|
|
|
protected function initializeAttribute(array $attr) |
202
|
|
|
{ |
203
|
|
|
return $this->serializeAdditionalData($attr); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Return's the attribute bunch processor instance. |
208
|
|
|
* |
209
|
|
|
* @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
210
|
|
|
*/ |
211
|
|
|
protected function getAttributeBunchProcessor() |
212
|
|
|
{ |
213
|
|
|
return $this->attributeBunchProcessor; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Map's the passed attribute code to the attribute ID that has been created recently. |
218
|
|
|
* |
219
|
|
|
* @param string $attributeCode The attribute code that has to be mapped |
220
|
|
|
* |
221
|
|
|
* @return void |
222
|
|
|
*/ |
223
|
|
|
protected function addAttributeCodeIdMapping($attributeCode) |
224
|
|
|
{ |
225
|
|
|
$this->getSubject()->addAttributeCodeIdMapping($attributeCode); |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Queries whether or not the attribute with the passed code has already been processed. |
230
|
|
|
* |
231
|
|
|
* @param string $attributeCode The attribute code to check |
232
|
|
|
* |
233
|
|
|
* @return boolean TRUE if the path has been processed, else FALSE |
234
|
|
|
*/ |
235
|
|
|
protected function hasBeenProcessed($attributeCode) |
236
|
|
|
{ |
237
|
|
|
return $this->getSubject()->hasBeenProcessed($attributeCode); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Return's the ID of the attribute that has been created recently. |
242
|
|
|
* |
243
|
|
|
* @return integer The attribute ID |
244
|
|
|
*/ |
245
|
|
|
protected function getLastAttributeId() |
246
|
|
|
{ |
247
|
|
|
return $this->getSubject()->getLastAttributeId(); |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Persist the passed EAV catalog attribute. |
252
|
|
|
* |
253
|
|
|
* @param array $catalogAttribute The EAV catalog attribute to persist |
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
protected function persistCatalogAttribute(array $catalogAttribute) |
258
|
|
|
{ |
259
|
|
|
return $this->getAttributeBunchProcessor()->persistCatalogAttribute($catalogAttribute); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
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: