| Total Complexity | 48 |
| Total Lines | 552 |
| 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 HTML output |
||
| 30 | */ |
||
| 31 | public function copy() |
||
| 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( \Aimeos\MShop\Exception $e ) |
||
| 57 | { |
||
| 58 | $error = array( 'catalog-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 59 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 60 | $this->logException( $e ); |
||
| 61 | } |
||
| 62 | catch( \Exception $e ) |
||
| 63 | { |
||
| 64 | $error = array( 'catalog-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 65 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 66 | $this->logException( $e ); |
||
| 67 | } |
||
| 68 | |||
| 69 | return $this->render( $view ); |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Creates a new resource |
||
| 75 | * |
||
| 76 | * @return string HTML output |
||
| 77 | */ |
||
| 78 | public function create() |
||
| 79 | { |
||
| 80 | $view = $this->getView(); |
||
| 81 | $context = $this->getContext(); |
||
| 82 | |||
| 83 | try |
||
| 84 | { |
||
| 85 | $data = $view->param( 'item', [] ); |
||
| 86 | |||
| 87 | if( !isset( $view->item ) ) { |
||
| 88 | $view->item = \Aimeos\MShop::create( $context, 'catalog' )->createItem(); |
||
| 89 | } |
||
| 90 | |||
| 91 | $data['catalog.siteid'] = $view->item->getSiteId(); |
||
| 92 | $data['catalog.parentid'] = $view->item->getParentId() ?: $view->param( 'item/catalog.parentid' ); |
||
| 93 | |||
| 94 | $view->itemSubparts = $this->getSubClientNames(); |
||
| 95 | $view->itemRootId = $this->getRootId(); |
||
| 96 | $view->itemData = $data; |
||
| 97 | $view->itemBody = ''; |
||
| 98 | |||
| 99 | foreach( $this->getSubClients() as $idx => $client ) |
||
| 100 | { |
||
| 101 | $view->tabindex = ++$idx + 1; |
||
| 102 | $view->itemBody .= $client->create(); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | catch( \Aimeos\MShop\Exception $e ) |
||
| 106 | { |
||
| 107 | $error = array( 'catalog-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 108 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 109 | $this->logException( $e ); |
||
| 110 | } |
||
| 111 | catch( \Exception $e ) |
||
| 112 | { |
||
| 113 | $error = array( 'catalog-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 114 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 115 | $this->logException( $e ); |
||
| 116 | } |
||
| 117 | |||
| 118 | return $this->render( $view ); |
||
| 119 | } |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Deletes a resource |
||
| 124 | * |
||
| 125 | * @return string|null HTML output |
||
| 126 | */ |
||
| 127 | public function delete() |
||
| 128 | { |
||
| 129 | $view = $this->getView(); |
||
| 130 | $context = $this->getContext(); |
||
| 131 | |||
| 132 | $manager = \Aimeos\MShop::create( $context, 'catalog' ); |
||
| 133 | $manager->begin(); |
||
| 134 | |||
| 135 | try |
||
| 136 | { |
||
| 137 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
| 138 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
| 139 | } |
||
| 140 | |||
| 141 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
| 142 | |||
| 143 | foreach( $this->getSubClients() as $client ) { |
||
| 144 | $client->delete(); |
||
| 145 | } |
||
| 146 | |||
| 147 | $manager->saveItem( $view->item ); |
||
| 148 | $manager->deleteItem( $id ); |
||
| 149 | $manager->commit(); |
||
| 150 | |||
| 151 | $this->nextAction( $view, 'search', 'catalog', null, 'delete' ); |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | catch( \Aimeos\MShop\Exception $e ) |
||
| 155 | { |
||
| 156 | $error = array( 'catalog-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 157 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 158 | $this->logException( $e ); |
||
| 159 | } |
||
| 160 | catch( \Exception $e ) |
||
| 161 | { |
||
| 162 | $error = array( 'catalog-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 163 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 164 | $this->logException( $e ); |
||
| 165 | } |
||
| 166 | |||
| 167 | $manager->rollback(); |
||
| 168 | |||
| 169 | return $this->search(); |
||
| 170 | } |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns a single resource |
||
| 175 | * |
||
| 176 | * @return string HTML output |
||
| 177 | */ |
||
| 178 | public function get() |
||
| 179 | { |
||
| 180 | $view = $this->getView(); |
||
| 181 | $context = $this->getContext(); |
||
| 182 | |||
| 183 | try |
||
| 184 | { |
||
| 185 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
| 186 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
| 187 | } |
||
| 188 | |||
| 189 | $manager = \Aimeos\MShop::create( $context, 'catalog' ); |
||
| 190 | |||
| 191 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
| 192 | $view->itemSubparts = $this->getSubClientNames(); |
||
| 193 | $view->itemData = $this->toArray( $view->item ); |
||
| 194 | $view->itemRootId = $this->getRootId(); |
||
| 195 | $view->itemBody = ''; |
||
| 196 | |||
| 197 | foreach( $this->getSubClients() as $idx => $client ) |
||
| 198 | { |
||
| 199 | $view->tabindex = ++$idx + 1; |
||
| 200 | $view->itemBody .= $client->get(); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | catch( \Aimeos\MShop\Exception $e ) |
||
| 204 | { |
||
| 205 | $error = array( 'catalog-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 206 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 207 | $this->logException( $e ); |
||
| 208 | } |
||
| 209 | catch( \Exception $e ) |
||
| 210 | { |
||
| 211 | $error = array( 'catalog-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 212 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 213 | $this->logException( $e ); |
||
| 214 | } |
||
| 215 | |||
| 216 | return $this->render( $view ); |
||
| 217 | } |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Saves the data |
||
| 222 | * |
||
| 223 | * @return string HTML output |
||
| 224 | */ |
||
| 225 | public function save() |
||
| 226 | { |
||
| 227 | $view = $this->getView(); |
||
| 228 | $context = $this->getContext(); |
||
| 229 | |||
| 230 | $manager = \Aimeos\MShop::create( $context, 'catalog' ); |
||
| 231 | $manager->begin(); |
||
| 232 | |||
| 233 | try |
||
| 234 | { |
||
| 235 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
| 236 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
| 237 | $view->itemBody = ''; |
||
| 238 | |||
| 239 | foreach( $this->getSubClients() as $client ) { |
||
| 240 | $view->itemBody .= $client->save(); |
||
| 241 | } |
||
| 242 | |||
| 243 | $manager->saveItem( clone $view->item ); |
||
| 244 | $manager->commit(); |
||
| 245 | |||
| 246 | $this->nextAction( $view, $view->param( 'next' ), 'catalog', $view->item->getId(), 'save' ); |
||
| 247 | return; |
||
| 248 | } |
||
| 249 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
| 250 | { |
||
| 251 | // fall through to create |
||
| 252 | } |
||
| 253 | catch( \Aimeos\MShop\Exception $e ) |
||
| 254 | { |
||
| 255 | $error = array( 'catalog-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 256 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 257 | $this->logException( $e ); |
||
| 258 | } |
||
| 259 | catch( \Exception $e ) |
||
| 260 | { |
||
| 261 | $error = array( 'catalog-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 262 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 263 | $this->logException( $e ); |
||
| 264 | } |
||
| 265 | |||
| 266 | $manager->rollback(); |
||
| 267 | |||
| 268 | return $this->create(); |
||
| 269 | } |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns the catalog root node |
||
| 274 | * |
||
| 275 | * @return string HTML output |
||
| 276 | */ |
||
| 277 | public function search() |
||
| 278 | { |
||
| 279 | $view = $this->getView(); |
||
| 280 | $context = $this->getContext(); |
||
| 281 | |||
| 282 | try |
||
| 283 | { |
||
| 284 | $view->item = \Aimeos\MShop::create( $context, 'catalog' )->createItem(); |
||
| 285 | $view->itemRootId = $this->getRootId(); |
||
| 286 | $view->itemBody = ''; |
||
| 287 | } |
||
| 288 | catch( \Aimeos\MShop\Exception $e ) |
||
| 289 | { |
||
| 290 | $error = array( 'catalog-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 291 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 292 | $this->logException( $e ); |
||
| 293 | } |
||
| 294 | catch( \Exception $e ) |
||
| 295 | { |
||
| 296 | $error = array( 'catalog-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 297 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 298 | $this->logException( $e ); |
||
| 299 | } |
||
| 300 | |||
| 301 | return $this->render( $view ); |
||
| 302 | } |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns the sub-client given by its name. |
||
| 307 | * |
||
| 308 | * @param string $type Name of the client type |
||
| 309 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 310 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
| 311 | */ |
||
| 312 | public function getSubClient( $type, $name = null ) |
||
| 388 | } |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * Returns the domain names whose items should be fetched too |
||
| 393 | * |
||
| 394 | * @return string[] List of domain names |
||
| 395 | */ |
||
| 396 | protected function getDomains() |
||
| 397 | { |
||
| 398 | /** admin/jqadm/catalog/domains |
||
| 399 | * List of domain items that should be fetched along with the catalog |
||
| 400 | * |
||
| 401 | * If you need to display additional content, you can configure your own |
||
| 402 | * list of domains (attribute, media, price, catalog, text, etc. are |
||
| 403 | * domains) whose items are fetched from the storage. |
||
| 404 | * |
||
| 405 | * @param array List of domain names |
||
| 406 | * @since 2016.01 |
||
| 407 | * @category Developer |
||
| 408 | */ |
||
| 409 | $domains = array( 'media', 'text' ); |
||
| 410 | |||
| 411 | return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/domains', $domains ); |
||
| 412 | } |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns the IDs of the root category |
||
| 417 | * |
||
| 418 | * @return string|null ID of the root category |
||
| 419 | */ |
||
| 420 | protected function getRootId() |
||
| 421 | { |
||
| 422 | $manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' ); |
||
| 423 | |||
| 424 | try { |
||
| 425 | return $manager->getTree( null, [], \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE )->getId(); |
||
|
1 ignored issue
–
show
|
|||
| 426 | } catch( \Exception $e ) { |
||
| 427 | return null; |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Returns the list of sub-client names configured for the client. |
||
| 434 | * |
||
| 435 | * @return array List of JQAdm client names |
||
| 436 | */ |
||
| 437 | protected function getSubClientNames() |
||
| 438 | { |
||
| 439 | /** admin/jqadm/catalog/standard/subparts |
||
| 440 | * List of JQAdm sub-clients rendered within the catalog section |
||
| 441 | * |
||
| 442 | * The output of the frontend is composed of the code generated by the JQAdm |
||
| 443 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
| 444 | * that are responsible for rendering certain sub-parts of the output. The |
||
| 445 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
| 446 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
| 447 | * the output that is placed inside the container of its parent. |
||
| 448 | * |
||
| 449 | * At first, always the JQAdm code generated by the parent is printed, then |
||
| 450 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
| 451 | * determines the order of the output of these sub-clients inside the parent |
||
| 452 | * container. If the configured list of clients is |
||
| 453 | * |
||
| 454 | * array( "subclient1", "subclient2" ) |
||
| 455 | * |
||
| 456 | * you can easily change the order of the output by reordering the subparts: |
||
| 457 | * |
||
| 458 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
| 459 | * |
||
| 460 | * You can also remove one or more parts if they shouldn't be rendered: |
||
| 461 | * |
||
| 462 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
| 463 | * |
||
| 464 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
| 465 | * should support adding, removing or reordering content by a fluid like |
||
| 466 | * design. |
||
| 467 | * |
||
| 468 | * @param array List of sub-client names |
||
| 469 | * @since 2016.01 |
||
| 470 | * @category Developer |
||
| 471 | */ |
||
| 472 | return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/standard/subparts', [] ); |
||
| 473 | } |
||
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * Creates new and updates existing items using the data array |
||
| 478 | * |
||
| 479 | * @param string[] Data array |
||
| 480 | * @return \Aimeos\MShop\Catalog\Item\Iface New catalog item object |
||
| 481 | */ |
||
| 482 | protected function fromArray( array $data ) |
||
| 483 | { |
||
| 484 | $conf = []; |
||
| 485 | |||
| 486 | if( isset( $data['config']['key'] ) ) |
||
| 487 | { |
||
| 488 | foreach( (array) $data['config']['key'] as $idx => $key ) |
||
| 489 | { |
||
| 490 | if( trim( $key ) !== '' && isset( $data['config']['val'][$idx] ) ) { |
||
| 491 | $conf[$key] = $data['config']['val'][$idx]; |
||
| 492 | } |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | $manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' ); |
||
| 497 | |||
| 498 | if( isset( $data['catalog.id'] ) && $data['catalog.id'] != '' ) { |
||
| 499 | $item = $manager->getItem( $data['catalog.id'], $this->getDomains() ); |
||
| 500 | } else { |
||
| 501 | $item = $manager->createItem(); |
||
| 502 | } |
||
| 503 | |||
| 504 | $item->fromArray( $data ); |
||
| 505 | $item->setConfig( $conf ); |
||
| 506 | |||
| 507 | if( $item->getId() == null ) { |
||
| 508 | return $manager->insertItem( $item, $data['catalog.parentid'] ?: null ); |
||
|
1 ignored issue
–
show
|
|||
| 509 | } |
||
| 510 | |||
| 511 | return $item; |
||
| 512 | } |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * Constructs the data array for the view from the given item |
||
| 517 | * |
||
| 518 | * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object |
||
| 519 | * @return string[] Multi-dimensional associative list of item data |
||
| 520 | */ |
||
| 521 | protected function toArray( \Aimeos\MShop\Catalog\Item\Iface $item, $copy = false ) |
||
| 540 | } |
||
| 541 | |||
| 542 | |||
| 543 | /** |
||
| 544 | * Returns the rendered template including the view data |
||
| 545 | * |
||
| 546 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
| 547 | * @return string HTML output |
||
| 548 | */ |
||
| 549 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
| 574 | } |
||
| 575 | } |
||
| 576 |