| Total Complexity | 40 |
| Total Lines | 754 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 2 |
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\Catalog\Base |
||
| 23 | implements \Aimeos\Client\Html\Common\Client\Factory\Iface |
||
| 24 | { |
||
| 25 | /** client/html/catalog/lists/subparts |
||
| 26 | * List of HTML sub-clients rendered within the catalog list 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/catalog/lists/subparts'; |
||
| 59 | |||
| 60 | /** client/html/catalog/lists/promo/name |
||
| 61 | * Name of the promotion part used by the catalog list client implementation |
||
| 62 | * |
||
| 63 | * Use "Myname" if your class is named "\Aimeos\Client\Html\Catalog\Lists\Promo\Myname". |
||
| 64 | * The name is case-sensitive and you should avoid camel case names like "MyName". |
||
| 65 | * |
||
| 66 | * @param string Last part of the client class name |
||
| 67 | * @since 2014.03 |
||
| 68 | * @category Developer |
||
| 69 | */ |
||
| 70 | |||
| 71 | /** client/html/catalog/lists/items/name |
||
| 72 | * Name of the items part used by the catalog list client implementation |
||
| 73 | * |
||
| 74 | * Use "Myname" if your class is named "\Aimeos\Client\Html\Catalog\Lists\Items\Myname". |
||
| 75 | * The name is case-sensitive and you should avoid camel case names like "MyName". |
||
| 76 | * |
||
| 77 | * @param string Last part of the client class name |
||
| 78 | * @since 2014.03 |
||
| 79 | * @category Developer |
||
| 80 | */ |
||
| 81 | private $subPartNames = array( 'items' ); |
||
| 82 | |||
| 83 | private $tags = []; |
||
| 84 | private $expire; |
||
| 85 | private $view; |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Returns the HTML code for insertion into the body. |
||
| 90 | * |
||
| 91 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 92 | * @return string HTML code |
||
| 93 | */ |
||
| 94 | public function body( string $uid = '' ) : string |
||
| 95 | { |
||
| 96 | $view = $this->getView(); |
||
| 97 | $context = $this->getContext(); |
||
| 98 | $prefixes = ['f_catid', 'f_supid', 'f_sort', 'l_page', 'l_type']; |
||
| 99 | |||
| 100 | /** client/html/catalog/lists/cache |
||
| 101 | * Enables or disables caching only for the catalog lists component |
||
| 102 | * |
||
| 103 | * Disable caching for components can be useful if you would have too much |
||
| 104 | * entries to cache or if the component contains non-cacheable parts that |
||
| 105 | * can't be replaced using the modifyBody() and modifyHeader() methods. |
||
| 106 | * |
||
| 107 | * @param boolean True to enable caching, false to disable |
||
| 108 | * @category Developer |
||
| 109 | * @category User |
||
| 110 | * @see client/html/catalog/detail/cache |
||
| 111 | * @see client/html/catalog/filter/cache |
||
| 112 | * @see client/html/catalog/stage/cache |
||
| 113 | */ |
||
| 114 | |||
| 115 | /** client/html/catalog/lists |
||
| 116 | * All parameters defined for the catalog list component and its subparts |
||
| 117 | * |
||
| 118 | * This returns all settings related to the filter component. |
||
| 119 | * Please refer to the single settings for details. |
||
| 120 | * |
||
| 121 | * @param array Associative list of name/value settings |
||
| 122 | * @category Developer |
||
| 123 | * @see client/html/catalog#list |
||
| 124 | */ |
||
| 125 | $confkey = 'client/html/catalog/lists'; |
||
| 126 | |||
| 127 | $args = map( $view->param() )->except( $prefixes )->filter( function( $val, $key ) { |
||
| 128 | return !strncmp( $key, 'f_', 2 ) || !strncmp( $key, 'l_', 2 ); |
||
| 129 | } ); |
||
| 130 | |||
| 131 | if( !$args->isEmpty() || ( $html = $this->getCached( 'body', $uid, $prefixes, $confkey ) ) === null ) |
||
| 132 | { |
||
| 133 | /** client/html/catalog/lists/template-body |
||
| 134 | * Relative path to the HTML body template of the catalog list client. |
||
| 135 | * |
||
| 136 | * The template file contains the HTML code and processing instructions |
||
| 137 | * to generate the result shown in the body of the frontend. The |
||
| 138 | * configuration string is the path to the template file relative |
||
| 139 | * to the templates directory (usually in client/html/templates). |
||
| 140 | * |
||
| 141 | * You can overwrite the template file configuration in extensions and |
||
| 142 | * provide alternative templates. These alternative templates should be |
||
| 143 | * named like the default one but with the string "standard" replaced by |
||
| 144 | * an unique name. You may use the name of your project for this. If |
||
| 145 | * you've implemented an alternative client class as well, "standard" |
||
| 146 | * should be replaced by the name of the new class. |
||
| 147 | * |
||
| 148 | * It's also possible to create a specific template for each type, e.g. |
||
| 149 | * for the grid, list or whatever view you want to offer your users. In |
||
| 150 | * that case, you can configure the template by adding "-<type>" to the |
||
| 151 | * configuration key. To configure an alternative list view template for |
||
| 152 | * example, use the key |
||
| 153 | * |
||
| 154 | * client/html/catalog/lists/template-body-list = catalog/lists/body-list.php |
||
| 155 | * |
||
| 156 | * The argument is the relative path to the new template file. The type of |
||
| 157 | * the view is determined by the "l_type" parameter (allowed characters for |
||
| 158 | * the types are a-z and 0-9), which is also stored in the session so users |
||
| 159 | * will keep the view during their visit. The catalog list type subpart |
||
| 160 | * contains the template for switching between list types. |
||
| 161 | * |
||
| 162 | * @param string Relative path to the template creating code for the HTML page body |
||
| 163 | * @since 2014.03 |
||
| 164 | * @category Developer |
||
| 165 | * @see client/html/catalog/lists/template-header |
||
| 166 | * @see client/html/catalog/lists/type/template-body |
||
| 167 | */ |
||
| 168 | $tplconf = 'client/html/catalog/lists/template-body'; |
||
| 169 | $default = 'catalog/lists/body-standard'; |
||
| 170 | |||
| 171 | try |
||
| 172 | { |
||
| 173 | $html = ''; |
||
| 174 | |||
| 175 | if( !isset( $this->view ) ) { |
||
| 176 | $view = $this->view = $this->getObject()->data( $view, $this->tags, $this->expire ); |
||
| 177 | } |
||
| 178 | |||
| 179 | foreach( $this->getSubClients() as $subclient ) { |
||
| 180 | $html .= $subclient->setView( $view )->body( $uid ); |
||
| 181 | } |
||
| 182 | $view->listBody = $html; |
||
| 183 | |||
| 184 | $html = $view->render( $this->getTemplatePath( $tplconf, $default ) ); |
||
| 185 | |||
| 186 | if( $args->isEmpty() ) { |
||
| 187 | $this->setCached( 'body', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire ); |
||
| 188 | } |
||
| 189 | |||
| 190 | return $html; |
||
| 191 | } |
||
| 192 | catch( \Aimeos\Client\Html\Exception $e ) |
||
| 193 | { |
||
| 194 | $error = array( $context->translate( 'client', $e->getMessage() ) ); |
||
| 195 | $view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error ); |
||
| 196 | } |
||
| 197 | catch( \Aimeos\Controller\Frontend\Exception $e ) |
||
| 198 | { |
||
| 199 | $error = array( $context->translate( 'controller/frontend', $e->getMessage() ) ); |
||
| 200 | $view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error ); |
||
| 201 | } |
||
| 202 | catch( \Aimeos\MShop\Exception $e ) |
||
| 203 | { |
||
| 204 | $error = array( $context->translate( 'mshop', $e->getMessage() ) ); |
||
| 205 | $view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error ); |
||
| 206 | } |
||
| 207 | catch( \Exception $e ) |
||
| 208 | { |
||
| 209 | $error = array( $context->translate( 'client', 'A non-recoverable error occured' ) ); |
||
| 210 | $view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error ); |
||
| 211 | $this->logException( $e ); |
||
| 212 | } |
||
| 213 | |||
| 214 | $html = $view->render( $this->getTemplatePath( $tplconf, $default ) ); |
||
| 215 | } |
||
| 216 | else |
||
| 217 | { |
||
| 218 | $html = $this->modifyBody( $html, $uid ); |
||
| 219 | } |
||
| 220 | |||
| 221 | return $html; |
||
| 222 | } |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * Returns the HTML string for insertion into the header. |
||
| 227 | * |
||
| 228 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 229 | * @return string|null String including HTML tags for the header on error |
||
| 230 | */ |
||
| 231 | public function header( string $uid = '' ) : ?string |
||
| 232 | { |
||
| 233 | $view = $this->getView(); |
||
| 234 | $confkey = 'client/html/catalog/lists'; |
||
| 235 | $prefixes = ['f_catid', 'f_supid', 'f_sort', 'l_page', 'l_type']; |
||
| 236 | |||
| 237 | $args = map( $view->param() )->except( $prefixes )->filter( function( $val, $key ) { |
||
| 238 | return !strncmp( $key, 'f_', 2 ) || !strncmp( $key, 'l_', 2 ); |
||
| 239 | } ); |
||
| 240 | |||
| 241 | if( !$args->isEmpty() || ( $html = $this->getCached( 'header', $uid, $prefixes, $confkey ) ) === null ) |
||
| 242 | { |
||
| 243 | /** client/html/catalog/lists/template-header |
||
| 244 | * Relative path to the HTML header template of the catalog list client. |
||
| 245 | * |
||
| 246 | * The template file contains the HTML code and processing instructions |
||
| 247 | * to generate the HTML code that is inserted into the HTML page header |
||
| 248 | * of the rendered page in the frontend. The configuration string is the |
||
| 249 | * path to the template file relative to the templates directory (usually |
||
| 250 | * in client/html/templates). |
||
| 251 | * |
||
| 252 | * You can overwrite the template file configuration in extensions and |
||
| 253 | * provide alternative templates. These alternative templates should be |
||
| 254 | * named like the default one but with the string "standard" replaced by |
||
| 255 | * an unique name. You may use the name of your project for this. If |
||
| 256 | * you've implemented an alternative client class as well, "standard" |
||
| 257 | * should be replaced by the name of the new class. |
||
| 258 | * |
||
| 259 | * It's also possible to create a specific template for each type, e.g. |
||
| 260 | * for the grid, list or whatever view you want to offer your users. In |
||
| 261 | * that case, you can configure the template by adding "-<type>" to the |
||
| 262 | * configuration key. To configure an alternative list view template for |
||
| 263 | * example, use the key |
||
| 264 | * |
||
| 265 | * client/html/catalog/lists/template-header-list = catalog/lists/header-list.php |
||
| 266 | * |
||
| 267 | * The argument is the relative path to the new template file. The type of |
||
| 268 | * the view is determined by the "l_type" parameter (allowed characters for |
||
| 269 | * the types are a-z and 0-9), which is also stored in the session so users |
||
| 270 | * will keep the view during their visit. The catalog list type subpart |
||
| 271 | * contains the template for switching between list types. |
||
| 272 | * |
||
| 273 | * @param string Relative path to the template creating code for the HTML page head |
||
| 274 | * @since 2014.03 |
||
| 275 | * @category Developer |
||
| 276 | * @see client/html/catalog/lists/template-body |
||
| 277 | * @see client/html/catalog/lists/type/template-body |
||
| 278 | */ |
||
| 279 | $tplconf = 'client/html/catalog/lists/template-header'; |
||
| 280 | $default = 'catalog/lists/header-standard'; |
||
| 281 | |||
| 282 | try |
||
| 283 | { |
||
| 284 | $html = ''; |
||
| 285 | |||
| 286 | if( !isset( $this->view ) ) { |
||
| 287 | $view = $this->view = $this->getObject()->data( $view, $this->tags, $this->expire ); |
||
| 288 | } |
||
| 289 | |||
| 290 | foreach( $this->getSubClients() as $subclient ) { |
||
| 291 | $html .= $subclient->setView( $view )->header( $uid ); |
||
| 292 | } |
||
| 293 | $view->listHeader = $html; |
||
| 294 | |||
| 295 | $html = $view->render( $this->getTemplatePath( $tplconf, $default ) ); |
||
| 296 | |||
| 297 | if( $args->isEmpty() ) { |
||
| 298 | $this->setCached( 'header', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire ); |
||
| 299 | } |
||
| 300 | |||
| 301 | return $html; |
||
| 302 | } |
||
| 303 | catch( \Exception $e ) |
||
| 304 | { |
||
| 305 | $this->logException( $e ); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | else |
||
| 309 | { |
||
| 310 | $html = $this->modifyHeader( $html, $uid ); |
||
| 311 | } |
||
| 312 | |||
| 313 | return $html; |
||
| 314 | } |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns the sub-client given by its name. |
||
| 319 | * |
||
| 320 | * @param string $type Name of the client type |
||
| 321 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 322 | * @return \Aimeos\Client\Html\Iface Sub-client object |
||
| 323 | */ |
||
| 324 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface |
||
| 401 | } |
||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * Processes the input, e.g. store given values. |
||
| 406 | * |
||
| 407 | * A view must be available and this method doesn't generate any output |
||
| 408 | * besides setting view variables if necessary. |
||
| 409 | */ |
||
| 410 | public function init() |
||
| 411 | { |
||
| 412 | $context = $this->getContext(); |
||
| 413 | $view = $this->getView(); |
||
| 414 | |||
| 415 | try |
||
| 416 | { |
||
| 417 | $site = $context->getLocale()->getSiteItem()->getCode(); |
||
| 418 | $params = $this->getClientParams( $view->param() ); |
||
| 419 | |||
| 420 | $catId = $context->getConfig()->get( 'client/html/catalog/lists/catid-default' ); |
||
| 421 | |||
| 422 | if( ( $catId = $view->param( 'f_catid', $catId ) ) ) |
||
| 423 | { |
||
| 424 | $params['f_name'] = $view->param( 'f_name' ); |
||
| 425 | $params['f_catid'] = $catId; |
||
| 426 | } |
||
| 427 | |||
| 428 | $context->getSession()->set( 'aimeos/catalog/lists/params/last/' . $site, $params ); |
||
| 429 | |||
| 430 | parent::init(); |
||
| 431 | } |
||
| 432 | catch( \Aimeos\Client\Html\Exception $e ) |
||
| 433 | { |
||
| 434 | $error = array( $context->translate( 'client', $e->getMessage() ) ); |
||
| 435 | $view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error ); |
||
| 436 | } |
||
| 437 | catch( \Aimeos\Controller\Frontend\Exception $e ) |
||
| 438 | { |
||
| 439 | $error = array( $context->translate( 'controller/frontend', $e->getMessage() ) ); |
||
| 440 | $view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error ); |
||
| 441 | } |
||
| 442 | catch( \Aimeos\MShop\Exception $e ) |
||
| 443 | { |
||
| 444 | $error = array( $context->translate( 'mshop', $e->getMessage() ) ); |
||
| 445 | $view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error ); |
||
| 446 | } |
||
| 447 | catch( \Exception $e ) |
||
| 448 | { |
||
| 449 | $error = array( $context->translate( 'client', 'A non-recoverable error occured' ) ); |
||
| 450 | $view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error ); |
||
| 451 | $this->logException( $e ); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Returns the list of sub-client names configured for the client. |
||
| 458 | * |
||
| 459 | * @return array List of HTML client names |
||
| 460 | */ |
||
| 461 | protected function getSubClientNames() : array |
||
| 464 | } |
||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * Sets the necessary parameter values in the view. |
||
| 469 | * |
||
| 470 | * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output |
||
| 471 | * @param array &$tags Result array for the list of tags that are associated to the output |
||
| 472 | * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry) |
||
| 473 | * @return \Aimeos\MW\View\Iface Modified view object |
||
| 474 | */ |
||
| 475 | public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface |
||
| 775 | } |
||
| 776 | } |
||
| 777 |