1 | <?php |
||
42 | class ProductAttributeObserver extends AbstractAttributeObserver implements StateDetectorAwareObserverInterface, ObserverFactoryInterface |
||
43 | { |
||
44 | |||
45 | /** |
||
46 | * The product bunch processor instance. |
||
47 | * |
||
48 | * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
||
49 | */ |
||
50 | protected $productBunchProcessor; |
||
51 | |||
52 | /** |
||
53 | * The array with the column mappings that has to be computed. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $columns = array(); |
||
58 | |||
59 | /** |
||
60 | * The backend type => data type mappings. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $backendTypeKeyMappings = array( |
||
65 | BackendTypeKeys::BACKEND_TYPE_DATETIME => 'string', |
||
66 | BackendTypeKeys::BACKEND_TYPE_DECIMAL => 'float', |
||
67 | BackendTypeKeys::BACKEND_TYPE_FLOAT => 'float', |
||
68 | BackendTypeKeys::BACKEND_TYPE_INT => 'integer', |
||
69 | BackendTypeKeys::BACKEND_TYPE_STATIC => 'string', |
||
70 | BackendTypeKeys::BACKEND_TYPE_TEXT => 'string', |
||
71 | BackendTypeKeys::BACKEND_TYPE_VARCHAR => 'string' |
||
72 | ); |
||
73 | |||
74 | /** |
||
75 | * Initialize the observer with the passed product bunch processor instance. |
||
76 | * |
||
77 | * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance |
||
78 | * @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance to use |
||
79 | */ |
||
80 | public function __construct(ProductBunchProcessorInterface $productBunchProcessor, StateDetectorInterface $stateDetector = null) |
||
89 | |||
90 | /** |
||
91 | * Will be invoked by the observer visitor when a factory has been defined to create the observer instance. |
||
92 | * |
||
93 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
||
94 | * |
||
95 | * @return \TechDivision\Import\Observers\ObserverInterface The observer instance |
||
96 | */ |
||
97 | public function createObserver(SubjectInterface $subject) |
||
111 | |||
112 | /** |
||
113 | * Returns an array of the columns with their types to detect state. |
||
114 | * |
||
115 | * @return array The array with the column names as key and their type as value |
||
116 | */ |
||
117 | public function getColumns(array $attribute = array()) |
||
121 | |||
122 | /** |
||
123 | * Intializes the existing attributes for the entity with the passed primary key. |
||
124 | * |
||
125 | * @param string $pk The primary key of the entity to load the attributes for |
||
126 | * @param integer $storeId The ID of the store view to load the attributes for |
||
127 | * |
||
128 | * @return array The entity attributes |
||
129 | */ |
||
130 | protected function getAttributesByPrimaryKeyAndStoreId($pk, $storeId) |
||
134 | |||
135 | /** |
||
136 | * Return's the product bunch processor instance. |
||
137 | * |
||
138 | * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
||
139 | */ |
||
140 | protected function getProductBunchProcessor() |
||
144 | |||
145 | /** |
||
146 | * Return's the PK to create the product => attribute relation. |
||
147 | * |
||
148 | * @return integer The PK to create the relation with |
||
149 | */ |
||
150 | protected function getPrimaryKey() |
||
154 | |||
155 | /** |
||
156 | * Return's the PK column name to create the product => attribute relation. |
||
157 | * |
||
158 | * @return string The PK column name |
||
159 | */ |
||
160 | protected function getPrimaryKeyMemberName() |
||
164 | |||
165 | /** |
||
166 | * Return's the column name that contains the primary key. |
||
167 | * |
||
168 | * @return string the column name that contains the primary key |
||
169 | */ |
||
170 | protected function getPrimaryKeyColumnName() |
||
174 | |||
175 | /** |
||
176 | * Queries whether or not the passed PK and store view code has already been processed. |
||
177 | * |
||
178 | * @param string $pk The PK to check been processed |
||
179 | * @param string $storeViewCode The store view code to check been processed |
||
180 | * |
||
181 | * @return boolean TRUE if the PK and store view code has been processed, else FALSE |
||
182 | */ |
||
183 | protected function storeViewHasBeenProcessed($pk, $storeViewCode) |
||
187 | |||
188 | /** |
||
189 | * Persist's the passed varchar attribute. |
||
190 | * |
||
191 | * @param array $attribute The attribute to persist |
||
192 | * |
||
193 | * @return void |
||
194 | */ |
||
195 | protected function persistVarcharAttribute($attribute) |
||
199 | |||
200 | /** |
||
201 | * Persist's the passed integer attribute. |
||
202 | * |
||
203 | * @param array $attribute The attribute to persist |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | protected function persistIntAttribute($attribute) |
||
211 | |||
212 | /** |
||
213 | * Persist's the passed decimal attribute. |
||
214 | * |
||
215 | * @param array $attribute The attribute to persist |
||
216 | * |
||
217 | * @return void |
||
218 | */ |
||
219 | protected function persistDecimalAttribute($attribute) |
||
223 | |||
224 | /** |
||
225 | * Persist's the passed datetime attribute. |
||
226 | * |
||
227 | * @param array $attribute The attribute to persist |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | protected function persistDatetimeAttribute($attribute) |
||
235 | |||
236 | /** |
||
237 | * Persist's the passed text attribute. |
||
238 | * |
||
239 | * @param array $attribute The attribute to persist |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
243 | protected function persistTextAttribute($attribute) |
||
247 | |||
248 | /** |
||
249 | * Delete's the datetime attribute with the passed value ID. |
||
250 | * |
||
251 | * @param array $row The attributes of the entity to delete |
||
252 | * @param string|null $name The name of the prepared statement that has to be executed |
||
253 | * |
||
254 | * @return void |
||
255 | */ |
||
256 | protected function deleteDatetimeAttribute(array $row, $name = null) |
||
260 | |||
261 | /** |
||
262 | * Delete's the decimal attribute with the passed value ID. |
||
263 | * |
||
264 | * @param array $row The attributes of the entity to delete |
||
265 | * @param string|null $name The name of the prepared statement that has to be executed |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | protected function deleteDecimalAttribute(array $row, $name = null) |
||
273 | |||
274 | /** |
||
275 | * Delete's the integer attribute with the passed value ID. |
||
276 | * |
||
277 | * @param array $row The attributes of the entity to delete |
||
278 | * @param string|null $name The name of the prepared statement that has to be executed |
||
279 | * |
||
280 | * @return void |
||
281 | */ |
||
282 | protected function deleteIntAttribute(array $row, $name = null) |
||
286 | |||
287 | /** |
||
288 | * Delete's the text attribute with the passed value ID. |
||
289 | * |
||
290 | * @param array $row The attributes of the entity to delete |
||
291 | * @param string|null $name The name of the prepared statement that has to be executed |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | protected function deleteTextAttribute(array $row, $name = null) |
||
299 | |||
300 | /** |
||
301 | * Delete's the varchar attribute with the passed value ID. |
||
302 | * |
||
303 | * @param array $row The attributes of the entity to delete |
||
304 | * @param string|null $name The name of the prepared statement that has to be executed |
||
305 | * |
||
306 | * @return void |
||
307 | */ |
||
308 | protected function deleteVarcharAttribute(array $row, $name = null) |
||
312 | } |
||
313 |
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: