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() |
|
185 | |||
186 | /** |
||
187 | * Serialize the additional_data attribute of the passed array. |
||
188 | * |
||
189 | * @param array $attr The attribute with the data to serialize |
||
190 | * |
||
191 | * @return array The attribute with the serialized additional_data |
||
192 | */ |
||
193 | 3 | protected function serializeAdditionalData(array $attr) |
|
204 | |||
205 | /** |
||
206 | * Initialize the attribute with the passed attributes and returns an instance. |
||
207 | * |
||
208 | * @param array $attr The attribute attributes |
||
209 | * |
||
210 | * @return array The initialized attribute |
||
211 | */ |
||
212 | 1 | protected function initializeAttribute(array $attr) |
|
216 | |||
217 | /** |
||
218 | * Return's the attribute bunch processor instance. |
||
219 | * |
||
220 | * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
||
221 | */ |
||
222 | 3 | protected function getAttributeBunchProcessor() |
|
226 | |||
227 | /** |
||
228 | * Map's the passed attribute code to the attribute ID that has been created recently. |
||
229 | * |
||
230 | * @param string $attributeCode The attribute code that has to be mapped |
||
231 | * |
||
232 | * @return void |
||
233 | */ |
||
234 | protected function addAttributeCodeIdMapping($attributeCode) |
||
238 | |||
239 | /** |
||
240 | * Queries whether or not the attribute with the passed code has already been processed. |
||
241 | * |
||
242 | * @param string $attributeCode The attribute code to check |
||
243 | * |
||
244 | * @return boolean TRUE if the path has been processed, else FALSE |
||
245 | */ |
||
246 | 3 | protected function hasBeenProcessed($attributeCode) |
|
250 | |||
251 | /** |
||
252 | * Return's the ID of the attribute that has been created recently. |
||
253 | * |
||
254 | * @return integer The attribute ID |
||
255 | */ |
||
256 | 3 | protected function getLastAttributeId() |
|
260 | |||
261 | /** |
||
262 | * Return's TRUE if the additional data contains a swatch type. |
||
263 | * |
||
264 | * @param array $additionalData The additional data to query for a valid swatch type |
||
265 | * |
||
266 | * @return boolean TRUE if the data contains a swatch type, else FALSE |
||
267 | */ |
||
268 | 1 | protected function isSwatchType(array $additionalData) |
|
272 | |||
273 | /** |
||
274 | * Persist the passed EAV catalog attribute. |
||
275 | * |
||
276 | * @param array $catalogAttribute The EAV catalog attribute to persist |
||
277 | * |
||
278 | * @return void |
||
279 | */ |
||
280 | 3 | protected function persistCatalogAttribute(array $catalogAttribute) |
|
284 | } |
||
285 |
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: