1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Attribute\Observers\AttributeObserver |
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\Utils\BackendTypeKeys; |
24
|
|
|
use TechDivision\Import\Attribute\Utils\ColumnKeys; |
25
|
|
|
use TechDivision\Import\Attribute\Utils\MemberNames; |
26
|
|
|
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Observer that create's the EAV attribute itself. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/techdivision/import-attribute |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
class AttributeObserver extends AbstractAttributeImportObserver |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The attribute processor instance. |
42
|
|
|
* |
43
|
|
|
* @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
44
|
|
|
*/ |
45
|
|
|
protected $attributeBunchProcessor; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initializes the observer with the passed subject instance. |
49
|
|
|
* |
50
|
|
|
* @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
51
|
|
|
*/ |
52
|
|
|
public function __construct(AttributeBunchProcessorInterface $attributeBunchProcessor) |
53
|
|
|
{ |
54
|
|
|
$this->attributeBunchProcessor = $attributeBunchProcessor; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Process the observer's business logic. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
protected function process() |
63
|
|
|
{ |
64
|
|
|
|
65
|
|
|
// query whether or not, we've found a new attribute code => means we've found a new attribute |
66
|
|
|
if ($this->hasBeenProcessed($this->getValue(ColumnKeys::ATTRIBUTE_CODE))) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// prepare the attribue values |
71
|
|
|
$attribute = $this->initializeAttribute($this->prepareAttributes()); |
72
|
|
|
|
73
|
|
|
// insert the entity and set the entity ID |
74
|
|
|
$this->setLastAttributeId($this->persistAttribute($attribute)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function getDefaultValues() |
81
|
|
|
{ |
82
|
|
|
return array( |
83
|
|
|
MemberNames::BACKEND_TYPE => BackendTypeKeys::BACKEND_TYPE_STATIC, |
84
|
|
|
MemberNames::IS_REQUIRED => 0, |
85
|
|
|
MemberNames::IS_USER_DEFINED => 1, |
86
|
|
|
MemberNames::IS_UNIQUE => 0, |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
92
|
|
|
* |
93
|
|
|
* @return array The prepared attributes |
94
|
|
|
*/ |
95
|
|
|
protected function prepareAttributes() |
96
|
|
|
{ |
97
|
|
|
// map the entity type code to the ID |
98
|
|
|
$entityType = $this->getEntityType($this->getValue(ColumnKeys::ENTITY_TYPE_CODE)); |
99
|
|
|
$entityTypeId = $entityType[MemberNames::ENTITY_TYPE_ID]; |
100
|
|
|
|
101
|
|
|
return $this->initializeEntity(array_merge( |
102
|
|
|
array( |
103
|
|
|
MemberNames::ENTITY_TYPE_ID => $entityTypeId |
104
|
|
|
), |
105
|
|
|
$this->getPreparedAttributeData( |
106
|
|
|
array( |
107
|
|
|
MemberNames::ATTRIBUTE_CODE, |
108
|
|
|
MemberNames::ATTRIBUTE_MODEL, |
109
|
|
|
MemberNames::BACKEND_MODEL, |
110
|
|
|
MemberNames::BACKEND_TYPE, |
111
|
|
|
MemberNames::BACKEND_TABLE, |
112
|
|
|
MemberNames::FRONTEND_MODEL, |
113
|
|
|
MemberNames::FRONTEND_INPUT, |
114
|
|
|
MemberNames::FRONTEND_LABEL, |
115
|
|
|
MemberNames::FRONTEND_CLASS, |
116
|
|
|
MemberNames::SOURCE_MODEL, |
117
|
|
|
MemberNames::IS_REQUIRED, |
118
|
|
|
MemberNames::IS_USER_DEFINED, |
119
|
|
|
MemberNames::DEFAULT_VALUE, |
120
|
|
|
MemberNames::IS_UNIQUE, |
121
|
|
|
MemberNames::NOTE, |
122
|
|
|
), |
123
|
|
|
$this->isForceDefaultValues() |
124
|
|
|
) |
125
|
|
|
)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Should default values be used for undefined columns |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
protected function isForceDefaultValues() |
133
|
|
|
{ |
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Initialize the attribute with the passed attributes and returns an instance. |
139
|
|
|
* |
140
|
|
|
* @param array $attr The attribute attributes |
141
|
|
|
* |
142
|
|
|
* @return array The initialized attribute |
143
|
|
|
*/ |
144
|
|
|
protected function initializeAttribute(array $attr) |
145
|
|
|
{ |
146
|
|
|
return $attr; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return's the attribute bunch processor instance. |
151
|
|
|
* |
152
|
|
|
* @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
153
|
|
|
*/ |
154
|
|
|
protected function getAttributeBunchProcessor() |
155
|
|
|
{ |
156
|
|
|
return $this->attributeBunchProcessor; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Map's the passed attribute code to the attribute ID that has been created recently. |
161
|
|
|
* |
162
|
|
|
* @param string $attributeCode The attribute code that has to be mapped |
163
|
|
|
* |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
protected function addAttributeCodeIdMapping($attributeCode) |
167
|
|
|
{ |
168
|
|
|
$this->getSubject()->addAttributeCodeIdMapping($attributeCode); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Queries whether or not the attribute with the passed code has already been processed. |
173
|
|
|
* |
174
|
|
|
* @param string $attributeCode The attribute code to check |
175
|
|
|
* |
176
|
|
|
* @return boolean TRUE if the path has been processed, else FALSE |
177
|
|
|
*/ |
178
|
|
|
protected function hasBeenProcessed($attributeCode) |
179
|
|
|
{ |
180
|
|
|
return $this->getSubject()->hasBeenProcessed($attributeCode); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set's the ID of the attribute that has been created recently. |
185
|
|
|
* |
186
|
|
|
* @param integer $lastAttributeId The attribute ID |
187
|
|
|
* |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
|
|
protected function setLastAttributeId($lastAttributeId) |
191
|
|
|
{ |
192
|
|
|
$this->getSubject()->setLastAttributeId($lastAttributeId); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Persist the passed attribute. |
197
|
|
|
* |
198
|
|
|
* @param array $attribute The attribute to persist |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
protected function persistAttribute(array $attribute) |
203
|
|
|
{ |
204
|
|
|
return $this->getAttributeBunchProcessor()->persistAttribute($attribute); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Return's the entity type for the passed code. |
209
|
|
|
* |
210
|
|
|
* @param string $entityTypeCode The entity type code |
211
|
|
|
* |
212
|
|
|
* @return array The requested entity type |
213
|
|
|
* @throws \Exception Is thrown, if the entity type with the passed code is not available |
214
|
|
|
*/ |
215
|
|
|
protected function getEntityType($entityTypeCode) |
216
|
|
|
{ |
217
|
|
|
return $this->getSubject()->getEntityType($entityTypeCode); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
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: