| Total Complexity | 41 |
| Total Lines | 596 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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/product/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\Product\Standard |
||
| 38 | * |
||
| 39 | * and you want to replace it with your own version named |
||
| 40 | * |
||
| 41 | * \Aimeos\Admin\JQAdm\Product\Myfavorite |
||
| 42 | * |
||
| 43 | * then you have to set the this configuration option: |
||
| 44 | * |
||
| 45 | * admin/jqadm/product/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 |
||
| 72 | } |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * Batch update of a resource |
||
| 77 | * |
||
| 78 | * @return string|null Output to display |
||
| 79 | */ |
||
| 80 | public function batch() : ?string |
||
| 81 | { |
||
| 82 | $view = $this->view(); |
||
| 83 | |||
| 84 | if( !empty( $ids = $view->param( 'id' ) ) ) |
||
| 85 | { |
||
| 86 | $manager = \Aimeos\MShop::create( $this->context(), 'index' ); |
||
| 87 | $filter = $manager->filter()->add( ['product.id' => $ids] )->slice( 0, count( $ids ) ); |
||
| 88 | $items = $manager->search( $filter, $this->getDomains() ); |
||
| 89 | |||
| 90 | $data = $view->param( 'item', [] ); |
||
| 91 | |||
| 92 | foreach( $items as $item ) { |
||
| 93 | $temp = $data; $item->fromArray( $temp, true ); |
||
| 94 | } |
||
| 95 | |||
| 96 | $view->items = $items; |
||
| 97 | |||
| 98 | foreach( $this->getSubClients() as $client ) { |
||
| 99 | $client->batch(); |
||
| 100 | } |
||
| 101 | |||
| 102 | $manager->save( $items ); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $this->redirect( 'product', 'search', null, 'save' ); |
||
| 106 | } |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Copies a resource |
||
| 111 | * |
||
| 112 | * @return string|null HTML output |
||
| 113 | */ |
||
| 114 | public function copy() : ?string |
||
| 115 | { |
||
| 116 | $view = $this->object()->data( $this->view() ); |
||
| 117 | |||
| 118 | try |
||
| 119 | { |
||
| 120 | if( ( $id = $view->param( 'id' ) ) === null ) |
||
| 121 | { |
||
| 122 | $msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' ); |
||
| 123 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) ); |
||
| 124 | } |
||
| 125 | |||
| 126 | $manager = \Aimeos\MShop::create( $this->context(), 'product' ); |
||
| 127 | $view->item = $manager->get( $id, $this->getDomains() ); |
||
| 128 | |||
| 129 | $view->itemData = $this->toArray( $view->item, true ); |
||
| 130 | $view->itemBody = parent::copy(); |
||
| 131 | } |
||
| 132 | catch( \Exception $e ) |
||
| 133 | { |
||
| 134 | $this->report( $e, 'copy' ); |
||
| 135 | } |
||
| 136 | |||
| 137 | return $this->render( $view ); |
||
| 138 | } |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * Creates a new resource |
||
| 143 | * |
||
| 144 | * @return string|null HTML output |
||
| 145 | */ |
||
| 146 | public function create() : ?string |
||
| 169 | } |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Deletes a resource |
||
| 174 | * |
||
| 175 | * @return string|null HTML output |
||
| 176 | */ |
||
| 177 | public function delete() : ?string |
||
| 219 | } |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns a single resource |
||
| 224 | * |
||
| 225 | * @return string|null HTML output |
||
| 226 | */ |
||
| 227 | public function get() : ?string |
||
| 251 | } |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * Imports a file |
||
| 256 | * |
||
| 257 | * @return string|null Output to display or redirect |
||
| 258 | */ |
||
| 259 | public function import() : ?string |
||
| 260 | { |
||
| 261 | $context = $this->context(); |
||
| 262 | $fs = $context->fs( 'fs-import' ); |
||
| 263 | $site = $context->locale()->getSiteCode(); |
||
|
|
|||
| 264 | $dir = $context->config()->get( 'controller/jobs/product/import/csv/location', 'product' ); |
||
| 265 | |||
| 266 | if( $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $dir . '/' . $site ) === false ) { |
||
| 267 | $fs->mkdir( $dir . '/' . $site ); |
||
| 268 | } |
||
| 269 | |||
| 270 | $uploads = (array) $this->view()->request()->getUploadedFiles(); |
||
| 271 | $files = $this->val( $uploads, 'import' ); |
||
| 272 | |||
| 273 | foreach( is_array( $files ) ? $files : [$files] as $idx => $file ) |
||
| 274 | { |
||
| 275 | $filename = date( 'YmdHis' ) . '_' . str_pad( $idx, 3, '0', STR_PAD_LEFT ) . '_' . substr( md5( microtime( true ) ), 0, 4 ) . '.csv'; |
||
| 276 | $fs->writes( $dir . '/' . $site . '/' . $filename, $file->getStream()->detach() ); |
||
| 277 | } |
||
| 278 | |||
| 279 | return $this->redirect( 'product', 'search', null, 'upload' ); |
||
| 280 | } |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * Saves the data |
||
| 285 | * |
||
| 286 | * @return string|null HTML output |
||
| 287 | */ |
||
| 288 | public function save() : ?string |
||
| 289 | { |
||
| 290 | $view = $this->view(); |
||
| 291 | $context = $this->context(); |
||
| 292 | |||
| 293 | $manager = \Aimeos\MShop::create( $context, 'index' ); |
||
| 294 | $manager->begin(); |
||
| 295 | |||
| 296 | try |
||
| 297 | { |
||
| 298 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
| 299 | $view->item = $item->getId() ? $item : $manager->save( $item ); |
||
| 300 | $view->itemBody = parent::save(); |
||
| 301 | |||
| 302 | $item = $manager->save( clone $view->item ); |
||
| 303 | $manager->commit(); |
||
| 304 | |||
| 305 | return $this->redirect( 'product', $view->param( 'next' ), $view->item->getId(), 'save' ); |
||
| 306 | } |
||
| 307 | catch( \Exception $e ) |
||
| 308 | { |
||
| 309 | $manager->rollback(); |
||
| 310 | $this->report( $e, 'save' ); |
||
| 311 | } |
||
| 312 | |||
| 313 | return $this->create(); |
||
| 314 | } |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns a list of resource according to the conditions |
||
| 319 | * |
||
| 320 | * @return string|null HTML output |
||
| 321 | */ |
||
| 322 | public function search() : ?string |
||
| 323 | { |
||
| 324 | $view = $this->view(); |
||
| 325 | |||
| 326 | try |
||
| 327 | { |
||
| 328 | $total = 0; |
||
| 329 | $domains = map( $this->getDomains() )->remove( 'product' ); |
||
| 330 | $params = $this->storeFilter( $view->param(), 'product' ); |
||
| 331 | $manager = \Aimeos\MShop::create( $this->context(), 'product' ); |
||
| 332 | |||
| 333 | $search = $manager->filter()->order( 'product.id' ); |
||
| 334 | $search = $this->initCriteria( $search, $params ); |
||
| 335 | |||
| 336 | $view->items = $manager->search( $search, $domains->toArray(), $total ); |
||
| 337 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
| 338 | $view->filterOperators = $search->getOperators(); |
||
| 339 | $view->itemTypes = $this->getTypeItems(); |
||
| 340 | $view->itemBody = parent::search(); |
||
| 341 | $view->total = $total; |
||
| 342 | } |
||
| 343 | catch( \Exception $e ) |
||
| 344 | { |
||
| 345 | $this->report( $e, 'search' ); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** admin/jqadm/product/template-list |
||
| 349 | * Relative path to the HTML body template for the product list. |
||
| 350 | * |
||
| 351 | * The template file contains the HTML code and processing instructions |
||
| 352 | * to generate the result shown in the body of the frontend. The |
||
| 353 | * configuration string is the path to the template file relative |
||
| 354 | * to the templates directory (usually in templates/admin/jqadm). |
||
| 355 | * |
||
| 356 | * You can overwrite the template file configuration in extensions and |
||
| 357 | * provide alternative templates. These alternative templates should be |
||
| 358 | * named like the default one but with the string "default" replaced by |
||
| 359 | * an unique name. You may use the name of your project for this. If |
||
| 360 | * you've implemented an alternative client class as well, "default" |
||
| 361 | * should be replaced by the name of the new class. |
||
| 362 | * |
||
| 363 | * @param string Relative path to the template creating the HTML code |
||
| 364 | * @since 2016.04 |
||
| 365 | */ |
||
| 366 | $tplconf = 'admin/jqadm/product/template-list'; |
||
| 367 | $default = 'product/list'; |
||
| 368 | |||
| 369 | return $view->render( $view->config( $tplconf, $default ) ); |
||
| 370 | } |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Returns the sub-client given by its name. |
||
| 375 | * |
||
| 376 | * @param string $type Name of the client type |
||
| 377 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 378 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
| 379 | */ |
||
| 380 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
||
| 381 | { |
||
| 382 | /** admin/jqadm/product/decorators/excludes |
||
| 383 | * Excludes decorators added by the "common" option from the product JQAdm client |
||
| 384 | * |
||
| 385 | * Decorators extend the functionality of a class by adding new aspects |
||
| 386 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 387 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 388 | * modify what is returned to the caller. |
||
| 389 | * |
||
| 390 | * This option allows you to remove a decorator added via |
||
| 391 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
| 392 | * around the JQAdm client. |
||
| 393 | * |
||
| 394 | * admin/jqadm/product/decorators/excludes = array( 'decorator1' ) |
||
| 395 | * |
||
| 396 | * This would remove the decorator named "decorator1" from the list of |
||
| 397 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
| 398 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
| 399 | * |
||
| 400 | * @param array List of decorator names |
||
| 401 | * @since 2016.01 |
||
| 402 | * @see admin/jqadm/common/decorators/default |
||
| 403 | * @see admin/jqadm/product/decorators/global |
||
| 404 | * @see admin/jqadm/product/decorators/local |
||
| 405 | */ |
||
| 406 | |||
| 407 | /** admin/jqadm/product/decorators/global |
||
| 408 | * Adds a list of globally available decorators only to the product JQAdm client |
||
| 409 | * |
||
| 410 | * Decorators extend the functionality of a class by adding new aspects |
||
| 411 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 412 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 413 | * modify what is returned to the caller. |
||
| 414 | * |
||
| 415 | * This option allows you to wrap global decorators |
||
| 416 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
| 417 | * |
||
| 418 | * admin/jqadm/product/decorators/global = array( 'decorator1' ) |
||
| 419 | * |
||
| 420 | * This would add the decorator named "decorator1" defined by |
||
| 421 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
| 422 | * |
||
| 423 | * @param array List of decorator names |
||
| 424 | * @since 2016.01 |
||
| 425 | * @see admin/jqadm/common/decorators/default |
||
| 426 | * @see admin/jqadm/product/decorators/excludes |
||
| 427 | * @see admin/jqadm/product/decorators/local |
||
| 428 | */ |
||
| 429 | |||
| 430 | /** admin/jqadm/product/decorators/local |
||
| 431 | * Adds a list of local decorators only to the product JQAdm client |
||
| 432 | * |
||
| 433 | * Decorators extend the functionality of a class by adding new aspects |
||
| 434 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 435 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 436 | * modify what is returned to the caller. |
||
| 437 | * |
||
| 438 | * This option allows you to wrap local decorators |
||
| 439 | * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client. |
||
| 440 | * |
||
| 441 | * admin/jqadm/product/decorators/local = array( 'decorator2' ) |
||
| 442 | * |
||
| 443 | * This would add the decorator named "decorator2" defined by |
||
| 444 | * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client. |
||
| 445 | * |
||
| 446 | * @param array List of decorator names |
||
| 447 | * @since 2016.01 |
||
| 448 | * @see admin/jqadm/common/decorators/default |
||
| 449 | * @see admin/jqadm/product/decorators/excludes |
||
| 450 | * @see admin/jqadm/product/decorators/global |
||
| 451 | */ |
||
| 452 | return $this->createSubClient( 'product/' . $type, $name ); |
||
| 453 | } |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Returns the domain names whose items should be fetched too |
||
| 458 | * |
||
| 459 | * @return string[] List of domain names |
||
| 460 | */ |
||
| 461 | protected function getDomains() : array |
||
| 462 | { |
||
| 463 | /** admin/jqadm/product/domains |
||
| 464 | * List of domain items that should be fetched along with the product |
||
| 465 | * |
||
| 466 | * If you need to display additional content, you can configure your own |
||
| 467 | * list of domains (attribute, media, price, product, text, etc. are |
||
| 468 | * domains) whose items are fetched from the storage. |
||
| 469 | * |
||
| 470 | * @param array List of domain names |
||
| 471 | * @since 2016.01 |
||
| 472 | */ |
||
| 473 | return $this->context()->config()->get( 'admin/jqadm/product/domains', [] ); |
||
| 474 | } |
||
| 475 | |||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns the list of sub-client names configured for the client. |
||
| 479 | * |
||
| 480 | * @return array List of JQAdm client names |
||
| 481 | */ |
||
| 482 | protected function getSubClientNames() : array |
||
| 483 | { |
||
| 484 | /** admin/jqadm/product/subparts |
||
| 485 | * List of JQAdm sub-clients rendered within the product section |
||
| 486 | * |
||
| 487 | * The output of the frontend is composed of the code generated by the JQAdm |
||
| 488 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
| 489 | * that are responsible for rendering certain sub-parts of the output. The |
||
| 490 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
| 491 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
| 492 | * the output that is placed inside the container of its parent. |
||
| 493 | * |
||
| 494 | * At first, always the JQAdm code generated by the parent is printed, then |
||
| 495 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
| 496 | * determines the order of the output of these sub-clients inside the parent |
||
| 497 | * container. If the configured list of clients is |
||
| 498 | * |
||
| 499 | * array( "subclient1", "subclient2" ) |
||
| 500 | * |
||
| 501 | * you can easily change the order of the output by reordering the subparts: |
||
| 502 | * |
||
| 503 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
| 504 | * |
||
| 505 | * You can also remove one or more parts if they shouldn't be rendered: |
||
| 506 | * |
||
| 507 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
| 508 | * |
||
| 509 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
| 510 | * should support adding, removing or reordering content by a fluid like |
||
| 511 | * design. |
||
| 512 | * |
||
| 513 | * @param array List of sub-client names |
||
| 514 | * @since 2016.01 |
||
| 515 | */ |
||
| 516 | return $this->context()->config()->get( 'admin/jqadm/product/subparts', [] ); |
||
| 517 | } |
||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * Returns the available product type items |
||
| 522 | * |
||
| 523 | * @return \Aimeos\Map List of item implementing \Aimeos\MShop\Common\Type\Iface |
||
| 524 | */ |
||
| 525 | protected function getTypeItems() : \Aimeos\Map |
||
| 526 | { |
||
| 527 | $typeManager = \Aimeos\MShop::create( $this->context(), 'product/type' ); |
||
| 528 | $search = $typeManager->filter( true )->order( 'product.type.code' )->slice( 0, 10000 ); |
||
| 529 | |||
| 530 | return $typeManager->search( $search ); |
||
| 531 | } |
||
| 532 | |||
| 533 | |||
| 534 | /** |
||
| 535 | * Creates new and updates existing items using the data array |
||
| 536 | * |
||
| 537 | * @param array $data Data array |
||
| 538 | * @return \Aimeos\MShop\Product\Item\Iface New product item object |
||
| 539 | */ |
||
| 540 | protected function fromArray( array $data ) : \Aimeos\MShop\Product\Item\Iface |
||
| 541 | { |
||
| 542 | $manager = \Aimeos\MShop::create( $this->context(), 'product' ); |
||
| 543 | |||
| 544 | if( isset( $data['product.id'] ) && $data['product.id'] != '' ) { |
||
| 545 | $item = $manager->get( $data['product.id'], $this->getDomains() ); |
||
| 546 | } else { |
||
| 547 | $item = $manager->create(); |
||
| 548 | } |
||
| 549 | |||
| 550 | $item->fromArray( $data, true )->setConfig( [] ); |
||
| 551 | |||
| 552 | foreach( (array) $this->val( $data, 'config', [] ) as $cfg ) |
||
| 553 | { |
||
| 554 | if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) { |
||
| 555 | $item->setConfigValue( $key, json_decode( $val, true ) ?? $val ); |
||
| 556 | } |
||
| 557 | } |
||
| 558 | |||
| 559 | return $item; |
||
| 560 | } |
||
| 561 | |||
| 562 | |||
| 563 | /** |
||
| 564 | * Constructs the data array for the view from the given item |
||
| 565 | * |
||
| 566 | * @param \Aimeos\MShop\Product\Item\Iface $item Product item object |
||
| 567 | * @return string[] Multi-dimensional associative list of item data |
||
| 568 | */ |
||
| 569 | protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array |
||
| 586 | } |
||
| 587 | |||
| 588 | |||
| 589 | /** |
||
| 590 | * Returns the rendered template including the view data |
||
| 591 | * |
||
| 592 | * @param \Aimeos\Base\View\Iface $view View object with data assigned |
||
| 593 | * @return string HTML output |
||
| 594 | */ |
||
| 595 | protected function render( \Aimeos\Base\View\Iface $view ) : string |
||
| 619 | } |
||
| 620 | } |
||
| 621 |
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.