| Total Complexity | 42 |
| Total Lines | 504 |
| 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|null HTML output |
||
| 30 | */ |
||
| 31 | public function copy() : ?string |
||
| 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, 'catalog' ); |
||
| 43 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
| 44 | |||
| 45 | $view->itemData = $this->toArray( $view->item, true ); |
||
| 46 | $view->itemSubparts = $this->getSubClientNames(); |
||
| 47 | $view->itemRootId = $this->getRootId(); |
||
| 48 | $view->itemBody = ''; |
||
| 49 | |||
| 50 | foreach( $this->getSubClients() as $idx => $client ) |
||
| 51 | { |
||
| 52 | $view->tabindex = ++$idx + 1; |
||
| 53 | $view->itemBody .= $client->copy(); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | catch( \Exception $e ) |
||
| 57 | { |
||
| 58 | $this->report( $e, 'copy' ); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $this->render( $view ); |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Creates a new resource |
||
| 67 | * |
||
| 68 | * @return string|null HTML output |
||
| 69 | */ |
||
| 70 | public function create() : ?string |
||
| 71 | { |
||
| 72 | $view = $this->getView(); |
||
| 73 | $context = $this->getContext(); |
||
| 74 | |||
| 75 | try |
||
| 76 | { |
||
| 77 | $data = $view->param( 'item', [] ); |
||
| 78 | |||
| 79 | if( !isset( $view->item ) ) { |
||
| 80 | $view->item = \Aimeos\MShop::create( $context, 'catalog' )->createItem(); |
||
| 81 | } |
||
| 82 | |||
| 83 | $data['catalog.siteid'] = $view->item->getSiteId(); |
||
| 84 | $data['catalog.parentid'] = $view->item->getParentId() ?: $view->param( 'parentid', $view->param( 'item/catalog.parentid' ) ); |
||
| 85 | |||
| 86 | $view->itemSubparts = $this->getSubClientNames(); |
||
| 87 | $view->itemRootId = $this->getRootId(); |
||
| 88 | $view->itemData = $data; |
||
| 89 | $view->itemBody = ''; |
||
| 90 | |||
| 91 | foreach( $this->getSubClients() as $idx => $client ) |
||
| 92 | { |
||
| 93 | $view->tabindex = ++$idx + 1; |
||
| 94 | $view->itemBody .= $client->create(); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | catch( \Exception $e ) |
||
| 98 | { |
||
| 99 | $this->report( $e, 'create' ); |
||
| 100 | } |
||
| 101 | |||
| 102 | return $this->render( $view ); |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Deletes a resource |
||
| 108 | * |
||
| 109 | * @return string|null HTML output |
||
| 110 | */ |
||
| 111 | public function delete() : ?string |
||
| 112 | { |
||
| 113 | $view = $this->getView(); |
||
| 114 | $context = $this->getContext(); |
||
| 115 | |||
| 116 | $manager = \Aimeos\MShop::create( $context, 'catalog' ); |
||
| 117 | $manager->begin(); |
||
| 118 | |||
| 119 | try |
||
| 120 | { |
||
| 121 | if( ( $ids = $view->param( 'id' ) ) === null ) { |
||
| 122 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
| 123 | } |
||
| 124 | |||
| 125 | $search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) ); |
||
| 126 | $search->setConditions( $search->compare( '==', 'catalog.id', $ids ) ); |
||
| 127 | $items = $manager->searchItems( $search, $this->getDomains() ); |
||
| 128 | |||
| 129 | foreach( $items as $item ) |
||
| 130 | { |
||
| 131 | $view->item = $item; |
||
| 132 | |||
| 133 | foreach( $this->getSubClients() as $client ) { |
||
| 134 | $client->delete(); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | $manager->deleteItems( $items->toArray() ); |
||
| 139 | $manager->commit(); |
||
| 140 | |||
| 141 | $this->nextAction( $view, 'search', 'catalog', null, 'delete' ); |
||
| 142 | return null; |
||
| 143 | } |
||
| 144 | catch( \Exception $e ) |
||
| 145 | { |
||
| 146 | $manager->rollback(); |
||
| 147 | $this->report( $e, 'delete' ); |
||
| 148 | } |
||
| 149 | |||
| 150 | return $this->search(); |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns a single resource |
||
| 156 | * |
||
| 157 | * @return string|null HTML output |
||
| 158 | */ |
||
| 159 | public function get() : ?string |
||
| 160 | { |
||
| 161 | $view = $this->getView(); |
||
| 162 | $context = $this->getContext(); |
||
| 163 | |||
| 164 | try |
||
| 165 | { |
||
| 166 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
| 167 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
| 168 | } |
||
| 169 | |||
| 170 | $manager = \Aimeos\MShop::create( $context, 'catalog' ); |
||
| 171 | |||
| 172 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
| 173 | $view->itemSubparts = $this->getSubClientNames(); |
||
| 174 | $view->itemData = $this->toArray( $view->item ); |
||
| 175 | $view->itemRootId = $this->getRootId(); |
||
| 176 | $view->itemBody = ''; |
||
| 177 | |||
| 178 | foreach( $this->getSubClients() as $idx => $client ) |
||
| 179 | { |
||
| 180 | $view->tabindex = ++$idx + 1; |
||
| 181 | $view->itemBody .= $client->get(); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | catch( \Exception $e ) |
||
| 185 | { |
||
| 186 | $this->report( $e, 'get' ); |
||
| 187 | } |
||
| 188 | |||
| 189 | return $this->render( $view ); |
||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Saves the data |
||
| 195 | * |
||
| 196 | * @return string|null HTML output |
||
| 197 | */ |
||
| 198 | public function save() : ?string |
||
| 199 | { |
||
| 200 | $view = $this->getView(); |
||
| 201 | $context = $this->getContext(); |
||
| 202 | |||
| 203 | $manager = \Aimeos\MShop::create( $context, 'catalog' ); |
||
| 204 | $manager->begin(); |
||
| 205 | |||
| 206 | try |
||
| 207 | { |
||
| 208 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
| 209 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
|
|
|||
| 210 | $view->itemBody = ''; |
||
| 211 | |||
| 212 | foreach( $this->getSubClients() as $client ) { |
||
| 213 | $view->itemBody .= $client->save(); |
||
| 214 | } |
||
| 215 | |||
| 216 | $manager->saveItem( clone $view->item ); |
||
| 217 | $manager->commit(); |
||
| 218 | |||
| 219 | $action = $view->param( 'next' ); |
||
| 220 | $id = ( $action === 'create' ? $view->item->getParentId() : $view->item->getId() ); |
||
| 221 | |||
| 222 | $this->nextAction( $view, $action, 'catalog', $id, 'save' ); |
||
| 223 | return null; |
||
| 224 | } |
||
| 225 | catch( \Exception $e ) |
||
| 226 | { |
||
| 227 | $manager->rollback(); |
||
| 228 | $this->report( $e, 'save' ); |
||
| 229 | } |
||
| 230 | |||
| 231 | return $this->create(); |
||
| 232 | } |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns the catalog root node |
||
| 237 | * |
||
| 238 | * @return string|null HTML output |
||
| 239 | */ |
||
| 240 | public function search() : ?string |
||
| 241 | { |
||
| 242 | $view = $this->getView(); |
||
| 243 | $context = $this->getContext(); |
||
| 244 | |||
| 245 | try |
||
| 246 | { |
||
| 247 | $view->item = \Aimeos\MShop::create( $context, 'catalog' )->createItem(); |
||
| 248 | $view->itemRootId = $this->getRootId(); |
||
| 249 | $view->itemBody = ''; |
||
| 250 | |||
| 251 | foreach( $this->getSubClients() as $client ) { |
||
| 252 | $view->itemBody .= $client->search(); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | catch( \Exception $e ) |
||
| 256 | { |
||
| 257 | $this->report( $e, 'search' ); |
||
| 258 | } |
||
| 259 | |||
| 260 | return $this->render( $view ); |
||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns the sub-client given by its name. |
||
| 266 | * |
||
| 267 | * @param string $type Name of the client type |
||
| 268 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 269 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
| 270 | */ |
||
| 271 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
||
| 347 | } |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * Returns the domain names whose items should be fetched too |
||
| 352 | * |
||
| 353 | * @return string[] List of domain names |
||
| 354 | */ |
||
| 355 | protected function getDomains() : array |
||
| 356 | { |
||
| 357 | /** admin/jqadm/catalog/domains |
||
| 358 | * List of domain items that should be fetched along with the catalog |
||
| 359 | * |
||
| 360 | * If you need to display additional content, you can configure your own |
||
| 361 | * list of domains (attribute, media, price, catalog, text, etc. are |
||
| 362 | * domains) whose items are fetched from the storage. |
||
| 363 | * |
||
| 364 | * @param array List of domain names |
||
| 365 | * @since 2016.01 |
||
| 366 | * @category Developer |
||
| 367 | */ |
||
| 368 | return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/domains', [] ); |
||
| 369 | } |
||
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * Returns the IDs of the root category |
||
| 374 | * |
||
| 375 | * @return string|null ID of the root category |
||
| 376 | */ |
||
| 377 | protected function getRootId() : ?string |
||
| 378 | { |
||
| 379 | $manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' ); |
||
| 380 | |||
| 381 | try { |
||
| 382 | return $manager->getTree( null, [], \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE )->getId(); |
||
| 383 | } catch( \Exception $e ) { |
||
| 384 | return null; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * Returns the list of sub-client names configured for the client. |
||
| 391 | * |
||
| 392 | * @return array List of JQAdm client names |
||
| 393 | */ |
||
| 394 | protected function getSubClientNames() : array |
||
| 395 | { |
||
| 396 | /** admin/jqadm/catalog/standard/subparts |
||
| 397 | * List of JQAdm sub-clients rendered within the catalog section |
||
| 398 | * |
||
| 399 | * The output of the frontend is composed of the code generated by the JQAdm |
||
| 400 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
| 401 | * that are responsible for rendering certain sub-parts of the output. The |
||
| 402 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
| 403 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
| 404 | * the output that is placed inside the container of its parent. |
||
| 405 | * |
||
| 406 | * At first, always the JQAdm code generated by the parent is printed, then |
||
| 407 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
| 408 | * determines the order of the output of these sub-clients inside the parent |
||
| 409 | * container. If the configured list of clients is |
||
| 410 | * |
||
| 411 | * array( "subclient1", "subclient2" ) |
||
| 412 | * |
||
| 413 | * you can easily change the order of the output by reordering the subparts: |
||
| 414 | * |
||
| 415 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
| 416 | * |
||
| 417 | * You can also remove one or more parts if they shouldn't be rendered: |
||
| 418 | * |
||
| 419 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
| 420 | * |
||
| 421 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
| 422 | * should support adding, removing or reordering content by a fluid like |
||
| 423 | * design. |
||
| 424 | * |
||
| 425 | * @param array List of sub-client names |
||
| 426 | * @since 2016.01 |
||
| 427 | * @category Developer |
||
| 428 | */ |
||
| 429 | return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/standard/subparts', [] ); |
||
| 430 | } |
||
| 431 | |||
| 432 | |||
| 433 | /** |
||
| 434 | * Creates new and updates existing items using the data array |
||
| 435 | * |
||
| 436 | * @param array $data Data array |
||
| 437 | * @return \Aimeos\MShop\Catalog\Item\Iface New catalog item object |
||
| 438 | */ |
||
| 439 | protected function fromArray( array $data ) : \Aimeos\MShop\Catalog\Item\Iface |
||
| 440 | { |
||
| 441 | $conf = []; |
||
| 442 | |||
| 443 | foreach( (array) $this->getValue( $data, 'config', [] ) as $idx => $entry ) |
||
| 444 | { |
||
| 445 | if( ( $key = trim( $entry['key'] ?? '' ) ) !== '' ) { |
||
| 446 | $conf[$key] = trim( $entry['val'] ?? '' ); |
||
| 447 | } |
||
| 448 | } |
||
| 449 | |||
| 450 | $manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' ); |
||
| 451 | |||
| 452 | if( isset( $data['catalog.id'] ) && $data['catalog.id'] != '' ) { |
||
| 453 | $item = $manager->getItem( $data['catalog.id'], $this->getDomains() ); |
||
| 454 | } else { |
||
| 455 | $item = $manager->createItem(); |
||
| 456 | } |
||
| 457 | |||
| 458 | $item->fromArray( $data, true ); |
||
| 459 | $item->setConfig( $conf ); |
||
| 460 | |||
| 461 | if( $item->getId() == null ) { |
||
| 462 | return $manager->insertItem( $item, $data['catalog.parentid'] ?: null ); |
||
| 463 | } |
||
| 464 | |||
| 465 | return $item; |
||
| 466 | } |
||
| 467 | |||
| 468 | |||
| 469 | /** |
||
| 470 | * Constructs the data array for the view from the given item |
||
| 471 | * |
||
| 472 | * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object |
||
| 473 | * @return string[] Multi-dimensional associative list of item data |
||
| 474 | */ |
||
| 475 | protected function toArray( \Aimeos\MShop\Catalog\Item\Iface $item, bool $copy = false ) : array |
||
| 492 | } |
||
| 493 | |||
| 494 | |||
| 495 | /** |
||
| 496 | * Returns the rendered template including the view data |
||
| 497 | * |
||
| 498 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
| 499 | * @return string|null HTML output |
||
| 500 | */ |
||
| 501 | protected function render( \Aimeos\MW\View\Iface $view ) : string |
||
| 526 | } |
||
| 527 | } |
||
| 528 |
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.