| Total Complexity | 49 |
| Total Lines | 625 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| 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/order/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\Order\Standard |
||
| 38 | * |
||
| 39 | * and you want to replace it with your own version named |
||
| 40 | * |
||
| 41 | * \Aimeos\Admin\JQAdm\Order\Myfavorite |
||
| 42 | * |
||
| 43 | * then you have to set the this configuration option: |
||
| 44 | * |
||
| 45 | * admin/jqadm/order/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->countries = $codes; |
||
| 79 | return $view; |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * Batch update of a resource |
||
| 85 | * |
||
| 86 | * @return string|null Output to display |
||
| 87 | */ |
||
| 88 | public function batch() : ?string |
||
| 89 | { |
||
| 90 | return $this->batchBase( 'order' ); |
||
|
|
|||
| 91 | } |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Copies a resource |
||
| 96 | * |
||
| 97 | * @return string|null HTML output |
||
| 98 | */ |
||
| 99 | public function copy() : ?string |
||
| 100 | { |
||
| 101 | $context = $this->context(); |
||
| 102 | $view = $this->object()->data( $this->view() ); |
||
| 103 | |||
| 104 | try |
||
| 105 | { |
||
| 106 | if( ( $id = $view->param( 'id' ) ) === null ) |
||
| 107 | { |
||
| 108 | $msg = $context->translate( 'admin', 'Required parameter "%1$s" is missing' ); |
||
| 109 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) ); |
||
| 110 | } |
||
| 111 | |||
| 112 | $manager = \Aimeos\MShop::create( $context, 'order' ); |
||
| 113 | $refs = $context->config()->get( 'mshop/order/manager/subdomains', [] ); |
||
| 114 | |||
| 115 | $view->item = $manager->get( $id, $refs ); |
||
| 116 | $view->itemData = $this->toArray( $view->item, true ); |
||
| 117 | $view->itemBody = parent::copy(); |
||
| 118 | } |
||
| 119 | catch( \Exception $e ) |
||
| 120 | { |
||
| 121 | $this->report( $e, 'copy' ); |
||
| 122 | } |
||
| 123 | |||
| 124 | return $this->render( $view ); |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * Creates a new resource |
||
| 130 | * |
||
| 131 | * @return string|null HTML output |
||
| 132 | */ |
||
| 133 | public function create() : ?string |
||
| 134 | { |
||
| 135 | $view = $this->object()->data( $this->view() ); |
||
| 136 | |||
| 137 | try |
||
| 138 | { |
||
| 139 | $data = $view->param( 'item', [] ); |
||
| 140 | |||
| 141 | if( !isset( $view->item ) ) { |
||
| 142 | $view->item = \Aimeos\MShop::create( $this->context(), 'order' )->create(); |
||
| 143 | } |
||
| 144 | |||
| 145 | $data['order.siteid'] = $view->item->getSiteId(); |
||
| 146 | |||
| 147 | $view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data ); |
||
| 148 | $view->itemBody = parent::create(); |
||
| 149 | } |
||
| 150 | catch( \Exception $e ) |
||
| 151 | { |
||
| 152 | $this->report( $e, 'create' ); |
||
| 153 | } |
||
| 154 | |||
| 155 | return $this->render( $view ); |
||
| 156 | } |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * Exports a resource |
||
| 161 | * |
||
| 162 | * @return string Admin output to display |
||
| 163 | */ |
||
| 164 | public function export() : ?string |
||
| 195 | } |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns a single resource |
||
| 200 | * |
||
| 201 | * @return string|null HTML output |
||
| 202 | */ |
||
| 203 | public function get() : ?string |
||
| 204 | { |
||
| 205 | $context = $this->context(); |
||
| 206 | $view = $this->object()->data( $this->view() ); |
||
| 207 | |||
| 208 | try |
||
| 209 | { |
||
| 210 | if( ( $id = $view->param( 'id' ) ) === null ) |
||
| 211 | { |
||
| 212 | $msg = $context->translate( 'admin', 'Required parameter "%1$s" is missing' ); |
||
| 213 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) ); |
||
| 214 | } |
||
| 215 | |||
| 216 | $manager = \Aimeos\MShop::create( $context, 'order' ); |
||
| 217 | $refs = $context->config()->get( 'mshop/order/manager/subdomains', [] ); |
||
| 218 | |||
| 219 | $view->item = $manager->get( $id, $refs ); |
||
| 220 | $view->itemData = $this->toArray( $view->item ); |
||
| 221 | $view->itemBody = parent::get(); |
||
| 222 | } |
||
| 223 | catch( \Exception $e ) |
||
| 224 | { |
||
| 225 | $this->report( $e, 'get' ); |
||
| 226 | } |
||
| 227 | |||
| 228 | return $this->render( $view ); |
||
| 229 | } |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * Saves the data |
||
| 234 | * |
||
| 235 | * @return string|null HTML output |
||
| 236 | */ |
||
| 237 | public function save() : ?string |
||
| 238 | { |
||
| 239 | $view = $this->view(); |
||
| 240 | |||
| 241 | $manager = \Aimeos\MShop::create( $this->context(), 'order' ); |
||
| 242 | $manager->begin(); |
||
| 243 | |||
| 244 | try |
||
| 245 | { |
||
| 246 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
| 247 | $view->item = $item->getId() ? $item : $manager->save( clone $item ); |
||
| 248 | $view->itemBody = parent::save(); |
||
| 249 | |||
| 250 | $manager->save( clone $view->item ); |
||
| 251 | $manager->commit(); |
||
| 252 | |||
| 253 | return $this->redirect( 'order', $view->param( 'next' ), $view->item->getId(), 'save' ); |
||
| 254 | } |
||
| 255 | catch( \Exception $e ) |
||
| 256 | { |
||
| 257 | $manager->rollback(); |
||
| 258 | $this->report( $e, 'save' ); |
||
| 259 | } |
||
| 260 | |||
| 261 | return $this->create(); |
||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * Returns a list of resource according to the conditions |
||
| 267 | * |
||
| 268 | * @return string|null HTML output |
||
| 269 | */ |
||
| 270 | public function search() : ?string |
||
| 271 | { |
||
| 272 | $view = $this->view(); |
||
| 273 | |||
| 274 | try |
||
| 275 | { |
||
| 276 | $total = 0; |
||
| 277 | $context = $this->context(); |
||
| 278 | $refs = $context->config()->get( 'mshop/order/manager/subdomains', [] ); |
||
| 279 | |||
| 280 | $manager = \Aimeos\MShop::create( $context, 'order' ); |
||
| 281 | $params = $this->storeFilter( $view->param(), 'order' ); |
||
| 282 | |||
| 283 | $search = $manager->filter( false, true )->order( '-order.id' ); |
||
| 284 | $search = $this->initCriteria( $search, $params ); |
||
| 285 | |||
| 286 | $view->items = $manager->search( $search, $refs, $total ); |
||
| 287 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
| 288 | $view->filterOperators = $search->getOperators(); |
||
| 289 | $view->itemBody = parent::search(); |
||
| 290 | $view->total = $total; |
||
| 291 | } |
||
| 292 | catch( \Exception $e ) |
||
| 293 | { |
||
| 294 | $this->report( $e, 'search' ); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** admin/jqadm/order/template-list |
||
| 298 | * Relative path to the HTML body template for the order list. |
||
| 299 | * |
||
| 300 | * The template file contains the HTML code and processing instructions |
||
| 301 | * to generate the result shown in the body of the frontend. The |
||
| 302 | * configuration string is the path to the template file relative |
||
| 303 | * to the templates directory (usually in templates/admin/jqadm). |
||
| 304 | * |
||
| 305 | * You can overwrite the template file configuration in extensions and |
||
| 306 | * provide alternative templates. These alternative templates should be |
||
| 307 | * named like the default one but with the string "default" replaced by |
||
| 308 | * an unique name. You may use the name of your project for this. If |
||
| 309 | * you've implemented an alternative client class as well, "default" |
||
| 310 | * should be replaced by the name of the new class. |
||
| 311 | * |
||
| 312 | * @param string Relative path to the template creating the HTML code |
||
| 313 | * @since 2016.04 |
||
| 314 | */ |
||
| 315 | $tplconf = 'admin/jqadm/order/template-list'; |
||
| 316 | $default = 'order/list'; |
||
| 317 | |||
| 318 | return $view->render( $view->config( $tplconf, $default ) ); |
||
| 319 | } |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the sub-client given by its name. |
||
| 324 | * |
||
| 325 | * @param string $type Name of the client type |
||
| 326 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 327 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
| 328 | */ |
||
| 329 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
||
| 402 | } |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * Returns the list of sub-client names configured for the client. |
||
| 407 | * |
||
| 408 | * @return array List of JQAdm client names |
||
| 409 | */ |
||
| 410 | protected function getSubClientNames() : array |
||
| 445 | } |
||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * Creates new and updates existing items using the data array |
||
| 450 | * |
||
| 451 | * @param array $data Data array |
||
| 452 | * @return \Aimeos\MShop\Order\Item\Iface New order item object |
||
| 453 | */ |
||
| 454 | protected function fromArray( array $data ) : \Aimeos\MShop\Order\Item\Iface |
||
| 455 | { |
||
| 456 | $context = $this->context(); |
||
| 457 | |||
| 458 | $manager = \Aimeos\MShop::create( $context, 'order' ); |
||
| 459 | $attrManager = \Aimeos\MShop::create( $context, 'order/service/attribute' ); |
||
| 460 | |||
| 461 | if( isset( $data['order.id'] ) ) { |
||
| 462 | $refs = $context->config()->get( 'mshop/order/manager/subdomains', [] ); |
||
| 463 | $basket = $manager->get( $data['order.id'], $refs )->off(); |
||
| 464 | } else { |
||
| 465 | $basket = $manager->create()->off(); |
||
| 466 | } |
||
| 467 | |||
| 468 | $basket->fromArray( $data, true ); |
||
| 469 | $allowed = array_flip( [ |
||
| 470 | 'order.product.statusdelivery', |
||
| 471 | 'order.product.statuspayment', |
||
| 472 | 'order.product.qtyopen', |
||
| 473 | 'order.product.timeframe', |
||
| 474 | 'order.product.notes', |
||
| 475 | ] ); |
||
| 476 | |||
| 477 | foreach( $basket->getProducts() as $pos => $product ) |
||
| 478 | { |
||
| 479 | $list = array_intersect_key( $data['product'][$pos], $allowed ); |
||
| 480 | $product->fromArray( $list ); |
||
| 481 | } |
||
| 482 | |||
| 483 | foreach( $basket->getAddresses() as $type => $addresses ) |
||
| 484 | { |
||
| 485 | foreach( $addresses as $pos => $address ) |
||
| 486 | { |
||
| 487 | if( isset( $data['address'][$type][$pos] ) ) { |
||
| 488 | $list = (array) $data['address'][$type][$pos]; |
||
| 489 | $basket->addAddress( $address->fromArray( $list, true ), $type, $pos ); |
||
| 490 | } else { |
||
| 491 | $basket->deleteAddress( $type, $pos ); |
||
| 492 | } |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | foreach( $basket->getServices() as $type => $services ) |
||
| 497 | { |
||
| 498 | foreach( $services as $service ) |
||
| 499 | { |
||
| 500 | $serviceId = $service->getId(); |
||
| 501 | $attrItems = $service->getAttributeItems(); |
||
| 502 | |||
| 503 | foreach( $data['service'][$type][$serviceId] ?? [] as $idx => $entry ) |
||
| 504 | { |
||
| 505 | $entry = array_filter( $entry ); |
||
| 506 | $id = $entry['order.service.attribute.id'] ?? ''; |
||
| 507 | $attrItem = $attrItems[$id] ?? $attrManager->create(); |
||
| 508 | |||
| 509 | $attrManager->save( $attrItem->fromArray( $entry, true )->setParentId( $service->getId() ) ); |
||
| 510 | unset( $attrItems[$id] ); |
||
| 511 | } |
||
| 512 | |||
| 513 | $attrManager->delete( $attrItems ); |
||
| 514 | } |
||
| 515 | } |
||
| 516 | |||
| 517 | return $basket; |
||
| 518 | } |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * Constructs the data array for the view from the given item |
||
| 523 | * |
||
| 524 | * @param \Aimeos\MShop\Order\Item\Iface $item Order item object |
||
| 525 | * @return string[] Multi-dimensional associative list of item data |
||
| 526 | */ |
||
| 527 | protected function toArray( \Aimeos\MShop\Order\Item\Iface $item, bool $copy = false ) : array |
||
| 528 | { |
||
| 529 | $siteId = $this->context()->locale()->getSiteId(); |
||
| 530 | $data = $item->toArray( true ); |
||
| 531 | |||
| 532 | if( $item->getCustomerId() != '' ) |
||
| 533 | { |
||
| 534 | try { |
||
| 535 | $data += \Aimeos\MShop::create( $this->context(), 'customer' )->get( $item->getCustomerId() )->toArray(); |
||
| 536 | } catch( \Exception $e ) {}; |
||
| 537 | } |
||
| 538 | |||
| 539 | |||
| 540 | if( $copy === true ) |
||
| 541 | { |
||
| 542 | $data['order.siteid'] = $siteId; |
||
| 543 | $data['order.id'] = ''; |
||
| 544 | } |
||
| 545 | |||
| 546 | foreach( $item->getAddresses() as $type => $addresses ) |
||
| 547 | { |
||
| 548 | foreach( $addresses as $pos => $addrItem ) |
||
| 549 | { |
||
| 550 | $data['address'][$type][$pos] = $addrItem->toArray( true ); |
||
| 551 | |||
| 552 | if( $copy === true ) |
||
| 553 | { |
||
| 554 | $data['address'][$type][$pos]['order.address.siteid'] = $siteId; |
||
| 555 | $data['address'][$type][$pos]['order.address.id'] = ''; |
||
| 556 | } |
||
| 557 | } |
||
| 558 | } |
||
| 559 | |||
| 560 | foreach( $item->getProducts() as $pos => $productItem ) |
||
| 561 | { |
||
| 562 | $data['product'][$pos] = $productItem->toArray( true ); |
||
| 563 | $data['product'][$pos]['attributes'] = []; |
||
| 564 | |||
| 565 | foreach( $productItem->getAttributeItems() as $attrItem ) |
||
| 566 | { |
||
| 567 | $entry = $attrItem->toArray( true ); |
||
| 568 | |||
| 569 | if( $copy === true ) |
||
| 570 | { |
||
| 571 | $entry['order.product.attribute.siteid'] = $siteId; |
||
| 572 | $entry['order.product.attribute.id'] = ''; |
||
| 573 | } |
||
| 574 | |||
| 575 | $data['product'][$pos]['attributes'][] = $entry; |
||
| 576 | } |
||
| 577 | |||
| 578 | if( $copy === true ) |
||
| 579 | { |
||
| 580 | $data['product'][$pos]['order.product.siteid'] = $siteId; |
||
| 581 | $data['product'][$pos]['order.product.id'] = ''; |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | foreach( $item->getServices() as $type => $services ) |
||
| 586 | { |
||
| 587 | foreach( $services as $serviceItem ) |
||
| 588 | { |
||
| 589 | $serviceId = $serviceItem->getId(); |
||
| 590 | $data['service'][$type][$serviceId] = $serviceItem->toArray( true ); |
||
| 591 | $data['service'][$type][$serviceId]['attributes'] = []; |
||
| 592 | |||
| 593 | foreach( $serviceItem->getAttributeItems() as $attrItem ) |
||
| 594 | { |
||
| 595 | $entry = $attrItem->toArray( true ); |
||
| 596 | |||
| 597 | if( $copy === true ) |
||
| 598 | { |
||
| 599 | $entry['order.service.attribute.siteid'] = $siteId; |
||
| 600 | $entry['order.service.attribute.id'] = ''; |
||
| 601 | } |
||
| 602 | |||
| 603 | $data['service'][$type][$serviceId]['attributes'][] = $entry; |
||
| 604 | } |
||
| 605 | |||
| 606 | if( $copy === true ) |
||
| 607 | { |
||
| 608 | $data['service'][$type][$serviceId]['order.service.siteid'] = $siteId; |
||
| 609 | $data['service'][$type][$serviceId]['order.service.id'] = ''; |
||
| 610 | } |
||
| 611 | } |
||
| 612 | } |
||
| 613 | |||
| 614 | return $data; |
||
| 615 | } |
||
| 616 | |||
| 617 | |||
| 618 | /** |
||
| 619 | * Returns the rendered template including the view data |
||
| 620 | * |
||
| 621 | * @param \Aimeos\Base\View\Iface $view View object with data assigned |
||
| 622 | * @return string HTML output |
||
| 623 | */ |
||
| 624 | protected function render( \Aimeos\Base\View\Iface $view ) : string |
||
| 648 | } |
||
| 649 | } |
||
| 650 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.