1 | <?php |
||
36 | class CatalogAttributeObserver extends AbstractAttributeImportObserver |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * The key for the additional data containing the swatch type. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | const SWATCH_INPUT_TYPE = 'swatch_input_type'; |
||
45 | |||
46 | /** |
||
47 | * The available swatch types. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $swatchTypes = array('text', 'visual'); |
||
52 | |||
53 | /** |
||
54 | * The attribute processor instance. |
||
55 | * |
||
56 | * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
||
57 | */ |
||
58 | protected $attributeBunchProcessor; |
||
59 | |||
60 | /** |
||
61 | * The array with the possible column names. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $columnNames = array( |
||
66 | ColumnKeys::FRONTEND_INPUT_RENDERER, |
||
67 | ColumnKeys::IS_GLOBAL, |
||
68 | ColumnKeys::IS_VISIBLE, |
||
69 | ColumnKeys::IS_SEARCHABLE, |
||
70 | ColumnKeys::IS_FILTERABLE, |
||
71 | ColumnKeys::IS_COMPARABLE, |
||
72 | ColumnKeys::IS_VISIBLE_ON_FRONT, |
||
73 | ColumnKeys::IS_HTML_ALLOWED_ON_FRONT, |
||
74 | ColumnKeys::IS_USED_FOR_PRICE_RULES, |
||
75 | ColumnKeys::IS_FILTERABLE_IN_SEARCH, |
||
76 | ColumnKeys::USED_IN_PRODUCT_LISTING, |
||
77 | ColumnKeys::USED_FOR_SORT_BY, |
||
78 | ColumnKeys::APPLY_TO, |
||
79 | ColumnKeys::IS_VISIBLE_IN_ADVANCED_SEARCH, |
||
80 | ColumnKeys::POSITION, |
||
81 | ColumnKeys::IS_WYSIWYG_ENABLED, |
||
82 | ColumnKeys::IS_USED_FOR_PROMO_RULES, |
||
83 | ColumnKeys::IS_REQUIRED_IN_ADMIN_STORE, |
||
84 | ColumnKeys::IS_USED_IN_GRID, |
||
85 | ColumnKeys::IS_VISIBLE_IN_GRID, |
||
86 | ColumnKeys::IS_FILTERABLE_IN_GRID, |
||
87 | ColumnKeys::SEARCH_WEIGHT, |
||
88 | ColumnKeys::ADDITIONAL_DATA |
||
89 | ); |
||
90 | |||
91 | /** |
||
92 | * Initializes the observer with the passed subject instance. |
||
93 | * |
||
94 | * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
||
95 | */ |
||
96 | 3 | public function __construct(AttributeBunchProcessorInterface $attributeBunchProcessor) |
|
100 | |||
101 | /** |
||
102 | * Process the observer's business logic. |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | 3 | protected function process() |
|
117 | |||
118 | /** |
||
119 | * Prepare the attributes of the entity that has to be persisted. |
||
120 | * |
||
121 | * @return array The prepared attributes |
||
122 | * @throws \Exception Is thrown, if the size of the option values doesn't equals the size of swatch values, in case |
||
123 | */ |
||
124 | 3 | protected function prepareAttributes() |
|
184 | |||
185 | /** |
||
186 | * Serialize the additional_data attribute of the passed array. |
||
187 | * |
||
188 | * @param array $attr The attribute with the data to serialize |
||
189 | * |
||
190 | * @return array The attribute with the serialized additional_data |
||
191 | */ |
||
192 | 3 | protected function serializeAdditionalData(array $attr) |
|
203 | |||
204 | /** |
||
205 | * Initialize the attribute with the passed attributes and returns an instance. |
||
206 | * |
||
207 | * @param array $attr The attribute attributes |
||
208 | * |
||
209 | * @return array The initialized attribute |
||
210 | */ |
||
211 | 1 | protected function initializeAttribute(array $attr) |
|
215 | |||
216 | /** |
||
217 | * Return's the attribute bunch processor instance. |
||
218 | * |
||
219 | * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
||
220 | */ |
||
221 | 3 | protected function getAttributeBunchProcessor() |
|
225 | |||
226 | /** |
||
227 | * Map's the passed attribute code to the attribute ID that has been created recently. |
||
228 | * |
||
229 | * @param string $attributeCode The attribute code that has to be mapped |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | protected function addAttributeCodeIdMapping($attributeCode) |
||
237 | |||
238 | /** |
||
239 | * Queries whether or not the attribute with the passed code has already been processed. |
||
240 | * |
||
241 | * @param string $attributeCode The attribute code to check |
||
242 | * |
||
243 | * @return boolean TRUE if the path has been processed, else FALSE |
||
244 | */ |
||
245 | 3 | protected function hasBeenProcessed($attributeCode) |
|
249 | |||
250 | /** |
||
251 | * Return's the ID of the attribute that has been created recently. |
||
252 | * |
||
253 | * @return integer The attribute ID |
||
254 | */ |
||
255 | 3 | protected function getLastAttributeId() |
|
259 | |||
260 | /** |
||
261 | * Return's TRUE if the additional data contains a swatch type. |
||
262 | * |
||
263 | * @param array $additionalData The additional data to query for a valid swatch type |
||
264 | * |
||
265 | * @return boolean TRUE if the data contains a swatch type, else FALSE |
||
266 | */ |
||
267 | 1 | protected function isSwatchType(array $additionalData) |
|
271 | |||
272 | /** |
||
273 | * Persist the passed EAV catalog attribute. |
||
274 | * |
||
275 | * @param array $catalogAttribute The EAV catalog attribute to persist |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | 3 | protected function persistCatalogAttribute(array $catalogAttribute) |
|
283 | } |
||
284 |
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: