| Total Complexity | 52 |
| Total Lines | 550 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 | /** |
||
| 28 | * Adds the required data used in the template |
||
| 29 | * |
||
| 30 | * @param \Aimeos\MW\View\Iface $view View object |
||
| 31 | * @return \Aimeos\MW\View\Iface View object with assigned parameters |
||
| 32 | */ |
||
| 33 | public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface |
||
| 34 | { |
||
| 35 | $codes = []; |
||
| 36 | |||
| 37 | foreach( $this->getContext()->config()->get( 'common/countries', [] ) as $code ) { |
||
| 38 | $codes[$code] = $view->translate( 'country', $code ); |
||
| 39 | } |
||
| 40 | |||
| 41 | asort( $codes ); |
||
| 42 | |||
| 43 | $view->itemSubparts = $this->getSubClientNames(); |
||
| 44 | $view->countries = $codes; |
||
| 45 | return $view; |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Copies a resource |
||
| 51 | * |
||
| 52 | * @return string|null HTML output |
||
| 53 | */ |
||
| 54 | public function copy() : ?string |
||
| 55 | { |
||
| 56 | $view = $this->getObject()->addData( $this->getView() ); |
||
| 57 | |||
| 58 | try |
||
| 59 | { |
||
| 60 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
| 61 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
| 62 | } |
||
| 63 | |||
| 64 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
||
| 65 | $view->item = $manager->get( $id, $this->getDomains() ); |
||
| 66 | |||
| 67 | $view->itemGroups = $this->getGroupItems( $view->item ); |
||
| 68 | $view->itemData = $this->toArray( $view->item, true ); |
||
| 69 | $view->itemBody = parent::copy(); |
||
| 70 | } |
||
| 71 | catch( \Exception $e ) |
||
| 72 | { |
||
| 73 | $this->report( $e, 'copy' ); |
||
| 74 | } |
||
| 75 | |||
| 76 | return $this->render( $view ); |
||
| 77 | } |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Creates a new resource |
||
| 82 | * |
||
| 83 | * @return string|null HTML output |
||
| 84 | */ |
||
| 85 | public function create() : ?string |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Deletes a resource |
||
| 114 | * |
||
| 115 | * @return string|null HTML output |
||
| 116 | */ |
||
| 117 | public function delete() : ?string |
||
| 118 | { |
||
| 119 | $view = $this->getView(); |
||
| 120 | $context = $this->getContext(); |
||
| 121 | |||
| 122 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
| 123 | $manager->begin(); |
||
| 124 | |||
| 125 | try |
||
| 126 | { |
||
| 127 | if( ( $ids = $view->param( 'id' ) ) === null ) { |
||
| 128 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
| 129 | } |
||
| 130 | |||
| 131 | if( !$view->access( ['super', 'admin'] ) ) { |
||
| 132 | throw new \Aimeos\Admin\JQAdm\Exception( 'Only super users and administrators can delete items' ); |
||
| 133 | } |
||
| 134 | |||
| 135 | $search = $manager->filter()->slice( 0, count( (array) $ids ) ); |
||
| 136 | $search->add( $search->and( [ |
||
| 137 | $search->compare( '==', 'customer.id', $ids ), |
||
| 138 | $search->compare( '!=', 'customer.siteid', '' ) |
||
| 139 | ] ) ); |
||
| 140 | |||
| 141 | $items = $manager->search( $search, $this->getDomains() ); |
||
| 142 | |||
| 143 | foreach( $items as $item ) |
||
| 144 | { |
||
| 145 | $view->item = $item; |
||
| 146 | parent::delete(); |
||
| 147 | } |
||
| 148 | |||
| 149 | $manager->delete( $items->toArray() ); |
||
| 150 | $manager->commit(); |
||
| 151 | |||
| 152 | if( $items->count() !== count( (array) $ids ) ) { |
||
| 153 | throw new \Aimeos\Admin\JQAdm\Exception( 'Not all items could be deleted' ); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $this->redirect( 'customer', 'search', null, 'delete' ); |
||
| 157 | } |
||
| 158 | catch( \Exception $e ) |
||
| 159 | { |
||
| 160 | $manager->rollback(); |
||
| 161 | $this->report( $e, 'delete' ); |
||
| 162 | } |
||
| 163 | |||
| 164 | return $this->search(); |
||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns a single resource |
||
| 170 | * |
||
| 171 | * @return string|null HTML output |
||
| 172 | */ |
||
| 173 | public function get() : ?string |
||
| 174 | { |
||
| 175 | $view = $this->getObject()->addData( $this->getView() ); |
||
| 176 | |||
| 177 | try |
||
| 178 | { |
||
| 179 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
| 180 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
| 181 | } |
||
| 182 | |||
| 183 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
||
| 184 | |||
| 185 | $view->item = $manager->get( $id, $this->getDomains() ); |
||
| 186 | $view->itemGroups = $this->getGroupItems( $view->item ); |
||
| 187 | $view->itemData = $this->toArray( $view->item ); |
||
| 188 | $view->itemBody = parent::get(); |
||
| 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(), 'customer' ); |
||
| 209 | $manager->begin(); |
||
| 210 | |||
| 211 | try |
||
| 212 | { |
||
| 213 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
| 214 | $view->item = $item->getId() ? $item : $manager->save( $item ); |
||
| 215 | $view->itemBody = parent::save(); |
||
| 216 | |||
| 217 | $manager->save( clone $view->item ); |
||
| 218 | $manager->commit(); |
||
| 219 | |||
| 220 | return $this->redirect( 'customer', $view->param( 'next' ), $view->item->getId(), 'save' ); |
||
|
|
|||
| 221 | } |
||
| 222 | catch( \Exception $e ) |
||
| 223 | { |
||
| 224 | $manager->rollback(); |
||
| 225 | $this->report( $e, 'save' ); |
||
| 226 | } |
||
| 227 | |||
| 228 | return $this->create(); |
||
| 229 | } |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns a list of resource according to the conditions |
||
| 234 | * |
||
| 235 | * @return string|null HTML output |
||
| 236 | */ |
||
| 237 | public function search() : ?string |
||
| 238 | { |
||
| 239 | $view = $this->getView(); |
||
| 240 | |||
| 241 | try |
||
| 242 | { |
||
| 243 | $total = 0; |
||
| 244 | $params = $this->storeFilter( $view->param(), 'customer' ); |
||
| 245 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
||
| 246 | $search = $this->initCriteria( $manager->filter(), $params ); |
||
| 247 | |||
| 248 | $view->items = $manager->search( $search, $this->getDomains(), $total ); |
||
| 249 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
| 250 | $view->filterOperators = $search->getOperators(); |
||
| 251 | $view->itemBody = parent::search(); |
||
| 252 | $view->total = $total; |
||
| 253 | } |
||
| 254 | catch( \Exception $e ) |
||
| 255 | { |
||
| 256 | $this->report( $e, 'search' ); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** admin/jqadm/customer/template-list |
||
| 260 | * Relative path to the HTML body template for the customer list. |
||
| 261 | * |
||
| 262 | * The template file contains the HTML code and processing instructions |
||
| 263 | * to generate the result shown in the body of the frontend. The |
||
| 264 | * configuration string is the path to the template file relative |
||
| 265 | * to the templates directory (usually in admin/jqadm/templates). |
||
| 266 | * |
||
| 267 | * You can overwrite the template file configuration in extensions and |
||
| 268 | * provide alternative templates. These alternative templates should be |
||
| 269 | * named like the default one but with the string "default" replaced by |
||
| 270 | * an unique name. You may use the name of your project for this. If |
||
| 271 | * you've implemented an alternative client class as well, "default" |
||
| 272 | * should be replaced by the name of the new class. |
||
| 273 | * |
||
| 274 | * @param string Relative path to the template creating the HTML code |
||
| 275 | * @since 2016.04 |
||
| 276 | * @category Developer |
||
| 277 | */ |
||
| 278 | $tplconf = 'admin/jqadm/customer/template-list'; |
||
| 279 | $default = 'customer/list-standard'; |
||
| 280 | |||
| 281 | return $view->render( $view->config( $tplconf, $default ) ); |
||
| 282 | } |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the sub-client given by its name. |
||
| 287 | * |
||
| 288 | * @param string $type Name of the client type |
||
| 289 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 290 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
| 291 | */ |
||
| 292 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
||
| 293 | { |
||
| 294 | /** admin/jqadm/customer/decorators/excludes |
||
| 295 | * Excludes decorators added by the "common" option from the customer JQAdm client |
||
| 296 | * |
||
| 297 | * Decorators extend the functionality of a class by adding new aspects |
||
| 298 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 299 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 300 | * modify what is returned to the caller. |
||
| 301 | * |
||
| 302 | * This option allows you to remove a decorator added via |
||
| 303 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
| 304 | * around the JQAdm client. |
||
| 305 | * |
||
| 306 | * admin/jqadm/customer/decorators/excludes = array( 'decorator1' ) |
||
| 307 | * |
||
| 308 | * This would remove the decorator named "decorator1" from the list of |
||
| 309 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
| 310 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
| 311 | * |
||
| 312 | * @param array List of decorator names |
||
| 313 | * @since 2017.07 |
||
| 314 | * @category Developer |
||
| 315 | * @see admin/jqadm/common/decorators/default |
||
| 316 | * @see admin/jqadm/customer/decorators/global |
||
| 317 | * @see admin/jqadm/customer/decorators/local |
||
| 318 | */ |
||
| 319 | |||
| 320 | /** admin/jqadm/customer/decorators/global |
||
| 321 | * Adds a list of globally available decorators only to the customer JQAdm client |
||
| 322 | * |
||
| 323 | * Decorators extend the functionality of a class by adding new aspects |
||
| 324 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 325 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 326 | * modify what is returned to the caller. |
||
| 327 | * |
||
| 328 | * This option allows you to wrap global decorators |
||
| 329 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
| 330 | * |
||
| 331 | * admin/jqadm/customer/decorators/global = array( 'decorator1' ) |
||
| 332 | * |
||
| 333 | * This would add the decorator named "decorator1" defined by |
||
| 334 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
| 335 | * |
||
| 336 | * @param array List of decorator names |
||
| 337 | * @since 2017.07 |
||
| 338 | * @category Developer |
||
| 339 | * @see admin/jqadm/common/decorators/default |
||
| 340 | * @see admin/jqadm/customer/decorators/excludes |
||
| 341 | * @see admin/jqadm/customer/decorators/local |
||
| 342 | */ |
||
| 343 | |||
| 344 | /** admin/jqadm/customer/decorators/local |
||
| 345 | * Adds a list of local decorators only to the customer JQAdm client |
||
| 346 | * |
||
| 347 | * Decorators extend the functionality of a class by adding new aspects |
||
| 348 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 349 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 350 | * modify what is returned to the caller. |
||
| 351 | * |
||
| 352 | * This option allows you to wrap local decorators |
||
| 353 | * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client. |
||
| 354 | * |
||
| 355 | * admin/jqadm/customer/decorators/local = array( 'decorator2' ) |
||
| 356 | * |
||
| 357 | * This would add the decorator named "decorator2" defined by |
||
| 358 | * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client. |
||
| 359 | * |
||
| 360 | * @param array List of decorator names |
||
| 361 | * @since 2017.07 |
||
| 362 | * @category Developer |
||
| 363 | * @see admin/jqadm/common/decorators/default |
||
| 364 | * @see admin/jqadm/customer/decorators/excludes |
||
| 365 | * @see admin/jqadm/customer/decorators/global |
||
| 366 | */ |
||
| 367 | return $this->createSubClient( 'customer/' . $type, $name ); |
||
| 368 | } |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns the domain names whose items should be fetched too |
||
| 373 | * |
||
| 374 | * @return string[] List of domain names |
||
| 375 | */ |
||
| 376 | protected function getDomains() : array |
||
| 377 | { |
||
| 378 | /** admin/jqadm/customer/domains |
||
| 379 | * List of domain items that should be fetched along with the customer |
||
| 380 | * |
||
| 381 | * If you need to display additional content, you can configure your own |
||
| 382 | * list of domains (attribute, media, price, customer, text, etc. are |
||
| 383 | * domains) whose items are fetched from the storage. |
||
| 384 | * |
||
| 385 | * @param array List of domain names |
||
| 386 | * @since 2017.07 |
||
| 387 | * @category Developer |
||
| 388 | */ |
||
| 389 | return $this->getContext()->getConfig()->get( 'admin/jqadm/customer/domains', [] ); |
||
| 390 | } |
||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * Returns the available group items |
||
| 395 | * |
||
| 396 | * @param \Aimeos\MShop\Customer\Item\Iface|null $item Customer item that should be updated |
||
| 397 | * @return \Aimeos\MShop\Customer\Item\Group\Iface[] Associative list of group IDs as keys and group items as values |
||
| 398 | */ |
||
| 399 | protected function getGroupItems( \Aimeos\MShop\Customer\Item\Iface $item = null ) : array |
||
| 400 | { |
||
| 401 | $list = []; |
||
| 402 | $view = $this->getView(); |
||
| 403 | $context = $this->getContext(); |
||
| 404 | |||
| 405 | $isSuper = $view->access( ['super'] ); |
||
| 406 | $isAdmin = $view->access( ['admin'] ); |
||
| 407 | $isEditor = $view->access( ['editor'] ); |
||
| 408 | |||
| 409 | $manager = \Aimeos\MShop::create( $context, 'customer/group' ); |
||
| 410 | $search = $manager->filter( true )->slice( 0, 10000 )->order( 'customer.group.label' ); |
||
| 411 | |||
| 412 | foreach( $manager->search( $search ) as $groupId => $groupItem ) |
||
| 413 | { |
||
| 414 | if( !$isSuper && $groupItem->getCode() === 'super' ) { |
||
| 415 | continue; |
||
| 416 | } |
||
| 417 | |||
| 418 | if( !$isSuper && !$isAdmin && $groupItem->getCode() === 'admin' ) { |
||
| 419 | continue; |
||
| 420 | } |
||
| 421 | |||
| 422 | if( !$isSuper && !$isAdmin && $groupItem->getCode() === 'editor' |
||
| 423 | && ( !$isEditor || $item === null || (string) $context->getUserId() !== (string) $item->getId() ) |
||
| 424 | ) { |
||
| 425 | continue; |
||
| 426 | } |
||
| 427 | |||
| 428 | $list[$groupId] = $groupItem; |
||
| 429 | } |
||
| 430 | |||
| 431 | return $list; |
||
| 432 | } |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * Returns the list of sub-client names configured for the client. |
||
| 437 | * |
||
| 438 | * @return array List of JQAdm client names |
||
| 439 | */ |
||
| 440 | protected function getSubClientNames() : array |
||
| 441 | { |
||
| 442 | /** admin/jqadm/customer/subparts |
||
| 443 | * List of JQAdm sub-clients rendered within the customer section |
||
| 444 | * |
||
| 445 | * The output of the frontend is composed of the code generated by the JQAdm |
||
| 446 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
| 447 | * that are responsible for rendering certain sub-parts of the output. The |
||
| 448 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
| 449 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
| 450 | * the output that is placed inside the container of its parent. |
||
| 451 | * |
||
| 452 | * At first, always the JQAdm code generated by the parent is printed, then |
||
| 453 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
| 454 | * determines the order of the output of these sub-clients inside the parent |
||
| 455 | * container. If the configured list of clients is |
||
| 456 | * |
||
| 457 | * array( "subclient1", "subclient2" ) |
||
| 458 | * |
||
| 459 | * you can easily change the order of the output by reordering the subparts: |
||
| 460 | * |
||
| 461 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
| 462 | * |
||
| 463 | * You can also remove one or more parts if they shouldn't be rendered: |
||
| 464 | * |
||
| 465 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
| 466 | * |
||
| 467 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
| 468 | * should support adding, removing or reordering content by a fluid like |
||
| 469 | * design. |
||
| 470 | * |
||
| 471 | * @param array List of sub-client names |
||
| 472 | * @since 2017.07 |
||
| 473 | * @category Developer |
||
| 474 | */ |
||
| 475 | return $this->getContext()->getConfig()->get( 'admin/jqadm/customer/subparts', [] ); |
||
| 476 | } |
||
| 477 | |||
| 478 | |||
| 479 | |||
| 480 | /** |
||
| 481 | * Creates new and updates existing items using the data array |
||
| 482 | * |
||
| 483 | * @param array $data Data array |
||
| 484 | * @return \Aimeos\MShop\Customer\Item\Iface New customer item object |
||
| 485 | */ |
||
| 486 | protected function fromArray( array $data ) : \Aimeos\MShop\Customer\Item\Iface |
||
| 487 | { |
||
| 488 | $context = $this->getContext(); |
||
| 489 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
| 490 | |||
| 491 | if( isset( $data['customer.id'] ) && $data['customer.id'] != '' ) { |
||
| 492 | $item = $manager->get( $data['customer.id'], $this->getDomains() ); |
||
| 493 | } else { |
||
| 494 | $item = $manager->create(); |
||
| 495 | } |
||
| 496 | |||
| 497 | $addr = $item->getPaymentAddress(); |
||
| 498 | $label = ( $addr->getFirstname() ? $addr->getFirstname() . ' ' : '' ) . $addr->getLastname(); |
||
| 499 | $label .= ( $addr->getCompany() ? ' (' . $addr->getCompany() . ')' : '' ); |
||
| 500 | |||
| 501 | $groupIds = $this->getValue( $data, 'customer.groups', [] ); |
||
| 502 | $gids = array_keys( $this->getGroupItems( $item ) ); |
||
| 503 | |||
| 504 | $item->setLabel( $label )->setStatus( $data['customer.status'] ?? 0 ) |
||
| 505 | ->setGroups( array_intersect( $gids, $groupIds ) ); |
||
| 506 | |||
| 507 | if( $this->getView()->access( ['super'] ) || $item->getId() === $context->getUserId() ) |
||
| 508 | { |
||
| 509 | !isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] ); |
||
| 510 | !isset( $data['customer.code'] ) ?: $item->setCode( $data['customer.code'] ); |
||
| 511 | } |
||
| 512 | |||
| 513 | return $item->fromArray( $data ); |
||
| 514 | } |
||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * Constructs the data array for the view from the given item |
||
| 519 | * |
||
| 520 | * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object |
||
| 521 | * @return string[] Multi-dimensional associative list of item data |
||
| 522 | */ |
||
| 523 | protected function toArray( \Aimeos\MShop\Customer\Item\Iface $item, bool $copy = false ) : array |
||
| 539 | } |
||
| 540 | |||
| 541 | |||
| 542 | /** |
||
| 543 | * Returns the rendered template including the view data |
||
| 544 | * |
||
| 545 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
| 546 | * @return string HTML output |
||
| 547 | */ |
||
| 548 | protected function render( \Aimeos\MW\View\Iface $view ) : string |
||
| 573 | } |
||
| 574 | } |
||
| 575 |