|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Attribute\Observers\AttributeOptionSwatchFileUploadObserver |
|
5
|
|
|
* |
|
6
|
|
|
* @author Marcus Döllerer <[email protected]> |
|
7
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
|
8
|
|
|
* @link https://www.techdivision.com |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace TechDivision\Import\Attribute\Observers; |
|
12
|
|
|
|
|
13
|
|
|
use TechDivision\Import\Attribute\Utils\ConfigurationKeys; |
|
14
|
|
|
use TechDivision\Import\Attribute\Utils\MemberNames; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Abstract attribute observer that handles files of visual swatches during import. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Marcus Döllerer <[email protected]> |
|
20
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
|
21
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
22
|
|
|
* @link https://github.com/techdivision/import-attribute |
|
23
|
|
|
* @link http://www.techdivision.com |
|
24
|
|
|
*/ |
|
25
|
|
|
class AttributeOptionSwatchFileUploadObserver extends AttributeOptionSwatchUpdateObserver |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Process the observer's business logic. |
|
30
|
|
|
* |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function process() |
|
34
|
|
|
{ |
|
35
|
|
|
// skip this step if the configuration value 'copy-images' is undefined or set to 'false' |
|
36
|
|
|
if (!$this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::COPY_IMAGES) || |
|
37
|
|
|
!$this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::COPY_IMAGES)) { |
|
38
|
|
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// initialize the option swatch attribute |
|
42
|
|
|
$attributeOptionSwatch = $this->initializeAttribute(array( |
|
43
|
|
|
MemberNames::OPTION_ID => $this->getLastOptionId() |
|
44
|
|
|
)); |
|
45
|
|
|
|
|
46
|
|
|
// skip this step for color swatches and text swatches |
|
47
|
|
|
if (!isset($attributeOptionSwatch['type']) || $attributeOptionSwatch['type'] !== '2') { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// upload the file to the configured directory |
|
52
|
|
|
$imagePath = $this->getSubject()->uploadFile($attributeOptionSwatch['value']); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
// inject the new image path and update the attribute option swatch |
|
55
|
|
|
$attributeOptionSwatch['value'] = $imagePath; |
|
56
|
|
|
$this->getAttributeBunchProcessor()->persistAttributeOptionSwatch($attributeOptionSwatch); |
|
57
|
|
|
|
|
58
|
|
|
// add debug log entry |
|
59
|
|
|
$this->getSubject() |
|
60
|
|
|
->getSystemLogger() |
|
61
|
|
|
->debug( |
|
62
|
|
|
sprintf( |
|
63
|
|
|
'Successfully copied image %s for swatch with id %s', |
|
64
|
|
|
$imagePath, |
|
65
|
|
|
$attributeOptionSwatch['swatch_id'] |
|
66
|
|
|
) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
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: