| Total Complexity | 40 |
| Total Lines | 519 |
| 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 |
||
| 21 | class Standard |
||
| 22 | extends \Aimeos\Client\Html\Common\Client\Factory\Base |
||
| 23 | implements \Aimeos\Client\Html\Common\Client\Factory\Iface |
||
| 24 | { |
||
| 25 | /** client/html/account/favorite/standard/subparts |
||
| 26 | * List of HTML sub-clients rendered within the account favorite section |
||
| 27 | * |
||
| 28 | * The output of the frontend is composed of the code generated by the HTML |
||
| 29 | * clients. Each HTML client can consist of serveral (or none) sub-clients |
||
| 30 | * that are responsible for rendering certain sub-parts of the output. The |
||
| 31 | * sub-clients can contain HTML clients themselves and therefore a |
||
| 32 | * hierarchical tree of HTML clients is composed. Each HTML client creates |
||
| 33 | * the output that is placed inside the container of its parent. |
||
| 34 | * |
||
| 35 | * At first, always the HTML code generated by the parent is printed, then |
||
| 36 | * the HTML code of its sub-clients. The order of the HTML sub-clients |
||
| 37 | * determines the order of the output of these sub-clients inside the parent |
||
| 38 | * container. If the configured list of clients is |
||
| 39 | * |
||
| 40 | * array( "subclient1", "subclient2" ) |
||
| 41 | * |
||
| 42 | * you can easily change the order of the output by reordering the subparts: |
||
| 43 | * |
||
| 44 | * client/html/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
| 45 | * |
||
| 46 | * You can also remove one or more parts if they shouldn't be rendered: |
||
| 47 | * |
||
| 48 | * client/html/<clients>/subparts = array( "subclient1" ) |
||
| 49 | * |
||
| 50 | * As the clients only generates structural HTML, the layout defined via CSS |
||
| 51 | * should support adding, removing or reordering content by a fluid like |
||
| 52 | * design. |
||
| 53 | * |
||
| 54 | * @param array List of sub-client names |
||
| 55 | * @since 2014.03 |
||
| 56 | * @category Developer |
||
| 57 | */ |
||
| 58 | private $subPartPath = 'client/html/account/favorite/standard/subparts'; |
||
| 59 | private $subPartNames = []; |
||
| 60 | private $view; |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns the HTML code for insertion into the body. |
||
| 65 | * |
||
| 66 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 67 | * @return string HTML code |
||
| 68 | */ |
||
| 69 | public function getBody( $uid = '' ) |
||
| 70 | { |
||
| 71 | $context = $this->getContext(); |
||
| 72 | $view = $this->getView(); |
||
| 73 | |||
| 74 | try |
||
| 75 | { |
||
| 76 | if( !isset( $this->view ) ) { |
||
| 77 | $view = $this->view = $this->getObject()->addData( $view ); |
||
| 78 | } |
||
| 79 | |||
| 80 | $html = ''; |
||
| 81 | foreach( $this->getSubClients() as $subclient ) { |
||
| 82 | $html .= $subclient->setView( $view )->getBody( $uid ); |
||
| 83 | } |
||
| 84 | $view->favoriteBody = $html; |
||
| 85 | } |
||
| 86 | catch( \Aimeos\Client\Html\Exception $e ) |
||
| 87 | { |
||
| 88 | $error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) ); |
||
| 89 | $view->favoriteErrorList = $view->get( 'favoriteErrorList', [] ) + $error; |
||
| 90 | } |
||
| 91 | catch( \Aimeos\Controller\Frontend\Exception $e ) |
||
| 92 | { |
||
| 93 | $error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) ); |
||
| 94 | $view->favoriteErrorList = $view->get( 'favoriteErrorList', [] ) + $error; |
||
| 95 | } |
||
| 96 | catch( \Aimeos\MShop\Exception $e ) |
||
| 97 | { |
||
| 98 | $error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 99 | $view->favoriteErrorList = $view->get( 'favoriteErrorList', [] ) + $error; |
||
| 100 | } |
||
| 101 | catch( \Exception $e ) |
||
| 102 | { |
||
| 103 | $error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) ); |
||
| 104 | $view->favoriteErrorList = $view->get( 'favoriteErrorList', [] ) + $error; |
||
| 105 | $this->logException( $e ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** client/html/account/favorite/standard/template-body |
||
| 109 | * Relative path to the HTML body template of the account favorite client. |
||
| 110 | * |
||
| 111 | * The template file contains the HTML code and processing instructions |
||
| 112 | * to generate the result shown in the body of the frontend. The |
||
| 113 | * configuration string is the path to the template file relative |
||
| 114 | * to the templates directory (usually in client/html/templates). |
||
| 115 | * |
||
| 116 | * You can overwrite the template file configuration in extensions and |
||
| 117 | * provide alternative templates. These alternative templates should be |
||
| 118 | * named like the default one but with the string "standard" replaced by |
||
| 119 | * an unique name. You may use the name of your project for this. If |
||
| 120 | * you've implemented an alternative client class as well, "standard" |
||
| 121 | * should be replaced by the name of the new class. |
||
| 122 | * |
||
| 123 | * @param string Relative path to the template creating code for the HTML page body |
||
| 124 | * @since 2014.03 |
||
| 125 | * @category Developer |
||
| 126 | * @see client/html/account/favorite/standard/template-header |
||
| 127 | */ |
||
| 128 | $tplconf = 'client/html/account/favorite/standard/template-body'; |
||
| 129 | $default = 'account/favorite/body-standard'; |
||
| 130 | |||
| 131 | return $view->render( $view->config( $tplconf, $default ) ); |
||
| 132 | } |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns the HTML string for insertion into the header. |
||
| 137 | * |
||
| 138 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 139 | * @return string|null String including HTML tags for the header on error |
||
| 140 | */ |
||
| 141 | public function getHeader( $uid = '' ) |
||
| 142 | { |
||
| 143 | $view = $this->getView(); |
||
| 144 | |||
| 145 | try |
||
| 146 | { |
||
| 147 | if( !isset( $this->view ) ) { |
||
| 148 | $view = $this->view = $this->getObject()->addData( $view ); |
||
| 149 | } |
||
| 150 | |||
| 151 | $html = ''; |
||
| 152 | foreach( $this->getSubClients() as $subclient ) { |
||
| 153 | $html .= $subclient->setView( $view )->getHeader( $uid ); |
||
| 154 | } |
||
| 155 | $view->favoriteHeader = $html; |
||
| 156 | |||
| 157 | /** client/html/account/favorite/standard/template-header |
||
| 158 | * Relative path to the HTML header template of the account favorite client. |
||
| 159 | * |
||
| 160 | * The template file contains the HTML code and processing instructions |
||
| 161 | * to generate the HTML code that is inserted into the HTML page header |
||
| 162 | * of the rendered page in the frontend. The configuration string is the |
||
| 163 | * path to the template file relative to the templates directory (usually |
||
| 164 | * in client/html/templates). |
||
| 165 | * |
||
| 166 | * You can overwrite the template file configuration in extensions and |
||
| 167 | * provide alternative templates. These alternative templates should be |
||
| 168 | * named like the default one but with the string "standard" replaced by |
||
| 169 | * an unique name. You may use the name of your project for this. If |
||
| 170 | * you've implemented an alternative client class as well, "standard" |
||
| 171 | * should be replaced by the name of the new class. |
||
| 172 | * |
||
| 173 | * @param string Relative path to the template creating code for the HTML page head |
||
| 174 | * @since 2014.03 |
||
| 175 | * @category Developer |
||
| 176 | * @see client/html/account/favorite/standard/template-body |
||
| 177 | */ |
||
| 178 | $tplconf = 'client/html/account/favorite/standard/template-header'; |
||
| 179 | $default = 'account/favorite/header-standard'; |
||
| 180 | |||
| 181 | return $view->render( $view->config( $tplconf, $default ) ); |
||
| 182 | } |
||
| 183 | catch( \Exception $e ) |
||
| 184 | { |
||
| 185 | $this->logException( $e ); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Returns the sub-client given by its name. |
||
| 192 | * |
||
| 193 | * @param string $type Name of the client type |
||
| 194 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 195 | * @return \Aimeos\Client\Html\Iface Sub-client object |
||
| 196 | */ |
||
| 197 | public function getSubClient( $type, $name = null ) |
||
| 198 | { |
||
| 199 | /** client/html/account/favorite/decorators/excludes |
||
| 200 | * Excludes decorators added by the "common" option from the account favorite html client |
||
| 201 | * |
||
| 202 | * Decorators extend the functionality of a class by adding new aspects |
||
| 203 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 204 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 205 | * modify what is returned to the caller. |
||
| 206 | * |
||
| 207 | * This option allows you to remove a decorator added via |
||
| 208 | * "client/html/common/decorators/default" before they are wrapped |
||
| 209 | * around the html client. |
||
| 210 | * |
||
| 211 | * client/html/account/favorite/decorators/excludes = array( 'decorator1' ) |
||
| 212 | * |
||
| 213 | * This would remove the decorator named "decorator1" from the list of |
||
| 214 | * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via |
||
| 215 | * "client/html/common/decorators/default" to the html client. |
||
| 216 | * |
||
| 217 | * @param array List of decorator names |
||
| 218 | * @since 2014.05 |
||
| 219 | * @category Developer |
||
| 220 | * @see client/html/common/decorators/default |
||
| 221 | * @see client/html/account/favorite/decorators/global |
||
| 222 | * @see client/html/account/favorite/decorators/local |
||
| 223 | */ |
||
| 224 | |||
| 225 | /** client/html/account/favorite/decorators/global |
||
| 226 | * Adds a list of globally available decorators only to the account favorite html client |
||
| 227 | * |
||
| 228 | * Decorators extend the functionality of a class by adding new aspects |
||
| 229 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 230 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 231 | * modify what is returned to the caller. |
||
| 232 | * |
||
| 233 | * This option allows you to wrap global decorators |
||
| 234 | * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client. |
||
| 235 | * |
||
| 236 | * client/html/account/favorite/decorators/global = array( 'decorator1' ) |
||
| 237 | * |
||
| 238 | * This would add the decorator named "decorator1" defined by |
||
| 239 | * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client. |
||
| 240 | * |
||
| 241 | * @param array List of decorator names |
||
| 242 | * @since 2014.05 |
||
| 243 | * @category Developer |
||
| 244 | * @see client/html/common/decorators/default |
||
| 245 | * @see client/html/account/favorite/decorators/excludes |
||
| 246 | * @see client/html/account/favorite/decorators/local |
||
| 247 | */ |
||
| 248 | |||
| 249 | /** client/html/account/favorite/decorators/local |
||
| 250 | * Adds a list of local decorators only to the account favorite html client |
||
| 251 | * |
||
| 252 | * Decorators extend the functionality of a class by adding new aspects |
||
| 253 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 254 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 255 | * modify what is returned to the caller. |
||
| 256 | * |
||
| 257 | * This option allows you to wrap local decorators |
||
| 258 | * ("\Aimeos\Client\Html\Account\Decorator\*") around the html client. |
||
| 259 | * |
||
| 260 | * client/html/account/favorite/decorators/local = array( 'decorator2' ) |
||
| 261 | * |
||
| 262 | * This would add the decorator named "decorator2" defined by |
||
| 263 | * "\Aimeos\Client\Html\Account\Decorator\Decorator2" only to the html client. |
||
| 264 | * |
||
| 265 | * @param array List of decorator names |
||
| 266 | * @since 2014.05 |
||
| 267 | * @category Developer |
||
| 268 | * @see client/html/common/decorators/default |
||
| 269 | * @see client/html/account/favorite/decorators/excludes |
||
| 270 | * @see client/html/account/favorite/decorators/global |
||
| 271 | */ |
||
| 272 | return $this->createSubClient( 'account/favorite/' . $type, $name ); |
||
| 273 | } |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Processes the input, e.g. store given values. |
||
| 278 | * A view must be available and this method doesn't generate any output |
||
| 279 | * besides setting view variables. |
||
| 280 | */ |
||
| 281 | public function process() |
||
| 282 | { |
||
| 283 | $view = $this->getView(); |
||
| 284 | $context = $this->getContext(); |
||
| 285 | $userId = $context->getUserId(); |
||
| 286 | $ids = (array) $view->param( 'fav_id', [] ); |
||
| 287 | |||
| 288 | try |
||
| 289 | { |
||
| 290 | if( $userId != null && !empty( $ids ) ) |
||
| 291 | { |
||
| 292 | switch( $view->param( 'fav_action' ) ) |
||
| 293 | { |
||
| 294 | case 'add': |
||
| 295 | $this->addFavorites( $ids, $userId ); break; |
||
| 296 | case 'delete': |
||
| 297 | $this->deleteFavorites( $ids, $userId ); break; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | parent::process(); |
||
| 302 | } |
||
| 303 | catch( \Aimeos\MShop\Exception $e ) |
||
| 304 | { |
||
| 305 | $error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 306 | $view->favoriteErrorList = $view->get( 'favoriteErrorList', [] ) + $error; |
||
| 307 | } |
||
| 308 | catch( \Aimeos\Controller\Frontend\Exception $e ) |
||
| 309 | { |
||
| 310 | $error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) ); |
||
| 311 | $view->favoriteErrorList = $view->get( 'favoriteErrorList', [] ) + $error; |
||
| 312 | } |
||
| 313 | catch( \Aimeos\Client\Html\Exception $e ) |
||
| 314 | { |
||
| 315 | $error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) ); |
||
| 316 | $view->favoriteErrorList = $view->get( 'favoriteErrorList', [] ) + $error; |
||
| 317 | } |
||
| 318 | catch( \Exception $e ) |
||
| 319 | { |
||
| 320 | $error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) ); |
||
| 321 | $view->favoriteErrorList = $view->get( 'favoriteErrorList', [] ) + $error; |
||
| 322 | $this->logException( $e ); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns the customer list items referencing the favorite products |
||
| 329 | * |
||
| 330 | * @param array $ids List of product IDs |
||
| 331 | * @param string $userId Unique customer ID |
||
| 332 | * @param string $type Type of the list item |
||
| 333 | * @return array Associative list of product IDs as keys and list items as values |
||
| 334 | */ |
||
| 335 | protected function getListItems( array $ids, $userId, $type ) |
||
| 336 | { |
||
| 337 | $context = $this->getContext(); |
||
| 338 | $manager = \Aimeos\MShop::create( $context, 'customer/lists' ); |
||
| 339 | |||
| 340 | $search = $manager->createSearch(); |
||
| 341 | $expr = array( |
||
| 342 | $search->compare( '==', 'customer.lists.parentid', $userId ), |
||
| 343 | $search->compare( '==', 'customer.lists.refid', $ids ), |
||
| 344 | $search->compare( '==', 'customer.lists.domain', 'product' ), |
||
| 345 | $search->compare( '==', 'customer.lists.type', $type ), |
||
| 346 | ); |
||
| 347 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
| 348 | |||
| 349 | $items = []; |
||
| 350 | foreach( $manager->searchItems( $search ) as $item ) { |
||
| 351 | $items[$item->getRefId()] = $item; |
||
|
|
|||
| 352 | } |
||
| 353 | |||
| 354 | return $items; |
||
| 355 | } |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * Adds new product favorite references to the given customer |
||
| 360 | * |
||
| 361 | * @param array $ids List of product IDs |
||
| 362 | * @param string $userId Unique customer ID |
||
| 363 | */ |
||
| 364 | protected function addFavorites( array $ids, $userId ) |
||
| 365 | { |
||
| 366 | $context = $this->getContext(); |
||
| 367 | $manager = \Aimeos\MShop::create( $context, 'customer/lists' ); |
||
| 368 | $listItems = $this->getListItems( $ids, $userId, 'favorite' ); |
||
| 369 | |||
| 370 | $item = $manager->createItem(); |
||
| 371 | $item->setDomain( 'product' ); |
||
| 372 | $item->setParentId( $userId ); |
||
| 373 | $item->setType( 'favorite' ); |
||
| 374 | $item->setStatus( 1 ); |
||
| 375 | |||
| 376 | foreach( $ids as $id ) |
||
| 377 | { |
||
| 378 | if( !isset( $listItems[$id] ) ) |
||
| 379 | { |
||
| 380 | $item->setId( null ); |
||
| 381 | $item->setRefId( $id ); |
||
| 382 | |||
| 383 | $item = $manager->saveItem( $item ); |
||
| 384 | $manager->moveItem( $item->getId() ); |
||
|
1 ignored issue
–
show
|
|||
| 385 | } |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * Removes product favorite references from the customer |
||
| 392 | * |
||
| 393 | * @param array $ids List of product IDs |
||
| 394 | * @param string $userId Unique customer ID |
||
| 395 | */ |
||
| 396 | protected function deleteFavorites( array $ids, $userId ) |
||
| 412 | } |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns the list of sub-client names configured for the client. |
||
| 417 | * |
||
| 418 | * @return array List of HTML client names |
||
| 419 | */ |
||
| 420 | protected function getSubClientNames() |
||
| 421 | { |
||
| 422 | return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames ); |
||
| 423 | } |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * Returns the sanitized page from the parameters for the product list. |
||
| 428 | * |
||
| 429 | * @param \Aimeos\MW\View\Iface $view View instance with helper for retrieving the required parameters |
||
| 430 | * @return integer Page number starting from 1 |
||
| 431 | */ |
||
| 432 | protected function getProductListPage( \Aimeos\MW\View\Iface $view ) |
||
| 436 | } |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Returns the sanitized page size from the parameters for the product list. |
||
| 441 | * |
||
| 442 | * @param \Aimeos\MW\View\Iface $view View instance with helper for retrieving the required parameters |
||
| 443 | * @return integer Page size |
||
| 444 | */ |
||
| 445 | protected function getProductListSize( \Aimeos\MW\View\Iface $view ) |
||
| 470 | } |
||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * Sets the necessary parameter values in the view. |
||
| 475 | * |
||
| 476 | * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output |
||
| 477 | * @param array &$tags Result array for the list of tags that are associated to the output |
||
| 478 | * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry) |
||
| 479 | * @return \Aimeos\MW\View\Iface Modified view object |
||
| 480 | */ |
||
| 481 | public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], &$expire = null ) |
||
| 540 | } |
||
| 541 | } |
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.