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