1 | <?php |
||
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) |
||
56 | |||
57 | /** |
||
58 | * Process the observer's business logic. |
||
59 | * |
||
60 | * @return void |
||
61 | */ |
||
62 | protected function process() |
||
76 | |||
77 | /** |
||
78 | * Prepare the attributes of the entity that has to be persisted. |
||
79 | * |
||
80 | * @return array The prepared attributes |
||
81 | */ |
||
82 | protected function prepareAttributes() |
||
128 | |||
129 | /** |
||
130 | * Initialize the attribute with the passed attributes and returns an instance. |
||
131 | * |
||
132 | * @param array $attr The attribute attributes |
||
133 | * |
||
134 | * @return array The initialized attribute |
||
135 | */ |
||
136 | protected function initializeAttribute(array $attr) |
||
140 | |||
141 | /** |
||
142 | * Return's the attribute bunch processor instance. |
||
143 | * |
||
144 | * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
||
145 | */ |
||
146 | protected function getAttributeBunchProcessor() |
||
150 | |||
151 | /** |
||
152 | * Map's the passed attribute code to the attribute ID that has been created recently. |
||
153 | * |
||
154 | * @param string $attributeCode The attribute code that has to be mapped |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | protected function addAttributeCodeIdMapping($attributeCode) |
||
162 | |||
163 | /** |
||
164 | * Queries whether or not the attribute with the passed code has already been processed. |
||
165 | * |
||
166 | * @param string $attributeCode The attribute code to check |
||
167 | * |
||
168 | * @return boolean TRUE if the path has been processed, else FALSE |
||
169 | */ |
||
170 | protected function hasBeenProcessed($attributeCode) |
||
174 | |||
175 | /** |
||
176 | * Set's the ID of the attribute that has been created recently. |
||
177 | * |
||
178 | * @param integer $lastAttributeId The attribute ID |
||
179 | * |
||
180 | * @return void |
||
181 | */ |
||
182 | protected function setLastAttributeId($lastAttributeId) |
||
186 | |||
187 | /** |
||
188 | * Persist the passed attribute. |
||
189 | * |
||
190 | * @param array $attribute The attribute to persist |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | protected function persistAttribute(array $attribute) |
||
198 | |||
199 | /** |
||
200 | * Return's the entity type for the passed code. |
||
201 | * |
||
202 | * @param string $entityTypeCode The entity type code |
||
203 | * |
||
204 | * @return array The requested entity type |
||
205 | * @throws \Exception Is thrown, if the entity type with the passed code is not available |
||
206 | */ |
||
207 | protected function getEntityType($entityTypeCode) |
||
211 | } |
||
212 |
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: