| Total Complexity | 48 |
| Total Lines | 592 |
| 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, 'coupon' ); |
||
| 43 | |||
| 44 | $view->item = $manager->getItem( $id ); |
||
| 45 | $view->itemData = $this->toArray( $view->item, true ); |
||
| 46 | $view->itemSubparts = $this->getSubClientNames(); |
||
| 47 | $view->itemProviders = $this->getProviderNames(); |
||
| 48 | $view->itemDecorators = $this->getDecoratorNames(); |
||
| 49 | $view->itemAttributes = $this->getConfigAttributes( $view->item ); |
||
| 50 | $view->itemBody = ''; |
||
| 51 | |||
| 52 | foreach( $this->getSubClients() as $idx => $client ) |
||
| 53 | { |
||
| 54 | $view->tabindex = ++$idx + 1; |
||
| 55 | $view->itemBody .= $client->copy(); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | catch( \Aimeos\MShop\Exception $e ) |
||
| 59 | { |
||
| 60 | $error = array( 'coupon-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 61 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 62 | $this->logException( $e ); |
||
| 63 | } |
||
| 64 | catch( \Exception $e ) |
||
| 65 | { |
||
| 66 | $error = array( 'coupon-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 67 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 68 | $this->logException( $e ); |
||
| 69 | } |
||
| 70 | |||
| 71 | return $this->render( $view ); |
||
| 72 | } |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * Creates a new resource |
||
| 77 | * |
||
| 78 | * @return string HTML output |
||
| 79 | */ |
||
| 80 | public function create() |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Deletes a resource |
||
| 126 | * |
||
| 127 | * @return string|null HTML output |
||
| 128 | */ |
||
| 129 | public function delete() |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns a single resource |
||
| 176 | * |
||
| 177 | * @return string HTML output |
||
| 178 | */ |
||
| 179 | public function get() |
||
| 180 | { |
||
| 181 | $view = $this->getView(); |
||
| 182 | $context = $this->getContext(); |
||
| 183 | |||
| 184 | try |
||
| 185 | { |
||
| 186 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
| 187 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
| 188 | } |
||
| 189 | |||
| 190 | $manager = \Aimeos\MShop::create( $context, 'coupon' ); |
||
| 191 | |||
| 192 | $view->item = $manager->getItem( $id ); |
||
| 193 | $view->itemData = $this->toArray( $view->item ); |
||
| 194 | $view->itemSubparts = $this->getSubClientNames(); |
||
| 195 | $view->itemDecorators = $this->getDecoratorNames(); |
||
| 196 | $view->itemProviders = $this->getProviderNames(); |
||
| 197 | $view->itemAttributes = $this->getConfigAttributes( $view->item ); |
||
| 198 | $view->itemBody = ''; |
||
| 199 | |||
| 200 | foreach( $this->getSubClients() as $idx => $client ) |
||
| 201 | { |
||
| 202 | $view->tabindex = ++$idx + 1; |
||
| 203 | $view->itemBody .= $client->get(); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | catch( \Aimeos\MShop\Exception $e ) |
||
| 207 | { |
||
| 208 | $error = array( 'coupon-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 209 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 210 | $this->logException( $e ); |
||
| 211 | } |
||
| 212 | catch( \Exception $e ) |
||
| 213 | { |
||
| 214 | $error = array( 'coupon-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 215 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 216 | $this->logException( $e ); |
||
| 217 | } |
||
| 218 | |||
| 219 | return $this->render( $view ); |
||
| 220 | } |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Saves the data |
||
| 225 | * |
||
| 226 | * @return string HTML output |
||
| 227 | */ |
||
| 228 | public function save() |
||
| 229 | { |
||
| 230 | $view = $this->getView(); |
||
| 231 | $context = $this->getContext(); |
||
| 232 | |||
| 233 | $manager = \Aimeos\MShop::create( $context, 'coupon' ); |
||
| 234 | $manager->begin(); |
||
| 235 | |||
| 236 | try |
||
| 237 | { |
||
| 238 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
| 239 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
| 240 | $view->itemBody = ''; |
||
| 241 | |||
| 242 | foreach( $this->getSubClients() as $client ) { |
||
| 243 | $view->itemBody .= $client->save(); |
||
| 244 | } |
||
| 245 | |||
| 246 | $manager->saveItem( clone $view->item ); |
||
| 247 | $manager->commit(); |
||
| 248 | |||
| 249 | $this->nextAction( $view, $view->param( 'next' ), 'coupon', $view->item->getId(), 'save' ); |
||
| 250 | return; |
||
| 251 | } |
||
| 252 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
| 253 | { |
||
| 254 | // fall through to create |
||
| 255 | } |
||
| 256 | catch( \Aimeos\MShop\Exception $e ) |
||
| 257 | { |
||
| 258 | $error = array( 'coupon-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 259 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 260 | $this->logException( $e ); |
||
| 261 | } |
||
| 262 | catch( \Exception $e ) |
||
| 263 | { |
||
| 264 | $error = array( 'coupon-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 265 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 266 | $this->logException( $e ); |
||
| 267 | } |
||
| 268 | |||
| 269 | $manager->rollback(); |
||
| 270 | |||
| 271 | return $this->create(); |
||
| 272 | } |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * Returns a list of resource according to the conditions |
||
| 277 | * |
||
| 278 | * @return string HTML output |
||
| 279 | */ |
||
| 280 | public function search() |
||
| 281 | { |
||
| 282 | $view = $this->getView(); |
||
| 283 | $context = $this->getContext(); |
||
| 284 | |||
| 285 | try |
||
| 286 | { |
||
| 287 | $total = 0; |
||
| 288 | $params = $this->storeSearchParams( $view->param(), 'coupon' ); |
||
| 289 | $manager = \Aimeos\MShop::create( $context, 'coupon' ); |
||
| 290 | $search = $this->initCriteria( $manager->createSearch(), $params ); |
||
| 291 | |||
| 292 | $view->items = $manager->searchItems( $search, [], $total ); |
||
| 293 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
| 294 | $view->filterOperators = $search->getOperators(); |
||
| 295 | $view->total = $total; |
||
| 296 | $view->itemBody = ''; |
||
| 297 | |||
| 298 | foreach( $this->getSubClients() as $client ) { |
||
| 299 | $view->itemBody .= $client->search(); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | catch( \Aimeos\MShop\Exception $e ) |
||
| 303 | { |
||
| 304 | $error = array( 'coupon-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 305 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 306 | $this->logException( $e ); |
||
| 307 | } |
||
| 308 | catch( \Exception $e ) |
||
| 309 | { |
||
| 310 | $error = array( 'coupon-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 311 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 312 | $this->logException( $e ); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** admin/jqadm/coupon/template-list |
||
| 316 | * Relative path to the HTML body template for the coupon list. |
||
| 317 | * |
||
| 318 | * The template file contains the HTML code and processing instructions |
||
| 319 | * to generate the result shown in the body of the frontend. The |
||
| 320 | * configuration string is the path to the template file relative |
||
| 321 | * to the templates directory (usually in admin/jqadm/templates). |
||
| 322 | * |
||
| 323 | * You can overwrite the template file configuration in extensions and |
||
| 324 | * provide alternative templates. These alternative templates should be |
||
| 325 | * named like the default one but with the string "default" replaced by |
||
| 326 | * an unique name. You may use the name of your project for this. If |
||
| 327 | * you've implemented an alternative client class as well, "default" |
||
| 328 | * should be replaced by the name of the new class. |
||
| 329 | * |
||
| 330 | * @param string Relative path to the template creating the HTML code |
||
| 331 | * @since 2016.04 |
||
| 332 | * @category Developer |
||
| 333 | */ |
||
| 334 | $tplconf = 'admin/jqadm/coupon/template-list'; |
||
| 335 | $default = 'coupon/list-standard'; |
||
| 336 | |||
| 337 | return $view->render( $view->config( $tplconf, $default ) ); |
||
| 338 | } |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns the sub-client given by its name. |
||
| 343 | * |
||
| 344 | * @param string $type Name of the client type |
||
| 345 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 346 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
| 347 | */ |
||
| 348 | public function getSubClient( $type, $name = null ) |
||
| 349 | { |
||
| 350 | /** admin/jqadm/coupon/decorators/excludes |
||
| 351 | * Excludes decorators added by the "common" option from the coupon JQAdm client |
||
| 352 | * |
||
| 353 | * Decorators extend the functionality of a class by adding new aspects |
||
| 354 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 355 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 356 | * modify what is returned to the caller. |
||
| 357 | * |
||
| 358 | * This option allows you to remove a decorator added via |
||
| 359 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
| 360 | * around the JQAdm client. |
||
| 361 | * |
||
| 362 | * admin/jqadm/coupon/decorators/excludes = array( 'decorator1' ) |
||
| 363 | * |
||
| 364 | * This would remove the decorator named "decorator1" from the list of |
||
| 365 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
| 366 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
| 367 | * |
||
| 368 | * @param array List of decorator names |
||
| 369 | * @since 2017.07 |
||
| 370 | * @category Developer |
||
| 371 | * @see admin/jqadm/common/decorators/default |
||
| 372 | * @see admin/jqadm/coupon/decorators/global |
||
| 373 | * @see admin/jqadm/coupon/decorators/local |
||
| 374 | */ |
||
| 375 | |||
| 376 | /** admin/jqadm/coupon/decorators/global |
||
| 377 | * Adds a list of globally available decorators only to the coupon JQAdm client |
||
| 378 | * |
||
| 379 | * Decorators extend the functionality of a class by adding new aspects |
||
| 380 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 381 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 382 | * modify what is returned to the caller. |
||
| 383 | * |
||
| 384 | * This option allows you to wrap global decorators |
||
| 385 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
| 386 | * |
||
| 387 | * admin/jqadm/coupon/decorators/global = array( 'decorator1' ) |
||
| 388 | * |
||
| 389 | * This would add the decorator named "decorator1" defined by |
||
| 390 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
| 391 | * |
||
| 392 | * @param array List of decorator names |
||
| 393 | * @since 2017.07 |
||
| 394 | * @category Developer |
||
| 395 | * @see admin/jqadm/common/decorators/default |
||
| 396 | * @see admin/jqadm/coupon/decorators/excludes |
||
| 397 | * @see admin/jqadm/coupon/decorators/local |
||
| 398 | */ |
||
| 399 | |||
| 400 | /** admin/jqadm/coupon/decorators/local |
||
| 401 | * Adds a list of local decorators only to the coupon JQAdm client |
||
| 402 | * |
||
| 403 | * Decorators extend the functionality of a class by adding new aspects |
||
| 404 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 405 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 406 | * modify what is returned to the caller. |
||
| 407 | * |
||
| 408 | * This option allows you to wrap local decorators |
||
| 409 | * ("\Aimeos\Admin\JQAdm\Coupon\Decorator\*") around the JQAdm client. |
||
| 410 | * |
||
| 411 | * admin/jqadm/coupon/decorators/local = array( 'decorator2' ) |
||
| 412 | * |
||
| 413 | * This would add the decorator named "decorator2" defined by |
||
| 414 | * "\Aimeos\Admin\JQAdm\Coupon\Decorator\Decorator2" only to the JQAdm client. |
||
| 415 | * |
||
| 416 | * @param array List of decorator names |
||
| 417 | * @since 2017.07 |
||
| 418 | * @category Developer |
||
| 419 | * @see admin/jqadm/common/decorators/default |
||
| 420 | * @see admin/jqadm/coupon/decorators/excludes |
||
| 421 | * @see admin/jqadm/coupon/decorators/global |
||
| 422 | */ |
||
| 423 | return $this->createSubClient( 'coupon/' . $type, $name ); |
||
| 424 | } |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * Returns the backend configuration attributes of the provider and decorators |
||
| 429 | * |
||
| 430 | * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item incl. provider/decorator property |
||
| 431 | * @return \Aimeos\MW\Common\Critera\Attribute\Iface[] List of configuration attributes |
||
| 432 | */ |
||
| 433 | public function getConfigAttributes( \Aimeos\MShop\Coupon\Item\Iface $item ) |
||
| 434 | { |
||
| 435 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' ); |
||
| 436 | |||
| 437 | try { |
||
| 438 | return $manager->getProvider( $item, '' )->getConfigBE(); |
||
|
1 ignored issue
–
show
|
|||
| 439 | } catch( \Aimeos\MShop\Exception $e ) { |
||
| 440 | return []; |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | |||
| 445 | /** |
||
| 446 | * Returns the list of sub-client names configured for the client. |
||
| 447 | * |
||
| 448 | * @return array List of JQAdm client names |
||
| 449 | */ |
||
| 450 | protected function getSubClientNames() |
||
| 486 | } |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * Returns the names of the available coupon decorators |
||
| 491 | * |
||
| 492 | * @return string[] List of decorator class names |
||
| 493 | */ |
||
| 494 | protected function getDecoratorNames() |
||
| 495 | { |
||
| 496 | $ds = DIRECTORY_SEPARATOR; |
||
| 497 | return $this->getClassNames( 'MShop' . $ds . 'Coupon' . $ds . 'Provider' . $ds . 'Decorator' ); |
||
| 498 | } |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns the names of the available coupon providers |
||
| 503 | * |
||
| 504 | * @return string[] List of provider class names |
||
| 505 | */ |
||
| 506 | protected function getProviderNames() |
||
| 507 | { |
||
| 508 | $ds = DIRECTORY_SEPARATOR; |
||
| 509 | return $this->getClassNames( 'MShop' . $ds . 'Coupon' . $ds . 'Provider' ); |
||
| 510 | } |
||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * Creates new and updates existing items using the data array |
||
| 515 | * |
||
| 516 | * @param string[] Data array |
||
| 517 | * @return \Aimeos\MShop\Coupon\Item\Iface New coupon item object |
||
| 518 | */ |
||
| 519 | protected function fromArray( array $data ) |
||
| 520 | { |
||
| 521 | $conf = []; |
||
| 522 | |||
| 523 | if( isset( $data['config']['key'] ) ) |
||
| 524 | { |
||
| 525 | foreach( (array) $data['config']['key'] as $idx => $key ) |
||
| 526 | { |
||
| 527 | if( trim( $key ) !== '' && isset( $data['config']['val'][$idx] ) ) |
||
| 528 | { |
||
| 529 | if( ( $val = json_decode( $data['config']['val'][$idx] ) ) === null ) { |
||
| 530 | $conf[$key] = $data['config']['val'][$idx]; |
||
| 531 | } else { |
||
| 532 | $conf[$key] = $val; |
||
| 533 | } |
||
| 534 | } |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' ); |
||
| 539 | |||
| 540 | if( isset( $data['coupon.id'] ) && $data['coupon.id'] != '' ) { |
||
| 541 | $item = $manager->getItem( $data['coupon.id'] ); |
||
| 542 | } else { |
||
| 543 | $item = $manager->createItem(); |
||
| 544 | } |
||
| 545 | |||
| 546 | $item->fromArray( $data ); |
||
| 547 | $item->setConfig( $conf ); |
||
| 548 | |||
| 549 | return $item; |
||
| 550 | } |
||
| 551 | |||
| 552 | |||
| 553 | /** |
||
| 554 | * Constructs the data array for the view from the given item |
||
| 555 | * |
||
| 556 | * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object |
||
| 557 | * @return string[] Multi-dimensional associative list of item data |
||
| 558 | */ |
||
| 559 | protected function toArray( \Aimeos\MShop\Coupon\Item\Iface $item, $copy = false ) |
||
| 560 | { |
||
| 561 | $config = $item->getConfig(); |
||
| 562 | $data = $item->toArray( true ); |
||
| 563 | $data['config'] = []; |
||
| 564 | |||
| 565 | if( $copy === true ) |
||
| 566 | { |
||
| 567 | $data['coupon.siteid'] = $this->getContext()->getLocale()->getSiteId(); |
||
| 568 | $data['coupon.id'] = ''; |
||
| 569 | } |
||
| 570 | |||
| 571 | ksort( $config ); |
||
| 572 | |||
| 573 | foreach( $config as $key => $value ) |
||
| 574 | { |
||
| 575 | $data['config']['key'][] = $key; |
||
| 576 | $data['config']['val'][] = $value; |
||
| 577 | } |
||
| 578 | |||
| 579 | return $data; |
||
| 580 | } |
||
| 581 | |||
| 582 | |||
| 583 | /** |
||
| 584 | * Returns the rendered template including the view data |
||
| 585 | * |
||
| 586 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
| 587 | * @return string HTML output |
||
| 588 | */ |
||
| 589 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
| 614 | } |
||
| 615 | } |
||
| 616 |