Total Complexity | 41 |
Total Lines | 523 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Standard |
||
23 | extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base |
||
24 | implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface |
||
25 | { |
||
26 | /** |
||
27 | * Adds the required data used in the template |
||
28 | * |
||
29 | * @param \Aimeos\MW\View\Iface $view View object |
||
30 | * @return \Aimeos\MW\View\Iface View object with assigned parameters |
||
31 | */ |
||
32 | public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface |
||
33 | { |
||
34 | $ds = DIRECTORY_SEPARATOR; |
||
35 | $view->itemDecorators = $this->getClassNames( 'MShop' . $ds . 'Coupon' . $ds . 'Provider' . $ds . 'Decorator' ); |
||
36 | $view->itemProviders = $this->getClassNames( 'MShop' . $ds . 'Coupon' . $ds . 'Provider' ); |
||
37 | $view->itemSubparts = $this->getSubClientNames(); |
||
38 | |||
39 | return $view; |
||
40 | } |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Copies a resource |
||
45 | * |
||
46 | * @return string|null HTML output |
||
47 | */ |
||
48 | public function copy() : ?string |
||
49 | { |
||
50 | $view = $this->getObject()->addData( $this->getView() ); |
||
51 | |||
52 | try |
||
53 | { |
||
54 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
55 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
56 | } |
||
57 | |||
58 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' ); |
||
59 | |||
60 | $view->item = $manager->getItem( $id ); |
||
61 | $view->itemData = $this->toArray( $view->item, true ); |
||
62 | $view->itemAttributes = $this->getConfigAttributes( $view->item ); |
||
63 | $view->itemBody = ''; |
||
64 | |||
65 | foreach( $this->getSubClients() as $idx => $client ) |
||
66 | { |
||
67 | $view->tabindex = ++$idx + 1; |
||
68 | $view->itemBody .= $client->copy(); |
||
69 | } |
||
70 | } |
||
71 | catch( \Exception $e ) |
||
72 | { |
||
73 | $this->report( $e, 'copy' ); |
||
74 | } |
||
75 | |||
76 | return $this->render( $view ); |
||
77 | } |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Creates a new resource |
||
82 | * |
||
83 | * @return string|null HTML output |
||
84 | */ |
||
85 | public function create() : ?string |
||
114 | } |
||
115 | |||
116 | |||
117 | /** |
||
118 | * Deletes a resource |
||
119 | * |
||
120 | * @return string|null HTML output |
||
121 | */ |
||
122 | public function delete() : ?string |
||
161 | } |
||
162 | |||
163 | |||
164 | /** |
||
165 | * Returns a single resource |
||
166 | * |
||
167 | * @return string|null HTML output |
||
168 | */ |
||
169 | public function get() : ?string |
||
170 | { |
||
171 | $view = $this->getObject()->addData( $this->getView() ); |
||
172 | |||
173 | try |
||
174 | { |
||
175 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
176 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
177 | } |
||
178 | |||
179 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' ); |
||
180 | |||
181 | $view->item = $manager->getItem( $id ); |
||
182 | $view->itemData = $this->toArray( $view->item ); |
||
183 | $view->itemAttributes = $this->getConfigAttributes( $view->item ); |
||
184 | $view->itemBody = ''; |
||
185 | |||
186 | foreach( $this->getSubClients() as $idx => $client ) |
||
187 | { |
||
188 | $view->tabindex = ++$idx + 1; |
||
189 | $view->itemBody .= $client->get(); |
||
190 | } |
||
191 | } |
||
192 | catch( \Exception $e ) |
||
193 | { |
||
194 | $this->report( $e, 'get' ); |
||
195 | } |
||
196 | |||
197 | return $this->render( $view ); |
||
198 | } |
||
199 | |||
200 | |||
201 | /** |
||
202 | * Saves the data |
||
203 | * |
||
204 | * @return string|null HTML output |
||
205 | */ |
||
206 | public function save() : ?string |
||
207 | { |
||
208 | $view = $this->getView(); |
||
209 | |||
210 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' ); |
||
211 | $manager->begin(); |
||
212 | |||
213 | try |
||
214 | { |
||
215 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
216 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
|
|||
217 | $view->itemBody = ''; |
||
218 | |||
219 | foreach( $this->getSubClients() as $client ) { |
||
220 | $view->itemBody .= $client->save(); |
||
221 | } |
||
222 | |||
223 | $manager->saveItem( clone $view->item ); |
||
224 | $manager->commit(); |
||
225 | |||
226 | $this->nextAction( $view, $view->param( 'next' ), 'coupon', $view->item->getId(), 'save' ); |
||
227 | return null; |
||
228 | } |
||
229 | catch( \Exception $e ) |
||
230 | { |
||
231 | $manager->rollback(); |
||
232 | $this->report( $e, 'save' ); |
||
233 | } |
||
234 | |||
235 | return $this->create(); |
||
236 | } |
||
237 | |||
238 | |||
239 | /** |
||
240 | * Returns a list of resource according to the conditions |
||
241 | * |
||
242 | * @return string|null HTML output |
||
243 | */ |
||
244 | public function search() : ?string |
||
245 | { |
||
246 | $view = $this->getView(); |
||
247 | |||
248 | try |
||
249 | { |
||
250 | $total = 0; |
||
251 | $params = $this->storeSearchParams( $view->param(), 'coupon' ); |
||
252 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' ); |
||
253 | $search = $this->initCriteria( $manager->createSearch(), $params ); |
||
254 | |||
255 | $view->items = $manager->searchItems( $search, [], $total ); |
||
256 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
257 | $view->filterOperators = $search->getOperators(); |
||
258 | $view->total = $total; |
||
259 | $view->itemBody = ''; |
||
260 | |||
261 | foreach( $this->getSubClients() as $client ) { |
||
262 | $view->itemBody .= $client->search(); |
||
263 | } |
||
264 | } |
||
265 | catch( \Exception $e ) |
||
266 | { |
||
267 | $this->report( $e, 'search' ); |
||
268 | } |
||
269 | |||
270 | /** admin/jqadm/coupon/template-list |
||
271 | * Relative path to the HTML body template for the coupon list. |
||
272 | * |
||
273 | * The template file contains the HTML code and processing instructions |
||
274 | * to generate the result shown in the body of the frontend. The |
||
275 | * configuration string is the path to the template file relative |
||
276 | * to the templates directory (usually in admin/jqadm/templates). |
||
277 | * |
||
278 | * You can overwrite the template file configuration in extensions and |
||
279 | * provide alternative templates. These alternative templates should be |
||
280 | * named like the default one but with the string "default" replaced by |
||
281 | * an unique name. You may use the name of your project for this. If |
||
282 | * you've implemented an alternative client class as well, "default" |
||
283 | * should be replaced by the name of the new class. |
||
284 | * |
||
285 | * @param string Relative path to the template creating the HTML code |
||
286 | * @since 2016.04 |
||
287 | * @category Developer |
||
288 | */ |
||
289 | $tplconf = 'admin/jqadm/coupon/template-list'; |
||
290 | $default = 'coupon/list-standard'; |
||
291 | |||
292 | return $view->render( $view->config( $tplconf, $default ) ); |
||
293 | } |
||
294 | |||
295 | |||
296 | /** |
||
297 | * Returns the sub-client given by its name. |
||
298 | * |
||
299 | * @param string $type Name of the client type |
||
300 | * @param string|null $name Name of the sub-client (Default if null) |
||
301 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
302 | */ |
||
303 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
||
304 | { |
||
305 | /** admin/jqadm/coupon/decorators/excludes |
||
306 | * Excludes decorators added by the "common" option from the coupon JQAdm client |
||
307 | * |
||
308 | * Decorators extend the functionality of a class by adding new aspects |
||
309 | * (e.g. log what is currently done), executing the methods of the underlying |
||
310 | * class only in certain conditions (e.g. only for logged in users) or |
||
311 | * modify what is returned to the caller. |
||
312 | * |
||
313 | * This option allows you to remove a decorator added via |
||
314 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
315 | * around the JQAdm client. |
||
316 | * |
||
317 | * admin/jqadm/coupon/decorators/excludes = array( 'decorator1' ) |
||
318 | * |
||
319 | * This would remove the decorator named "decorator1" from the list of |
||
320 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
321 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
322 | * |
||
323 | * @param array List of decorator names |
||
324 | * @since 2017.07 |
||
325 | * @category Developer |
||
326 | * @see admin/jqadm/common/decorators/default |
||
327 | * @see admin/jqadm/coupon/decorators/global |
||
328 | * @see admin/jqadm/coupon/decorators/local |
||
329 | */ |
||
330 | |||
331 | /** admin/jqadm/coupon/decorators/global |
||
332 | * Adds a list of globally available decorators only to the coupon JQAdm client |
||
333 | * |
||
334 | * Decorators extend the functionality of a class by adding new aspects |
||
335 | * (e.g. log what is currently done), executing the methods of the underlying |
||
336 | * class only in certain conditions (e.g. only for logged in users) or |
||
337 | * modify what is returned to the caller. |
||
338 | * |
||
339 | * This option allows you to wrap global decorators |
||
340 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
341 | * |
||
342 | * admin/jqadm/coupon/decorators/global = array( 'decorator1' ) |
||
343 | * |
||
344 | * This would add the decorator named "decorator1" defined by |
||
345 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
346 | * |
||
347 | * @param array List of decorator names |
||
348 | * @since 2017.07 |
||
349 | * @category Developer |
||
350 | * @see admin/jqadm/common/decorators/default |
||
351 | * @see admin/jqadm/coupon/decorators/excludes |
||
352 | * @see admin/jqadm/coupon/decorators/local |
||
353 | */ |
||
354 | |||
355 | /** admin/jqadm/coupon/decorators/local |
||
356 | * Adds a list of local decorators only to the coupon JQAdm client |
||
357 | * |
||
358 | * Decorators extend the functionality of a class by adding new aspects |
||
359 | * (e.g. log what is currently done), executing the methods of the underlying |
||
360 | * class only in certain conditions (e.g. only for logged in users) or |
||
361 | * modify what is returned to the caller. |
||
362 | * |
||
363 | * This option allows you to wrap local decorators |
||
364 | * ("\Aimeos\Admin\JQAdm\Coupon\Decorator\*") around the JQAdm client. |
||
365 | * |
||
366 | * admin/jqadm/coupon/decorators/local = array( 'decorator2' ) |
||
367 | * |
||
368 | * This would add the decorator named "decorator2" defined by |
||
369 | * "\Aimeos\Admin\JQAdm\Coupon\Decorator\Decorator2" only to the JQAdm client. |
||
370 | * |
||
371 | * @param array List of decorator names |
||
372 | * @since 2017.07 |
||
373 | * @category Developer |
||
374 | * @see admin/jqadm/common/decorators/default |
||
375 | * @see admin/jqadm/coupon/decorators/excludes |
||
376 | * @see admin/jqadm/coupon/decorators/global |
||
377 | */ |
||
378 | return $this->createSubClient( 'coupon/' . $type, $name ); |
||
379 | } |
||
380 | |||
381 | |||
382 | /** |
||
383 | * Returns the backend configuration attributes of the provider and decorators |
||
384 | * |
||
385 | * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item incl. provider/decorator property |
||
386 | * @return \Aimeos\MW\Common\Critera\Attribute\Iface[] List of configuration attributes |
||
387 | */ |
||
388 | public function getConfigAttributes( \Aimeos\MShop\Coupon\Item\Iface $item ) : array |
||
389 | { |
||
390 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' ); |
||
391 | |||
392 | try { |
||
393 | return $manager->getProvider( $item, '' )->getConfigBE(); |
||
394 | } catch( \Aimeos\MShop\Exception $e ) { |
||
395 | return []; |
||
396 | } |
||
397 | } |
||
398 | |||
399 | |||
400 | /** |
||
401 | * Returns the list of sub-client names configured for the client. |
||
402 | * |
||
403 | * @return array List of JQAdm client names |
||
404 | */ |
||
405 | protected function getSubClientNames() : array |
||
441 | } |
||
442 | |||
443 | |||
444 | /** |
||
445 | * Creates new and updates existing items using the data array |
||
446 | * |
||
447 | * @param array $data Data array |
||
448 | * @return \Aimeos\MShop\Coupon\Item\Iface New coupon item object |
||
449 | */ |
||
450 | protected function fromArray( array $data ) : \Aimeos\MShop\Coupon\Item\Iface |
||
451 | { |
||
452 | $conf = []; |
||
453 | |||
454 | if( isset( $data['config']['key'] ) ) |
||
455 | { |
||
456 | foreach( (array) $data['config']['key'] as $idx => $key ) |
||
457 | { |
||
458 | if( trim( $key ) !== '' && isset( $data['config']['val'][$idx] ) ) |
||
459 | { |
||
460 | if( ( $val = json_decode( $data['config']['val'][$idx] ) ) === null ) { |
||
461 | $conf[$key] = $data['config']['val'][$idx]; |
||
462 | } else { |
||
463 | $conf[$key] = $val; |
||
464 | } |
||
465 | } |
||
466 | } |
||
467 | } |
||
468 | |||
469 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' ); |
||
470 | |||
471 | if( isset( $data['coupon.id'] ) && $data['coupon.id'] != '' ) { |
||
472 | $item = $manager->getItem( $data['coupon.id'] ); |
||
473 | } else { |
||
474 | $item = $manager->createItem(); |
||
475 | } |
||
476 | |||
477 | $item->fromArray( $data, true ); |
||
478 | $item->setConfig( $conf ); |
||
479 | |||
480 | return $item; |
||
481 | } |
||
482 | |||
483 | |||
484 | /** |
||
485 | * Constructs the data array for the view from the given item |
||
486 | * |
||
487 | * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object |
||
488 | * @return string[] Multi-dimensional associative list of item data |
||
489 | */ |
||
490 | protected function toArray( \Aimeos\MShop\Coupon\Item\Iface $item, bool $copy = false ) : array |
||
491 | { |
||
492 | $config = $item->getConfig(); |
||
493 | $data = $item->toArray( true ); |
||
494 | $data['config'] = []; |
||
495 | |||
496 | if( $copy === true ) |
||
497 | { |
||
498 | $data['coupon.siteid'] = $this->getContext()->getLocale()->getSiteId(); |
||
499 | $data['coupon.id'] = ''; |
||
500 | } |
||
501 | |||
502 | ksort( $config ); |
||
503 | |||
504 | foreach( $config as $key => $value ) |
||
505 | { |
||
506 | $data['config']['key'][] = $key; |
||
507 | $data['config']['val'][] = $value; |
||
508 | } |
||
509 | |||
510 | return $data; |
||
511 | } |
||
512 | |||
513 | |||
514 | /** |
||
515 | * Returns the rendered template including the view data |
||
516 | * |
||
517 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
518 | * @return string|null HTML output |
||
519 | */ |
||
520 | protected function render( \Aimeos\MW\View\Iface $view ) : string |
||
545 | } |
||
546 | } |
||
547 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.