| Total Complexity | 54 |
| Total Lines | 599 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| 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/customer/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\Customer\Standard |
||
| 38 | * |
||
| 39 | * and you want to replace it with your own version named |
||
| 40 | * |
||
| 41 | * \Aimeos\Admin\JQAdm\Customer\Myfavorite |
||
| 42 | * |
||
| 43 | * then you have to set the this configuration option: |
||
| 44 | * |
||
| 45 | * admin/jqadm/customer/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 |
||
| 68 | { |
||
| 69 | $codes = []; |
||
| 70 | |||
| 71 | foreach( $this->context()->config()->get( 'common/countries', [] ) as $code ) { |
||
| 72 | $codes[$code] = $view->translate( 'country', $code ); |
||
| 73 | } |
||
| 74 | |||
| 75 | asort( $codes ); |
||
| 76 | |||
| 77 | $manager = \Aimeos\MShop::create( $this->context(), 'group' ); |
||
| 78 | $filter = $manager->filter()->slice( 0, 1000 ); |
||
| 79 | |||
| 80 | $view->itemSubparts = $this->getSubClientNames(); |
||
| 81 | $view->itemGroups = $manager->search( $filter ); |
||
| 82 | $view->countries = $codes; |
||
| 83 | |||
| 84 | return $view; |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Batch update of a resource |
||
| 90 | * |
||
| 91 | * @return string|null Output to display |
||
| 92 | */ |
||
| 93 | public function batch() : ?string |
||
| 94 | { |
||
| 95 | $view = $this->view(); |
||
| 96 | |||
| 97 | if( !empty( $ids = $view->param( 'id' ) ) ) |
||
| 98 | { |
||
| 99 | $context = $this->context(); |
||
| 100 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
| 101 | $filter = $manager->filter()->add( ['customer.id' => $ids] )->slice( 0, count( $ids ) ); |
||
| 102 | $items = $manager->search( $filter ); |
||
| 103 | |||
| 104 | $data = $view->param( 'item', [] ); |
||
| 105 | |||
| 106 | foreach( $items as $item ) |
||
| 107 | { |
||
| 108 | if( $view->access( ['super', 'admin'] ) || $item->getId() === $context->user() ) |
||
| 109 | { |
||
| 110 | !isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] ); |
||
| 111 | !isset( $data['groups'] ) ?: $item->setGroups( array_filter( (array) $data['groups'] ) ); |
||
| 112 | } |
||
| 113 | |||
| 114 | !isset( $data['customer.dateverified'] ) ?: $item->setDateVerified( $data['customer.dateverified'] ); |
||
| 115 | !isset( $data['customer.status'] ) ?: $item->setStatus( $data['customer.status'] ); |
||
| 116 | |||
| 117 | $temp = $data; $item->fromArray( $temp ); |
||
| 118 | } |
||
| 119 | |||
| 120 | $manager->save( $items ); |
||
| 121 | } |
||
| 122 | |||
| 123 | return $this->redirect( 'customer', 'search', null, 'save' ); |
||
| 124 | } |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Copies a resource |
||
| 129 | * |
||
| 130 | * @return string|null HTML output |
||
| 131 | */ |
||
| 132 | public function copy() : ?string |
||
| 133 | { |
||
| 134 | $view = $this->object()->data( $this->view() ); |
||
| 135 | |||
| 136 | try |
||
| 137 | { |
||
| 138 | if( ( $id = $view->param( 'id' ) ) === null ) |
||
| 139 | { |
||
| 140 | $msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' ); |
||
| 141 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) ); |
||
| 142 | } |
||
| 143 | |||
| 144 | $manager = \Aimeos\MShop::create( $this->context(), 'customer' ); |
||
| 145 | $view->item = $manager->get( $id, $this->getDomains() ); |
||
| 146 | |||
| 147 | $view->itemData = $this->toArray( $view->item, true ); |
||
| 148 | $view->itemBody = parent::copy(); |
||
| 149 | } |
||
| 150 | catch( \Exception $e ) |
||
| 151 | { |
||
| 152 | $this->report( $e, 'copy' ); |
||
| 153 | } |
||
| 154 | |||
| 155 | return $this->render( $view ); |
||
| 156 | } |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * Creates a new resource |
||
| 161 | * |
||
| 162 | * @return string|null HTML output |
||
| 163 | */ |
||
| 164 | public function create() : ?string |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Deletes a resource |
||
| 192 | * |
||
| 193 | * @return string|null HTML output |
||
| 194 | */ |
||
| 195 | public function delete() : ?string |
||
| 196 | { |
||
| 197 | $view = $this->view(); |
||
| 198 | $context = $this->context(); |
||
| 199 | |||
| 200 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
| 201 | $manager->begin(); |
||
| 202 | |||
| 203 | try |
||
| 204 | { |
||
| 205 | if( ( $ids = $view->param( 'id' ) ) === null ) |
||
| 206 | { |
||
| 207 | $msg = $context->translate( 'admin', 'Required parameter "%1$s" is missing' ); |
||
| 208 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) ); |
||
| 209 | } |
||
| 210 | |||
| 211 | if( !$view->access( ['super', 'admin'] ) ) |
||
| 212 | { |
||
| 213 | $msg = $context->translate( 'admin', 'Only super users and administrators can delete entries' ); |
||
| 214 | throw new \Aimeos\Admin\JQAdm\Exception( $msg ); |
||
| 215 | } |
||
| 216 | |||
| 217 | $search = $manager->filter()->slice( 0, count( (array) $ids ) ); |
||
| 218 | $search->add( $search->and( [ |
||
| 219 | $search->compare( '==', 'customer.id', $ids ), |
||
| 220 | $search->compare( '!=', 'customer.siteid', '' ) |
||
| 221 | ] ) ); |
||
| 222 | |||
| 223 | $items = $manager->search( $search, $this->getDomains() ); |
||
| 224 | |||
| 225 | foreach( $items as $item ) |
||
| 226 | { |
||
| 227 | $view->item = $item; |
||
| 228 | parent::delete(); |
||
| 229 | } |
||
| 230 | |||
| 231 | $manager->delete( $items->toArray() ); |
||
| 232 | $manager->commit(); |
||
| 233 | |||
| 234 | if( $items->count() !== count( (array) $ids ) ) |
||
| 235 | { |
||
| 236 | $msg = $context->translate( 'admin', 'Not all entries could be deleted' ); |
||
| 237 | throw new \Aimeos\Admin\JQAdm\Exception( $msg ); |
||
| 238 | } |
||
| 239 | |||
| 240 | return $this->redirect( 'customer', 'search', null, 'delete' ); |
||
| 241 | } |
||
| 242 | catch( \Exception $e ) |
||
| 243 | { |
||
| 244 | $manager->rollback(); |
||
| 245 | $this->report( $e, 'delete' ); |
||
| 246 | } |
||
| 247 | |||
| 248 | return $this->search(); |
||
| 249 | } |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Returns a single resource |
||
| 254 | * |
||
| 255 | * @return string|null HTML output |
||
| 256 | */ |
||
| 257 | public function get() : ?string |
||
| 258 | { |
||
| 259 | $view = $this->object()->data( $this->view() ); |
||
| 260 | |||
| 261 | try |
||
| 262 | { |
||
| 263 | if( ( $id = $view->param( 'id' ) ) === null ) |
||
| 264 | { |
||
| 265 | $msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' ); |
||
| 266 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) ); |
||
| 267 | } |
||
| 268 | |||
| 269 | $manager = \Aimeos\MShop::create( $this->context(), 'customer' ); |
||
| 270 | |||
| 271 | $view->item = $manager->get( $id, $this->getDomains() ); |
||
| 272 | $view->itemData = $this->toArray( $view->item ); |
||
| 273 | $view->itemBody = parent::get(); |
||
| 274 | } |
||
| 275 | catch( \Exception $e ) |
||
| 276 | { |
||
| 277 | $this->report( $e, 'get' ); |
||
| 278 | } |
||
| 279 | |||
| 280 | return $this->render( $view ); |
||
| 281 | } |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Saves the data |
||
| 286 | * |
||
| 287 | * @return string|null HTML output |
||
| 288 | */ |
||
| 289 | public function save() : ?string |
||
| 290 | { |
||
| 291 | $view = $this->view(); |
||
| 292 | |||
| 293 | $manager = \Aimeos\MShop::create( $this->context(), 'customer' ); |
||
| 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 | $manager->save( clone $view->item ); |
||
| 303 | $manager->commit(); |
||
| 304 | |||
| 305 | return $this->redirect( 'customer', $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->object()->data( $this->view() ); |
||
| 325 | |||
| 326 | try |
||
| 327 | { |
||
| 328 | $total = 0; |
||
| 329 | $params = $this->storeFilter( $view->param(), 'customer' ); |
||
| 330 | $manager = \Aimeos\MShop::create( $this->context(), 'customer' ); |
||
| 331 | $search = $this->initCriteria( $manager->filter(), $params ); |
||
| 332 | |||
| 333 | $view->items = $manager->search( $search, $this->getDomains(), $total ); |
||
| 334 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
| 335 | $view->filterOperators = $search->getOperators(); |
||
| 336 | $view->itemBody = parent::search(); |
||
| 337 | $view->total = $total; |
||
| 338 | } |
||
| 339 | catch( \Exception $e ) |
||
| 340 | { |
||
| 341 | $this->report( $e, 'search' ); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** admin/jqadm/customer/template-list |
||
| 345 | * Relative path to the HTML body template for the customer list. |
||
| 346 | * |
||
| 347 | * The template file contains the HTML code and processing instructions |
||
| 348 | * to generate the result shown in the body of the frontend. The |
||
| 349 | * configuration string is the path to the template file relative |
||
| 350 | * to the templates directory (usually in templates/admin/jqadm). |
||
| 351 | * |
||
| 352 | * You can overwrite the template file configuration in extensions and |
||
| 353 | * provide alternative templates. These alternative templates should be |
||
| 354 | * named like the default one but with the string "default" replaced by |
||
| 355 | * an unique name. You may use the name of your project for this. If |
||
| 356 | * you've implemented an alternative client class as well, "default" |
||
| 357 | * should be replaced by the name of the new class. |
||
| 358 | * |
||
| 359 | * @param string Relative path to the template creating the HTML code |
||
| 360 | * @since 2016.04 |
||
| 361 | */ |
||
| 362 | $tplconf = 'admin/jqadm/customer/template-list'; |
||
| 363 | $default = 'customer/list'; |
||
| 364 | |||
| 365 | return $view->render( $view->config( $tplconf, $default ) ); |
||
| 366 | } |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns the sub-client given by its name. |
||
| 371 | * |
||
| 372 | * @param string $type Name of the client type |
||
| 373 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 374 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
| 375 | */ |
||
| 376 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
||
| 377 | { |
||
| 378 | /** admin/jqadm/customer/decorators/excludes |
||
| 379 | * Excludes decorators added by the "common" option from the customer JQAdm client |
||
| 380 | * |
||
| 381 | * Decorators extend the functionality of a class by adding new aspects |
||
| 382 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 383 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 384 | * modify what is returned to the caller. |
||
| 385 | * |
||
| 386 | * This option allows you to remove a decorator added via |
||
| 387 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
| 388 | * around the JQAdm client. |
||
| 389 | * |
||
| 390 | * admin/jqadm/customer/decorators/excludes = array( 'decorator1' ) |
||
| 391 | * |
||
| 392 | * This would remove the decorator named "decorator1" from the list of |
||
| 393 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
| 394 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
| 395 | * |
||
| 396 | * @param array List of decorator names |
||
| 397 | * @since 2017.07 |
||
| 398 | * @see admin/jqadm/common/decorators/default |
||
| 399 | * @see admin/jqadm/customer/decorators/global |
||
| 400 | * @see admin/jqadm/customer/decorators/local |
||
| 401 | */ |
||
| 402 | |||
| 403 | /** admin/jqadm/customer/decorators/global |
||
| 404 | * Adds a list of globally available decorators only to the customer JQAdm client |
||
| 405 | * |
||
| 406 | * Decorators extend the functionality of a class by adding new aspects |
||
| 407 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 408 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 409 | * modify what is returned to the caller. |
||
| 410 | * |
||
| 411 | * This option allows you to wrap global decorators |
||
| 412 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
| 413 | * |
||
| 414 | * admin/jqadm/customer/decorators/global = array( 'decorator1' ) |
||
| 415 | * |
||
| 416 | * This would add the decorator named "decorator1" defined by |
||
| 417 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
| 418 | * |
||
| 419 | * @param array List of decorator names |
||
| 420 | * @since 2017.07 |
||
| 421 | * @see admin/jqadm/common/decorators/default |
||
| 422 | * @see admin/jqadm/customer/decorators/excludes |
||
| 423 | * @see admin/jqadm/customer/decorators/local |
||
| 424 | */ |
||
| 425 | |||
| 426 | /** admin/jqadm/customer/decorators/local |
||
| 427 | * Adds a list of local decorators only to the customer JQAdm client |
||
| 428 | * |
||
| 429 | * Decorators extend the functionality of a class by adding new aspects |
||
| 430 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 431 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 432 | * modify what is returned to the caller. |
||
| 433 | * |
||
| 434 | * This option allows you to wrap local decorators |
||
| 435 | * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client. |
||
| 436 | * |
||
| 437 | * admin/jqadm/customer/decorators/local = array( 'decorator2' ) |
||
| 438 | * |
||
| 439 | * This would add the decorator named "decorator2" defined by |
||
| 440 | * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client. |
||
| 441 | * |
||
| 442 | * @param array List of decorator names |
||
| 443 | * @since 2017.07 |
||
| 444 | * @see admin/jqadm/common/decorators/default |
||
| 445 | * @see admin/jqadm/customer/decorators/excludes |
||
| 446 | * @see admin/jqadm/customer/decorators/global |
||
| 447 | */ |
||
| 448 | return $this->createSubClient( 'customer/' . $type, $name ); |
||
| 449 | } |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * Returns the domain names whose items should be fetched too |
||
| 454 | * |
||
| 455 | * @return string[] List of domain names |
||
| 456 | */ |
||
| 457 | protected function getDomains() : array |
||
| 470 | } |
||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * Returns the list of sub-client names configured for the client. |
||
| 475 | * |
||
| 476 | * @return array List of JQAdm client names |
||
| 477 | */ |
||
| 478 | protected function getSubClientNames() : array |
||
| 513 | } |
||
| 514 | |||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * Creates new and updates existing items using the data array |
||
| 519 | * |
||
| 520 | * @param array $data Data array |
||
| 521 | * @return \Aimeos\MShop\Customer\Item\Iface New customer item object |
||
| 522 | */ |
||
| 523 | protected function fromArray( array $data ) : \Aimeos\MShop\Customer\Item\Iface |
||
| 559 | } |
||
| 560 | |||
| 561 | |||
| 562 | /** |
||
| 563 | * Constructs the data array for the view from the given item |
||
| 564 | * |
||
| 565 | * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object |
||
| 566 | * @return string[] Multi-dimensional associative list of item data |
||
| 567 | */ |
||
| 568 | protected function toArray( \Aimeos\MShop\Customer\Item\Iface $item, bool $copy = false ) : array |
||
| 589 | } |
||
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * Returns the rendered template including the view data |
||
| 594 | * |
||
| 595 | * @param \Aimeos\Base\View\Iface $view View object with data assigned |
||
| 596 | * @return string HTML output |
||
| 597 | */ |
||
| 598 | protected function render( \Aimeos\Base\View\Iface $view ) : string |
||
| 624 |