|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Attribute\Observers\PreLoadAttributeIdObserver |
|
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\Services\AttributeBunchProcessorInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Observer that pre-loads the attribute ID of the EAV attribute with the code found in the CSV file. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Tim Wagner <[email protected]> |
|
30
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
32
|
|
|
* @link https://github.com/techdivision/import-attribute |
|
33
|
|
|
* @link http://www.techdivision.com |
|
34
|
|
|
*/ |
|
35
|
|
|
class PreLoadAttributeIdObserver extends AbstractAttributeImportObserver |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The attribute processor instance. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $attributeBunchProcessor; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Initializes the observer with the passed subject instance. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(AttributeBunchProcessorInterface $attributeBunchProcessor) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->attributeBunchProcessor = $attributeBunchProcessor; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Return's the attribute bunch processor instance. |
|
57
|
|
|
* |
|
58
|
|
|
* @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getAttributeBunchProcessor() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->attributeBunchProcessor; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Process the observer's business logic. |
|
67
|
|
|
* |
|
68
|
|
|
* @return array The processed row |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function process() |
|
71
|
|
|
{ |
|
72
|
|
|
|
|
73
|
|
|
// query whether or not, we've found a new attribute code => means we've found a new EAV attribute |
|
74
|
|
|
if ($this->hasBeenProcessed($attributeCode = $this->getValue(ColumnKeys::ATTRIBUTE_CODE))) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// preserve the attribute ID for the EAV attribute with the passed code |
|
79
|
|
|
if ($attribute = $this->loadAttributeByAttributeCode($attributeCode)) { |
|
80
|
|
|
$this->preLoadAttributeId($attribute); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Queries whether or not the attribute with the passed code has already been processed. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $attributeCode The attribute code to check |
|
88
|
|
|
* |
|
89
|
|
|
* @return boolean TRUE if the path has been processed, else FALSE |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function hasBeenProcessed($attributeCode) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->getSubject()->hasBeenProcessed($attributeCode); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Pre-load and temporary persist the attribute ID for the passed EAV attribute. |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $attribute The EAV attribute to pre-load |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function preLoadAttributeId(array $attribute) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->getSubject()->preLoadAttributeId($attribute); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Load's and return's the EAV attribute with the passed code. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $attributeCode The code of the EAV attribute to load |
|
112
|
|
|
* |
|
113
|
|
|
* @return array The EAV attribute |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function loadAttributeByAttributeCode($attributeCode) |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->getAttributeBunchProcessor()->loadAttributeByAttributeCode($attributeCode); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
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: