1 | <?php |
||
35 | class ProductMediaObserver extends AbstractProductImportObserver |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * The artefact type. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | const ARTEFACT_TYPE = 'media'; |
||
44 | |||
45 | /** |
||
46 | * The the default image label. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | const DEFAULT_IMAGE_LABEL = 'Image'; |
||
51 | |||
52 | /** |
||
53 | * The array with the image information of on row before they'll be converted into artefacts. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $images = array(); |
||
58 | |||
59 | /** |
||
60 | * The image artefacts that has to be exported. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $artefacts = array(); |
||
65 | |||
66 | /** |
||
67 | * The array with names of the images that should be hidden on the product detail page. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $imagesToHide = array(); |
||
72 | |||
73 | /** |
||
74 | * Holds the image values of the main row. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $mainRow = array(); |
||
79 | |||
80 | /** |
||
81 | * Process the observer's business logic. |
||
82 | * |
||
83 | * @return array The processed row |
||
84 | */ |
||
85 | protected function process() |
||
108 | |||
109 | /** |
||
110 | * Resolve's the value with the passed colum name from the actual row. If a callback will |
||
111 | * be passed, the callback will be invoked with the found value as parameter. If |
||
112 | * the value is NULL or empty, the default value will be returned. |
||
113 | * |
||
114 | * @param string $name The name of the column to return the value for |
||
115 | * @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
||
116 | * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
||
117 | * |
||
118 | * @return mixed|null The, almost formatted, value |
||
119 | * @see \TechDivision\Import\Observers\AbstractObserver::getValue() |
||
120 | */ |
||
121 | protected function getImageValue($name, $default = null, callable $callback = null) |
||
134 | |||
135 | /** |
||
136 | * Parses the column and exports the image data to a separate file. |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | protected function processImages() |
||
197 | |||
198 | /** |
||
199 | * Parses the column and exports the additional image data to a separate file. |
||
200 | * |
||
201 | * @return void |
||
202 | */ |
||
203 | protected function processAdditionalImages() |
||
254 | |||
255 | /** |
||
256 | * Load the images that has to be hidden on the product detail page. |
||
257 | * |
||
258 | * @return void |
||
259 | */ |
||
260 | protected function loadImagesToHide() |
||
271 | |||
272 | /** |
||
273 | * Return's the array with the available image types and their label columns. |
||
274 | * |
||
275 | * @return array The array with the available image types |
||
276 | */ |
||
277 | protected function getImageTypes() |
||
281 | |||
282 | /** |
||
283 | * Return's the default image label. |
||
284 | * |
||
285 | * @return string|null The default image label |
||
286 | */ |
||
287 | protected function getDefaultImageLabel() |
||
291 | |||
292 | /** |
||
293 | * Returns the mapped filename (which is the new filename). |
||
294 | * |
||
295 | * @param string $filename The filename to map |
||
296 | * |
||
297 | * @return string The mapped filename |
||
298 | */ |
||
299 | protected function getImageMapping($filename) |
||
303 | |||
304 | /** |
||
305 | * Returns the original filename for passed one (which is the new filename). |
||
306 | * |
||
307 | * @param string $newFilename The new filename to return the original one for |
||
308 | * |
||
309 | * @return string The original filename |
||
310 | */ |
||
311 | protected function getInversedImageMapping($newFilename) |
||
315 | |||
316 | /** |
||
317 | * Create's and return's a new empty artefact entity. |
||
318 | * |
||
319 | * @param array $columns The array with the column data |
||
320 | * @param array $originalColumnNames The array with a mapping from the old to the new column names |
||
321 | * |
||
322 | * @return array The new artefact entity |
||
323 | */ |
||
324 | protected function newArtefact(array $columns, array $originalColumnNames) |
||
328 | |||
329 | /** |
||
330 | * Add the passed product type artefacts to the product with the |
||
331 | * last entity ID. |
||
332 | * |
||
333 | * @param array $artefacts The product type artefacts |
||
334 | * |
||
335 | * @return void |
||
336 | * @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId() |
||
337 | */ |
||
338 | protected function addArtefacts(array $artefacts) |
||
342 | } |
||
343 |
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: