Total Complexity | 49 |
Total Lines | 601 |
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 | * Copies a resource |
||
28 | * |
||
29 | * @return string HTML output |
||
30 | */ |
||
31 | public function copy() |
||
32 | { |
||
33 | $view = $this->getView(); |
||
34 | $context = $this->getContext(); |
||
35 | |||
36 | try |
||
37 | { |
||
38 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
39 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
40 | } |
||
41 | |||
42 | $manager = \Aimeos\MShop::create( $context, 'coupon' ); |
||
43 | |||
44 | $view->item = $manager->getItem( $id ); |
||
45 | $view->itemData = $this->toArray( $view->item, true ); |
||
46 | $view->itemSubparts = $this->getSubClientNames(); |
||
47 | $view->itemProviders = $this->getProviderNames(); |
||
48 | $view->itemDecorators = $this->getDecoratorNames(); |
||
49 | $view->itemAttributes = $this->getConfigAttributes( $view->item ); |
||
50 | $view->itemBody = ''; |
||
51 | |||
52 | foreach( $this->getSubClients() as $idx => $client ) |
||
53 | { |
||
54 | $view->tabindex = ++$idx + 1; |
||
55 | $view->itemBody .= $client->copy(); |
||
56 | } |
||
57 | } |
||
58 | catch( \Aimeos\MShop\Exception $e ) |
||
59 | { |
||
60 | $error = array( 'coupon-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
61 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
62 | $this->logException( $e ); |
||
63 | } |
||
64 | catch( \Exception $e ) |
||
65 | { |
||
66 | $error = array( 'coupon-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
67 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
68 | $this->logException( $e ); |
||
69 | } |
||
70 | |||
71 | return $this->render( $view ); |
||
72 | } |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Creates a new resource |
||
77 | * |
||
78 | * @return string HTML output |
||
79 | */ |
||
80 | public function create() |
||
121 | } |
||
122 | |||
123 | |||
124 | /** |
||
125 | * Deletes a resource |
||
126 | * |
||
127 | * @return string|null HTML output |
||
128 | */ |
||
129 | public function delete() |
||
180 | } |
||
181 | |||
182 | |||
183 | /** |
||
184 | * Returns a single resource |
||
185 | * |
||
186 | * @return string HTML output |
||
187 | */ |
||
188 | public function get() |
||
189 | { |
||
190 | $view = $this->getView(); |
||
191 | $context = $this->getContext(); |
||
192 | |||
193 | try |
||
194 | { |
||
195 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
196 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
197 | } |
||
198 | |||
199 | $manager = \Aimeos\MShop::create( $context, 'coupon' ); |
||
200 | |||
201 | $view->item = $manager->getItem( $id ); |
||
202 | $view->itemData = $this->toArray( $view->item ); |
||
203 | $view->itemSubparts = $this->getSubClientNames(); |
||
204 | $view->itemDecorators = $this->getDecoratorNames(); |
||
205 | $view->itemProviders = $this->getProviderNames(); |
||
206 | $view->itemAttributes = $this->getConfigAttributes( $view->item ); |
||
207 | $view->itemBody = ''; |
||
208 | |||
209 | foreach( $this->getSubClients() as $idx => $client ) |
||
210 | { |
||
211 | $view->tabindex = ++$idx + 1; |
||
212 | $view->itemBody .= $client->get(); |
||
213 | } |
||
214 | } |
||
215 | catch( \Aimeos\MShop\Exception $e ) |
||
216 | { |
||
217 | $error = array( 'coupon-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
218 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
219 | $this->logException( $e ); |
||
220 | } |
||
221 | catch( \Exception $e ) |
||
222 | { |
||
223 | $error = array( 'coupon-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
224 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
225 | $this->logException( $e ); |
||
226 | } |
||
227 | |||
228 | return $this->render( $view ); |
||
229 | } |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Saves the data |
||
234 | * |
||
235 | * @return string HTML output |
||
236 | */ |
||
237 | public function save() |
||
238 | { |
||
239 | $view = $this->getView(); |
||
240 | $context = $this->getContext(); |
||
241 | |||
242 | $manager = \Aimeos\MShop::create( $context, 'coupon' ); |
||
243 | $manager->begin(); |
||
244 | |||
245 | try |
||
246 | { |
||
247 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
248 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
249 | $view->itemBody = ''; |
||
250 | |||
251 | foreach( $this->getSubClients() as $client ) { |
||
252 | $view->itemBody .= $client->save(); |
||
253 | } |
||
254 | |||
255 | $manager->saveItem( clone $view->item ); |
||
256 | $manager->commit(); |
||
257 | |||
258 | $this->nextAction( $view, $view->param( 'next' ), 'coupon', $view->item->getId(), 'save' ); |
||
259 | return; |
||
260 | } |
||
261 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
262 | { |
||
263 | // fall through to create |
||
264 | } |
||
265 | catch( \Aimeos\MShop\Exception $e ) |
||
266 | { |
||
267 | $error = array( 'coupon-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
268 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
269 | $this->logException( $e ); |
||
270 | } |
||
271 | catch( \Exception $e ) |
||
272 | { |
||
273 | $error = array( 'coupon-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
274 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
275 | $this->logException( $e ); |
||
276 | } |
||
277 | |||
278 | $manager->rollback(); |
||
279 | |||
280 | return $this->create(); |
||
281 | } |
||
282 | |||
283 | |||
284 | /** |
||
285 | * Returns a list of resource according to the conditions |
||
286 | * |
||
287 | * @return string HTML output |
||
288 | */ |
||
289 | public function search() |
||
290 | { |
||
291 | $view = $this->getView(); |
||
292 | $context = $this->getContext(); |
||
293 | |||
294 | try |
||
295 | { |
||
296 | $total = 0; |
||
297 | $params = $this->storeSearchParams( $view->param(), 'coupon' ); |
||
298 | $manager = \Aimeos\MShop::create( $context, 'coupon' ); |
||
299 | $search = $this->initCriteria( $manager->createSearch(), $params ); |
||
300 | |||
301 | $view->items = $manager->searchItems( $search, [], $total ); |
||
302 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
303 | $view->filterOperators = $search->getOperators(); |
||
304 | $view->total = $total; |
||
305 | $view->itemBody = ''; |
||
306 | |||
307 | foreach( $this->getSubClients() as $client ) { |
||
308 | $view->itemBody .= $client->search(); |
||
309 | } |
||
310 | } |
||
311 | catch( \Aimeos\MShop\Exception $e ) |
||
312 | { |
||
313 | $error = array( 'coupon-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
314 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
315 | $this->logException( $e ); |
||
316 | } |
||
317 | catch( \Exception $e ) |
||
318 | { |
||
319 | $error = array( 'coupon-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
320 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
321 | $this->logException( $e ); |
||
322 | } |
||
323 | |||
324 | /** admin/jqadm/coupon/template-list |
||
325 | * Relative path to the HTML body template for the coupon list. |
||
326 | * |
||
327 | * The template file contains the HTML code and processing instructions |
||
328 | * to generate the result shown in the body of the frontend. The |
||
329 | * configuration string is the path to the template file relative |
||
330 | * to the templates directory (usually in admin/jqadm/templates). |
||
331 | * |
||
332 | * You can overwrite the template file configuration in extensions and |
||
333 | * provide alternative templates. These alternative templates should be |
||
334 | * named like the default one but with the string "default" replaced by |
||
335 | * an unique name. You may use the name of your project for this. If |
||
336 | * you've implemented an alternative client class as well, "default" |
||
337 | * should be replaced by the name of the new class. |
||
338 | * |
||
339 | * @param string Relative path to the template creating the HTML code |
||
340 | * @since 2016.04 |
||
341 | * @category Developer |
||
342 | */ |
||
343 | $tplconf = 'admin/jqadm/coupon/template-list'; |
||
344 | $default = 'coupon/list-standard'; |
||
345 | |||
346 | return $view->render( $view->config( $tplconf, $default ) ); |
||
347 | } |
||
348 | |||
349 | |||
350 | /** |
||
351 | * Returns the sub-client given by its name. |
||
352 | * |
||
353 | * @param string $type Name of the client type |
||
354 | * @param string|null $name Name of the sub-client (Default if null) |
||
355 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
356 | */ |
||
357 | public function getSubClient( $type, $name = null ) |
||
358 | { |
||
359 | /** admin/jqadm/coupon/decorators/excludes |
||
360 | * Excludes decorators added by the "common" option from the coupon JQAdm client |
||
361 | * |
||
362 | * Decorators extend the functionality of a class by adding new aspects |
||
363 | * (e.g. log what is currently done), executing the methods of the underlying |
||
364 | * class only in certain conditions (e.g. only for logged in users) or |
||
365 | * modify what is returned to the caller. |
||
366 | * |
||
367 | * This option allows you to remove a decorator added via |
||
368 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
369 | * around the JQAdm client. |
||
370 | * |
||
371 | * admin/jqadm/coupon/decorators/excludes = array( 'decorator1' ) |
||
372 | * |
||
373 | * This would remove the decorator named "decorator1" from the list of |
||
374 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
375 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
376 | * |
||
377 | * @param array List of decorator names |
||
378 | * @since 2017.07 |
||
379 | * @category Developer |
||
380 | * @see admin/jqadm/common/decorators/default |
||
381 | * @see admin/jqadm/coupon/decorators/global |
||
382 | * @see admin/jqadm/coupon/decorators/local |
||
383 | */ |
||
384 | |||
385 | /** admin/jqadm/coupon/decorators/global |
||
386 | * Adds a list of globally available decorators only to the coupon JQAdm client |
||
387 | * |
||
388 | * Decorators extend the functionality of a class by adding new aspects |
||
389 | * (e.g. log what is currently done), executing the methods of the underlying |
||
390 | * class only in certain conditions (e.g. only for logged in users) or |
||
391 | * modify what is returned to the caller. |
||
392 | * |
||
393 | * This option allows you to wrap global decorators |
||
394 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
395 | * |
||
396 | * admin/jqadm/coupon/decorators/global = array( 'decorator1' ) |
||
397 | * |
||
398 | * This would add the decorator named "decorator1" defined by |
||
399 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
400 | * |
||
401 | * @param array List of decorator names |
||
402 | * @since 2017.07 |
||
403 | * @category Developer |
||
404 | * @see admin/jqadm/common/decorators/default |
||
405 | * @see admin/jqadm/coupon/decorators/excludes |
||
406 | * @see admin/jqadm/coupon/decorators/local |
||
407 | */ |
||
408 | |||
409 | /** admin/jqadm/coupon/decorators/local |
||
410 | * Adds a list of local decorators only to the coupon JQAdm client |
||
411 | * |
||
412 | * Decorators extend the functionality of a class by adding new aspects |
||
413 | * (e.g. log what is currently done), executing the methods of the underlying |
||
414 | * class only in certain conditions (e.g. only for logged in users) or |
||
415 | * modify what is returned to the caller. |
||
416 | * |
||
417 | * This option allows you to wrap local decorators |
||
418 | * ("\Aimeos\Admin\JQAdm\Coupon\Decorator\*") around the JQAdm client. |
||
419 | * |
||
420 | * admin/jqadm/coupon/decorators/local = array( 'decorator2' ) |
||
421 | * |
||
422 | * This would add the decorator named "decorator2" defined by |
||
423 | * "\Aimeos\Admin\JQAdm\Coupon\Decorator\Decorator2" only to the JQAdm client. |
||
424 | * |
||
425 | * @param array List of decorator names |
||
426 | * @since 2017.07 |
||
427 | * @category Developer |
||
428 | * @see admin/jqadm/common/decorators/default |
||
429 | * @see admin/jqadm/coupon/decorators/excludes |
||
430 | * @see admin/jqadm/coupon/decorators/global |
||
431 | */ |
||
432 | return $this->createSubClient( 'coupon/' . $type, $name ); |
||
433 | } |
||
434 | |||
435 | |||
436 | /** |
||
437 | * Returns the backend configuration attributes of the provider and decorators |
||
438 | * |
||
439 | * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item incl. provider/decorator property |
||
440 | * @return \Aimeos\MW\Common\Critera\Attribute\Iface[] List of configuration attributes |
||
441 | */ |
||
442 | public function getConfigAttributes( \Aimeos\MShop\Coupon\Item\Iface $item ) |
||
443 | { |
||
444 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' ); |
||
445 | |||
446 | try { |
||
447 | return $manager->getProvider( $item, '' )->getConfigBE(); |
||
448 | } catch( \Aimeos\MShop\Exception $e ) { |
||
449 | return []; |
||
450 | } |
||
451 | } |
||
452 | |||
453 | |||
454 | /** |
||
455 | * Returns the list of sub-client names configured for the client. |
||
456 | * |
||
457 | * @return array List of JQAdm client names |
||
458 | */ |
||
459 | protected function getSubClientNames() |
||
495 | } |
||
496 | |||
497 | |||
498 | /** |
||
499 | * Returns the names of the available coupon decorators |
||
500 | * |
||
501 | * @return string[] List of decorator class names |
||
502 | */ |
||
503 | protected function getDecoratorNames() |
||
504 | { |
||
505 | $ds = DIRECTORY_SEPARATOR; |
||
506 | return $this->getClassNames( 'MShop' . $ds . 'Coupon' . $ds . 'Provider' . $ds . 'Decorator' ); |
||
507 | } |
||
508 | |||
509 | |||
510 | /** |
||
511 | * Returns the names of the available coupon providers |
||
512 | * |
||
513 | * @return string[] List of provider class names |
||
514 | */ |
||
515 | protected function getProviderNames() |
||
516 | { |
||
517 | $ds = DIRECTORY_SEPARATOR; |
||
518 | return $this->getClassNames( 'MShop' . $ds . 'Coupon' . $ds . 'Provider' ); |
||
519 | } |
||
520 | |||
521 | |||
522 | /** |
||
523 | * Creates new and updates existing items using the data array |
||
524 | * |
||
525 | * @param array $data Data array |
||
526 | * @return \Aimeos\MShop\Coupon\Item\Iface New coupon item object |
||
527 | */ |
||
528 | protected function fromArray( array $data ) |
||
529 | { |
||
530 | $conf = []; |
||
531 | |||
532 | if( isset( $data['config']['key'] ) ) |
||
533 | { |
||
534 | foreach( (array) $data['config']['key'] as $idx => $key ) |
||
535 | { |
||
536 | if( trim( $key ) !== '' && isset( $data['config']['val'][$idx] ) ) |
||
537 | { |
||
538 | if( ( $val = json_decode( $data['config']['val'][$idx] ) ) === null ) { |
||
539 | $conf[$key] = $data['config']['val'][$idx]; |
||
540 | } else { |
||
541 | $conf[$key] = $val; |
||
542 | } |
||
543 | } |
||
544 | } |
||
545 | } |
||
546 | |||
547 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' ); |
||
548 | |||
549 | if( isset( $data['coupon.id'] ) && $data['coupon.id'] != '' ) { |
||
550 | $item = $manager->getItem( $data['coupon.id'] ); |
||
551 | } else { |
||
552 | $item = $manager->createItem(); |
||
553 | } |
||
554 | |||
555 | $item->fromArray( $data, true ); |
||
556 | $item->setConfig( $conf ); |
||
557 | |||
558 | return $item; |
||
559 | } |
||
560 | |||
561 | |||
562 | /** |
||
563 | * Constructs the data array for the view from the given item |
||
564 | * |
||
565 | * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object |
||
566 | * @return string[] Multi-dimensional associative list of item data |
||
567 | */ |
||
568 | protected function toArray( \Aimeos\MShop\Coupon\Item\Iface $item, $copy = false ) |
||
569 | { |
||
570 | $config = $item->getConfig(); |
||
571 | $data = $item->toArray( true ); |
||
572 | $data['config'] = []; |
||
573 | |||
574 | if( $copy === true ) |
||
575 | { |
||
576 | $data['coupon.siteid'] = $this->getContext()->getLocale()->getSiteId(); |
||
577 | $data['coupon.id'] = ''; |
||
578 | } |
||
579 | |||
580 | ksort( $config ); |
||
581 | |||
582 | foreach( $config as $key => $value ) |
||
583 | { |
||
584 | $data['config']['key'][] = $key; |
||
585 | $data['config']['val'][] = $value; |
||
586 | } |
||
587 | |||
588 | return $data; |
||
589 | } |
||
590 | |||
591 | |||
592 | /** |
||
593 | * Returns the rendered template including the view data |
||
594 | * |
||
595 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
596 | * @return string HTML output |
||
597 | */ |
||
598 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
623 | } |
||
624 | } |
||
625 |
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.