| Total Complexity | 51 |
| Total Lines | 586 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 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 |
||
| 23 | class Standard |
||
| 24 | extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base |
||
| 25 | implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface |
||
| 26 | { |
||
| 27 | /** admin/jqadm/customer/name |
||
| 28 | * Class name of the used account favorite client implementation |
||
| 29 | * |
||
| 30 | * Each default admin client can be replace by an alternative imlementation. |
||
| 31 | * To use this implementation, you have to set the last part of the class |
||
| 32 | * name as configuration value so the client factory knows which class it |
||
| 33 | * has to instantiate. |
||
| 34 | * |
||
| 35 | * For example, if the name of the default class is |
||
| 36 | * |
||
| 37 | * \Aimeos\Admin\JQAdm\Customer\Standard |
||
| 38 | * |
||
| 39 | * and you want to replace it with your own version named |
||
| 40 | * |
||
| 41 | * \Aimeos\Admin\JQAdm\Customer\Myfavorite |
||
| 42 | * |
||
| 43 | * then you have to set the this configuration option: |
||
| 44 | * |
||
| 45 | * admin/jqadm/customer/name = Myfavorite |
||
| 46 | * |
||
| 47 | * The value is the last part of your own class name and it's case sensitive, |
||
| 48 | * so take care that the configuration value is exactly named like the last |
||
| 49 | * part of the class name. |
||
| 50 | * |
||
| 51 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
| 52 | * characters are possible! You should always start the last part of the class |
||
| 53 | * name with an upper case character and continue only with lower case characters |
||
| 54 | * or numbers. Avoid chamel case names like "MyFavorite"! |
||
| 55 | * |
||
| 56 | * @param string Last part of the class name |
||
| 57 | * @since 2016.01 |
||
| 58 | */ |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Adds the required data used in the template |
||
| 63 | * |
||
| 64 | * @param \Aimeos\Base\View\Iface $view View object |
||
| 65 | * @return \Aimeos\Base\View\Iface View object with assigned parameters |
||
| 66 | */ |
||
| 67 | public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface |
||
| 68 | { |
||
| 69 | $codes = []; |
||
| 70 | |||
| 71 | foreach( $this->context()->config()->get( 'common/countries', [] ) as $code ) { |
||
| 72 | $codes[$code] = $view->translate( 'country', $code ); |
||
| 73 | } |
||
| 74 | |||
| 75 | asort( $codes ); |
||
| 76 | |||
| 77 | $view->itemSubparts = $this->getSubClientNames(); |
||
| 78 | $view->itemGroups = $this->getGroupItems(); |
||
|
|
|||
| 79 | $view->countries = $codes; |
||
| 80 | return $view; |
||
| 81 | } |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Batch update of a resource |
||
| 86 | * |
||
| 87 | * @return string|null Output to display |
||
| 88 | */ |
||
| 89 | public function batch() : ?string |
||
| 90 | { |
||
| 91 | $view = $this->view(); |
||
| 92 | |||
| 93 | if( !empty( $ids = $view->param( 'id' ) ) ) |
||
| 94 | { |
||
| 95 | $context = $this->context(); |
||
| 96 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
| 97 | $filter = $manager->filter()->add( ['customer.id' => $ids] )->slice( 0, count( $ids ) ); |
||
| 98 | $items = $manager->search( $filter ); |
||
| 99 | |||
| 100 | $data = $view->param( 'item', [] ); |
||
| 101 | |||
| 102 | foreach( $items as $item ) |
||
| 103 | { |
||
| 104 | if( $view->access( ['super', 'admin'] ) || $item->getId() === $context->user() ) |
||
| 105 | { |
||
| 106 | !isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] ); |
||
| 107 | !isset( $data['groups'] ) ?: $item->setGroups( array_filter( (array) $data['groups'] ) ); |
||
| 108 | } |
||
| 109 | |||
| 110 | !isset( $data['customer.dateverified'] ) ?: $item->setDateVerified( $data['customer.dateverified'] ); |
||
| 111 | !isset( $data['customer.status'] ) ?: $item->setStatus( $data['customer.status'] ); |
||
| 112 | |||
| 113 | $temp = $data; $item->fromArray( $temp ); |
||
| 114 | } |
||
| 115 | |||
| 116 | $manager->save( $items ); |
||
| 117 | } |
||
| 118 | |||
| 119 | return $this->redirect( 'customer', 'search', null, 'save' ); |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Copies a resource |
||
| 125 | * |
||
| 126 | * @return string|null HTML output |
||
| 127 | */ |
||
| 128 | public function copy() : ?string |
||
| 129 | { |
||
| 130 | $view = $this->object()->data( $this->view() ); |
||
| 131 | |||
| 132 | try |
||
| 133 | { |
||
| 134 | if( ( $id = $view->param( 'id' ) ) === null ) |
||
| 135 | { |
||
| 136 | $msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' ); |
||
| 137 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) ); |
||
| 138 | } |
||
| 139 | |||
| 140 | $manager = \Aimeos\MShop::create( $this->context(), 'customer' ); |
||
| 141 | $view->item = $manager->get( $id, $this->getDomains() ); |
||
| 142 | |||
| 143 | $view->itemData = $this->toArray( $view->item, true ); |
||
| 144 | $view->itemBody = parent::copy(); |
||
| 145 | } |
||
| 146 | catch( \Exception $e ) |
||
| 147 | { |
||
| 148 | $this->report( $e, 'copy' ); |
||
| 149 | } |
||
| 150 | |||
| 151 | return $this->render( $view ); |
||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Creates a new resource |
||
| 157 | * |
||
| 158 | * @return string|null HTML output |
||
| 159 | */ |
||
| 160 | public function create() : ?string |
||
| 183 | } |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * Deletes a resource |
||
| 188 | * |
||
| 189 | * @return string|null HTML output |
||
| 190 | */ |
||
| 191 | public function delete() : ?string |
||
| 245 | } |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns a single resource |
||
| 250 | * |
||
| 251 | * @return string|null HTML output |
||
| 252 | */ |
||
| 253 | public function get() : ?string |
||
| 277 | } |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Saves the data |
||
| 282 | * |
||
| 283 | * @return string|null HTML output |
||
| 284 | */ |
||
| 285 | public function save() : ?string |
||
| 310 | } |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Returns a list of resource according to the conditions |
||
| 315 | * |
||
| 316 | * @return string|null HTML output |
||
| 317 | */ |
||
| 318 | public function search() : ?string |
||
| 319 | { |
||
| 320 | $view = $this->object()->data( $this->view() ); |
||
| 321 | |||
| 322 | try |
||
| 323 | { |
||
| 324 | $total = 0; |
||
| 325 | $params = $this->storeFilter( $view->param(), 'customer' ); |
||
| 326 | $manager = \Aimeos\MShop::create( $this->context(), 'customer' ); |
||
| 327 | $search = $this->initCriteria( $manager->filter(), $params ); |
||
| 328 | |||
| 329 | $view->items = $manager->search( $search, $this->getDomains(), $total ); |
||
| 330 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
| 331 | $view->filterOperators = $search->getOperators(); |
||
| 332 | $view->itemBody = parent::search(); |
||
| 333 | $view->total = $total; |
||
| 334 | } |
||
| 335 | catch( \Exception $e ) |
||
| 336 | { |
||
| 337 | $this->report( $e, 'search' ); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** admin/jqadm/customer/template-list |
||
| 341 | * Relative path to the HTML body template for the customer list. |
||
| 342 | * |
||
| 343 | * The template file contains the HTML code and processing instructions |
||
| 344 | * to generate the result shown in the body of the frontend. The |
||
| 345 | * configuration string is the path to the template file relative |
||
| 346 | * to the templates directory (usually in templates/admin/jqadm). |
||
| 347 | * |
||
| 348 | * You can overwrite the template file configuration in extensions and |
||
| 349 | * provide alternative templates. These alternative templates should be |
||
| 350 | * named like the default one but with the string "default" replaced by |
||
| 351 | * an unique name. You may use the name of your project for this. If |
||
| 352 | * you've implemented an alternative client class as well, "default" |
||
| 353 | * should be replaced by the name of the new class. |
||
| 354 | * |
||
| 355 | * @param string Relative path to the template creating the HTML code |
||
| 356 | * @since 2016.04 |
||
| 357 | */ |
||
| 358 | $tplconf = 'admin/jqadm/customer/template-list'; |
||
| 359 | $default = 'customer/list'; |
||
| 360 | |||
| 361 | return $view->render( $view->config( $tplconf, $default ) ); |
||
| 362 | } |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * Returns the sub-client given by its name. |
||
| 367 | * |
||
| 368 | * @param string $type Name of the client type |
||
| 369 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 370 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
| 371 | */ |
||
| 372 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
||
| 373 | { |
||
| 374 | /** admin/jqadm/customer/decorators/excludes |
||
| 375 | * Excludes decorators added by the "common" option from the customer JQAdm client |
||
| 376 | * |
||
| 377 | * Decorators extend the functionality of a class by adding new aspects |
||
| 378 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 379 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 380 | * modify what is returned to the caller. |
||
| 381 | * |
||
| 382 | * This option allows you to remove a decorator added via |
||
| 383 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
| 384 | * around the JQAdm client. |
||
| 385 | * |
||
| 386 | * admin/jqadm/customer/decorators/excludes = array( 'decorator1' ) |
||
| 387 | * |
||
| 388 | * This would remove the decorator named "decorator1" from the list of |
||
| 389 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
| 390 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
| 391 | * |
||
| 392 | * @param array List of decorator names |
||
| 393 | * @since 2017.07 |
||
| 394 | * @see admin/jqadm/common/decorators/default |
||
| 395 | * @see admin/jqadm/customer/decorators/global |
||
| 396 | * @see admin/jqadm/customer/decorators/local |
||
| 397 | */ |
||
| 398 | |||
| 399 | /** admin/jqadm/customer/decorators/global |
||
| 400 | * Adds a list of globally available decorators only to the customer JQAdm client |
||
| 401 | * |
||
| 402 | * Decorators extend the functionality of a class by adding new aspects |
||
| 403 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 404 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 405 | * modify what is returned to the caller. |
||
| 406 | * |
||
| 407 | * This option allows you to wrap global decorators |
||
| 408 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
| 409 | * |
||
| 410 | * admin/jqadm/customer/decorators/global = array( 'decorator1' ) |
||
| 411 | * |
||
| 412 | * This would add the decorator named "decorator1" defined by |
||
| 413 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
| 414 | * |
||
| 415 | * @param array List of decorator names |
||
| 416 | * @since 2017.07 |
||
| 417 | * @see admin/jqadm/common/decorators/default |
||
| 418 | * @see admin/jqadm/customer/decorators/excludes |
||
| 419 | * @see admin/jqadm/customer/decorators/local |
||
| 420 | */ |
||
| 421 | |||
| 422 | /** admin/jqadm/customer/decorators/local |
||
| 423 | * Adds a list of local decorators only to the customer JQAdm client |
||
| 424 | * |
||
| 425 | * Decorators extend the functionality of a class by adding new aspects |
||
| 426 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 427 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 428 | * modify what is returned to the caller. |
||
| 429 | * |
||
| 430 | * This option allows you to wrap local decorators |
||
| 431 | * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client. |
||
| 432 | * |
||
| 433 | * admin/jqadm/customer/decorators/local = array( 'decorator2' ) |
||
| 434 | * |
||
| 435 | * This would add the decorator named "decorator2" defined by |
||
| 436 | * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client. |
||
| 437 | * |
||
| 438 | * @param array List of decorator names |
||
| 439 | * @since 2017.07 |
||
| 440 | * @see admin/jqadm/common/decorators/default |
||
| 441 | * @see admin/jqadm/customer/decorators/excludes |
||
| 442 | * @see admin/jqadm/customer/decorators/global |
||
| 443 | */ |
||
| 444 | return $this->createSubClient( 'customer/' . $type, $name ); |
||
| 445 | } |
||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * Returns the domain names whose items should be fetched too |
||
| 450 | * |
||
| 451 | * @return string[] List of domain names |
||
| 452 | */ |
||
| 453 | protected function getDomains() : array |
||
| 466 | } |
||
| 467 | |||
| 468 | |||
| 469 | /** |
||
| 470 | * Returns the list of sub-client names configured for the client. |
||
| 471 | * |
||
| 472 | * @return array List of JQAdm client names |
||
| 473 | */ |
||
| 474 | protected function getSubClientNames() : array |
||
| 475 | { |
||
| 476 | /** admin/jqadm/customer/subparts |
||
| 477 | * List of JQAdm sub-clients rendered within the customer section |
||
| 478 | * |
||
| 479 | * The output of the frontend is composed of the code generated by the JQAdm |
||
| 480 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
| 481 | * that are responsible for rendering certain sub-parts of the output. The |
||
| 482 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
| 483 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
| 484 | * the output that is placed inside the container of its parent. |
||
| 485 | * |
||
| 486 | * At first, always the JQAdm code generated by the parent is printed, then |
||
| 487 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
| 488 | * determines the order of the output of these sub-clients inside the parent |
||
| 489 | * container. If the configured list of clients is |
||
| 490 | * |
||
| 491 | * array( "subclient1", "subclient2" ) |
||
| 492 | * |
||
| 493 | * you can easily change the order of the output by reordering the subparts: |
||
| 494 | * |
||
| 495 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
| 496 | * |
||
| 497 | * You can also remove one or more parts if they shouldn't be rendered: |
||
| 498 | * |
||
| 499 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
| 500 | * |
||
| 501 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
| 502 | * should support adding, removing or reordering content by a fluid like |
||
| 503 | * design. |
||
| 504 | * |
||
| 505 | * @param array List of sub-client names |
||
| 506 | * @since 2017.07 |
||
| 507 | */ |
||
| 508 | return $this->context()->config()->get( 'admin/jqadm/customer/subparts', [] ); |
||
| 509 | } |
||
| 510 | |||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * Creates new and updates existing items using the data array |
||
| 515 | * |
||
| 516 | * @param array $data Data array |
||
| 517 | * @return \Aimeos\MShop\Customer\Item\Iface New customer item object |
||
| 518 | */ |
||
| 519 | protected function fromArray( array $data ) : \Aimeos\MShop\Customer\Item\Iface |
||
| 520 | { |
||
| 521 | $context = $this->context(); |
||
| 522 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
| 523 | |||
| 524 | if( isset( $data['customer.id'] ) && $data['customer.id'] != '' ) { |
||
| 525 | $item = $manager->get( $data['customer.id'], $this->getDomains() ); |
||
| 526 | } else { |
||
| 527 | $item = $manager->create(); |
||
| 528 | } |
||
| 529 | |||
| 530 | $addr = $item->getPaymentAddress(); |
||
| 531 | $label = ( $addr->getFirstname() ? $addr->getFirstname() . ' ' : '' ) . $addr->getLastname(); |
||
| 532 | $label .= ( $addr->getCompany() ? ' (' . $addr->getCompany() . ')' : '' ); |
||
| 533 | |||
| 534 | $item->setLabel( $label )->setStatus( $data['customer.status'] ?? 0 ) |
||
| 535 | ->setDateVerified( $data['customer.dateverified'] ?? null ); |
||
| 536 | |||
| 537 | if( $this->view()->access( ['super', 'admin'] ) ) { |
||
| 538 | $item->setGroups( array_unique( $this->val( $data, 'groups', [] ) ) ); |
||
| 539 | } |
||
| 540 | |||
| 541 | if( $this->view()->access( ['super', 'admin'] ) || $item->getId() === $context->user() ) |
||
| 542 | { |
||
| 543 | !isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] ); |
||
| 544 | !isset( $data['customer.code'] ) ?: $item->setCode( $data['customer.code'] ); |
||
| 545 | } |
||
| 546 | |||
| 547 | return $item->fromArray( $data ); |
||
| 548 | } |
||
| 549 | |||
| 550 | |||
| 551 | /** |
||
| 552 | * Constructs the data array for the view from the given item |
||
| 553 | * |
||
| 554 | * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object |
||
| 555 | * @return string[] Multi-dimensional associative list of item data |
||
| 556 | */ |
||
| 557 | protected function toArray( \Aimeos\MShop\Customer\Item\Iface $item, bool $copy = false ) : array |
||
| 576 | } |
||
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * Returns the rendered template including the view data |
||
| 581 | * |
||
| 582 | * @param \Aimeos\Base\View\Iface $view View object with data assigned |
||
| 583 | * @return string HTML output |
||
| 584 | */ |
||
| 585 | protected function render( \Aimeos\Base\View\Iface $view ) : string |
||
| 609 | } |
||
| 610 | } |
||
| 611 |