Total Complexity | 57 |
Total Lines | 596 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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 |
||
24 | class Standard |
||
25 | extends \Aimeos\Client\Html\Basket\Base |
||
26 | implements \Aimeos\Client\Html\Common\Client\Factory\Iface |
||
27 | { |
||
28 | /** client/html/basket/subparts |
||
29 | * List of HTML sub-clients rendered within the basket standard section |
||
30 | * |
||
31 | * The output of the frontend is composed of the code generated by the HTML |
||
32 | * clients. Each HTML client can consist of serveral (or none) sub-clients |
||
33 | * that are responsible for rendering certain sub-parts of the output. The |
||
34 | * sub-clients can contain HTML clients themselves and therefore a |
||
35 | * hierarchical tree of HTML clients is composed. Each HTML client creates |
||
36 | * the output that is placed inside the container of its parent. |
||
37 | * |
||
38 | * At first, always the HTML code generated by the parent is printed, then |
||
39 | * the HTML code of its sub-clients. The order of the HTML sub-clients |
||
40 | * determines the order of the output of these sub-clients inside the parent |
||
41 | * container. If the configured list of clients is |
||
42 | * |
||
43 | * array( "subclient1", "subclient2" ) |
||
44 | * |
||
45 | * you can easily change the order of the output by reordering the subparts: |
||
46 | * |
||
47 | * client/html/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
48 | * |
||
49 | * You can also remove one or more parts if they shouldn't be rendered: |
||
50 | * |
||
51 | * client/html/<clients>/subparts = array( "subclient1" ) |
||
52 | * |
||
53 | * As the clients only generates structural HTML, the layout defined via CSS |
||
54 | * should support adding, removing or reordering content by a fluid like |
||
55 | * design. |
||
56 | * |
||
57 | * @param array List of sub-client names |
||
58 | * @since 2014.03 |
||
59 | * @category Developer |
||
60 | */ |
||
61 | private $subPartPath = 'client/html/basket/subparts'; |
||
62 | private $subPartNames = []; |
||
63 | private $view; |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Returns the HTML code for insertion into the body. |
||
68 | * |
||
69 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
70 | * @return string HTML code |
||
71 | */ |
||
72 | public function getBody( string $uid = '' ) : string |
||
73 | { |
||
74 | $context = $this->getContext(); |
||
75 | $view = $this->getView(); |
||
76 | |||
77 | try |
||
78 | { |
||
79 | $view = $this->view = $this->view ?? $this->getObject()->addData( $view ); |
||
80 | |||
81 | $html = ''; |
||
82 | foreach( $this->getSubClients() as $subclient ) { |
||
83 | $html .= $subclient->setView( $view )->getBody( $uid ); |
||
84 | } |
||
85 | $view->standardBody = $html; |
||
86 | } |
||
87 | catch( \Aimeos\Client\Html\Exception $e ) |
||
88 | { |
||
89 | $error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) ); |
||
90 | $view->standardErrorList = array_merge( $view->get( 'standardErrorList', [] ), $error ); |
||
91 | } |
||
92 | catch( \Aimeos\Controller\Frontend\Exception $e ) |
||
93 | { |
||
94 | $error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) ); |
||
95 | $view->standardErrorList = array_merge( $view->get( 'standardErrorList', [] ), $error ); |
||
96 | } |
||
97 | catch( \Aimeos\MShop\Exception $e ) |
||
98 | { |
||
99 | $error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
100 | $view->standardErrorList = array_merge( $view->get( 'standardErrorList', [] ), $error ); |
||
101 | } |
||
102 | catch( \Exception $e ) |
||
103 | { |
||
104 | $error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) ); |
||
105 | $view->standardErrorList = array_merge( $view->get( 'standardErrorList', [] ), $error ); |
||
106 | $this->logException( $e ); |
||
107 | } |
||
108 | |||
109 | /** client/html/basket/template-body |
||
110 | * Relative path to the HTML body template of the basket standard client. |
||
111 | * |
||
112 | * The template file contains the HTML code and processing instructions |
||
113 | * to generate the result shown in the body of the frontend. The |
||
114 | * configuration string is the path to the template file relative |
||
115 | * to the templates directory (usually in client/html/templates). |
||
116 | * |
||
117 | * You can overwrite the template file configuration in extensions and |
||
118 | * provide alternative templates. These alternative templates should be |
||
119 | * named like the default one but with the string "standard" replaced by |
||
120 | * an unique name. You may use the name of your project for this. If |
||
121 | * you've implemented an alternative client class as well, "standard" |
||
122 | * should be replaced by the name of the new class. |
||
123 | * |
||
124 | * @param string Relative path to the template creating code for the HTML page body |
||
125 | * @since 2014.03 |
||
126 | * @category Developer |
||
127 | * @see client/html/basket/template-header |
||
128 | */ |
||
129 | $tplconf = 'client/html/basket/template-body'; |
||
130 | $default = 'basket/standard/body-standard'; |
||
131 | |||
132 | return $view->render( $view->config( $tplconf, $default ) ); |
||
133 | } |
||
134 | |||
135 | |||
136 | /** |
||
137 | * Returns the HTML string for insertion into the header. |
||
138 | * |
||
139 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
140 | * @return string|null String including HTML tags for the header on error |
||
141 | */ |
||
142 | public function getHeader( string $uid = '' ) : ?string |
||
143 | { |
||
144 | $view = $this->getView(); |
||
145 | |||
146 | try |
||
147 | { |
||
148 | $view = $this->view = $this->view ?? $this->getObject()->addData( $view ); |
||
149 | |||
150 | $html = ''; |
||
151 | foreach( $this->getSubClients() as $subclient ) { |
||
152 | $html .= $subclient->setView( $view )->getHeader( $uid ); |
||
153 | } |
||
154 | $view->standardHeader = $html; |
||
155 | } |
||
156 | catch( \Exception $e ) |
||
157 | { |
||
158 | $this->logException( $e ); |
||
159 | } |
||
160 | |||
161 | /** client/html/basket/template-header |
||
162 | * Relative path to the HTML header template of the basket standard client. |
||
163 | * |
||
164 | * The template file contains the HTML code and processing instructions |
||
165 | * to generate the HTML code that is inserted into the HTML page header |
||
166 | * of the rendered page in the frontend. The configuration string is the |
||
167 | * path to the template file relative to the templates directory (usually |
||
168 | * in client/html/templates). |
||
169 | * |
||
170 | * You can overwrite the template file configuration in extensions and |
||
171 | * provide alternative templates. These alternative templates should be |
||
172 | * named like the default one but with the string "standard" replaced by |
||
173 | * an unique name. You may use the name of your project for this. If |
||
174 | * you've implemented an alternative client class as well, "standard" |
||
175 | * should be replaced by the name of the new class. |
||
176 | * |
||
177 | * @param string Relative path to the template creating code for the HTML page head |
||
178 | * @since 2014.03 |
||
179 | * @category Developer |
||
180 | * @see client/html/basket/template-body |
||
181 | */ |
||
182 | $tplconf = 'client/html/basket/template-header'; |
||
183 | $default = 'basket/standard/header-standard'; |
||
184 | |||
185 | return $view->render( $view->config( $tplconf, $default ) ); |
||
186 | } |
||
187 | |||
188 | |||
189 | /** |
||
190 | * Returns the sub-client given by its name. |
||
191 | * |
||
192 | * @param string $type Name of the client type |
||
193 | * @param string|null $name Name of the sub-client (Default if null) |
||
194 | * @return \Aimeos\Client\Html\Iface Sub-client object |
||
195 | */ |
||
196 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface |
||
273 | } |
||
274 | |||
275 | |||
276 | /** |
||
277 | * Sets the necessary parameter values in the view. |
||
278 | * |
||
279 | * A view must be available and this method doesn't generate any output |
||
280 | * besides setting view variables if necessary. |
||
281 | */ |
||
282 | public function process() |
||
362 | } |
||
363 | |||
364 | |||
365 | /** |
||
366 | * Returns the list of sub-client names configured for the client. |
||
367 | * |
||
368 | * @return array List of HTML client names |
||
369 | */ |
||
370 | protected function getSubClientNames() : array |
||
373 | } |
||
374 | |||
375 | |||
376 | /** |
||
377 | * Sets the necessary parameter values in the view. |
||
378 | * |
||
379 | * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output |
||
380 | * @param array &$tags Result array for the list of tags that are associated to the output |
||
381 | * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry) |
||
382 | * @return \Aimeos\MW\View\Iface Modified view object |
||
383 | */ |
||
384 | public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface |
||
419 | } |
||
420 | |||
421 | |||
422 | /** |
||
423 | * Adds the coupon specified by the view parameters from the basket. |
||
424 | * |
||
425 | * @param \Aimeos\MW\View\Iface $view View object |
||
426 | */ |
||
427 | protected function addCoupon( \Aimeos\MW\View\Iface $view ) |
||
453 | } |
||
454 | } |
||
455 | |||
456 | |||
457 | /** |
||
458 | * Adds the products specified by the view parameters to the basket. |
||
459 | * |
||
460 | * @param \Aimeos\MW\View\Iface $view View object |
||
461 | */ |
||
462 | protected function addProducts( \Aimeos\MW\View\Iface $view ) |
||
463 | { |
||
464 | $attrIds[] = \Aimeos\MShop::create( $this->getContext(), 'attribute' )->find( 'custom', [], 'product', 'upload' )->getId(); |
||
465 | |||
466 | $attrIds[] = \Aimeos\MShop::create( $this->getContext(), 'attribute' )->find( 'file', [], 'product', 'upload' )->getId(); |
||
467 | |||
468 | $fs = $this->getContext()->fs( 'fs' ); |
||
469 | |||
470 | if( !$fs->has('basket-upload' ) ) { |
||
471 | $fs->mkdir( 'basket-upload' ); |
||
472 | } |
||
473 | |||
474 | $context = $this->getContext(); |
||
475 | $domains = ['attribute', 'media', 'price', 'product', 'text', 'custom']; |
||
476 | |||
477 | $basketCntl = \Aimeos\Controller\Frontend::create( $context, 'basket' ); |
||
478 | $productCntl = \Aimeos\Controller\Frontend::create( $context, 'product' )->uses( $domains ); |
||
479 | |||
480 | if( ( $prodid = $view->param( 'b_prodid', '' ) ) !== '' && $view->param( 'b_quantity', 0 ) > 0 ) |
||
481 | { |
||
482 | $basketCntl->addProduct( |
||
483 | $productCntl->get( $prodid ), |
||
484 | (float) $view->param( 'b_quantity', 0 ), |
||
485 | (array) $view->param( 'b_attrvarid', [] ), |
||
486 | $this->getAttributeMap( $view->param( 'b_attrconfid', [] ) ), |
||
487 | array_filter( (array) $view->param( 'b_attrcustid', [] ) ), |
||
488 | (string) $view->param( 'b_stocktype', 'default' ), |
||
489 | (string) $view->param( 'b_supplier', '' ), |
||
490 | $view->param( 'b_siteid' ) |
||
491 | ); |
||
492 | } |
||
493 | else |
||
494 | { |
||
495 | $list = []; |
||
496 | $entries = (array) $view->param( 'b_prod', [] ); |
||
497 | |||
498 | for($i = 0; $i < count($entries); ++$i) { |
||
499 | $paths = []; |
||
500 | foreach ($attrIds as $attrId) { |
||
501 | if (isset($entries[$i]['attrcustid'][$attrId]) && is_array($entries[$i]['attrcustid'][$attrId])) { |
||
502 | /** @var UploadedFile $file */ |
||
503 | foreach ($entries[$i]['attrcustid'][$attrId] as $file) { |
||
504 | $filepath = 'basket-upload/' . md5($file->getFilename() . microtime(true)) . '.' . $file->extension(); |
||
505 | try { |
||
506 | $stream = fopen($file->getRealPath(), 'r+'); |
||
507 | $fs->writes($filepath, $stream); |
||
508 | fclose($stream); |
||
509 | } catch (\Exception $ex) { |
||
510 | Log::error($ex->getMessage()); |
||
511 | } |
||
512 | $paths[] = $filepath; |
||
513 | } |
||
514 | $entries[$i]['attrcustid'][$attrId] = $paths; |
||
515 | } |
||
516 | if (isset($entries[$i]['prodid'])) { |
||
517 | $list[] = $entries[$i]['prodid']; |
||
518 | } |
||
519 | } |
||
520 | } |
||
521 | |||
522 | foreach( $entries as $values ) |
||
523 | { |
||
524 | if( ( $values['prodid'] ?? null ) && ( $values['quantity'] ?? 0 ) > 0 ) |
||
525 | { |
||
526 | $basketCntl->addProduct( $productCntl->get( $values['prodid'] ), |
||
527 | (float) ( $values['quantity'] ?? 0 ), |
||
528 | array_filter( (array) ( $values['attrvarid'] ?? [] ) ), |
||
529 | $this->getAttributeMap( (array) ( $values['attrconfid'] ?? [] ) ), |
||
530 | array_filter( (array) ( $values['attrcustid'] ?? [] ) ), |
||
531 | (string) ( $values['stocktype'] ?? 'default' ), |
||
532 | (string) ( $values['supplier'] ?? '' ), |
||
533 | $values['siteid'] ?? null |
||
534 | ); |
||
535 | } |
||
536 | } |
||
537 | } |
||
538 | |||
539 | $this->clearCached(); |
||
540 | } |
||
541 | |||
542 | |||
543 | /** |
||
544 | * Removes the coupon specified by the view parameters from the basket. |
||
545 | * |
||
546 | * @param \Aimeos\MW\View\Iface $view View object |
||
547 | */ |
||
548 | protected function deleteCoupon( \Aimeos\MW\View\Iface $view ) |
||
549 | { |
||
550 | if( ( $coupon = $view->param( 'b_coupon' ) ) != '' ) |
||
551 | { |
||
552 | \Aimeos\Controller\Frontend::create( $this->getContext(), 'basket' )->deleteCoupon( $coupon ); |
||
553 | $this->clearCached(); |
||
554 | } |
||
555 | } |
||
556 | |||
557 | |||
558 | /** |
||
559 | * Removes the products specified by the view parameters from the basket. |
||
560 | * |
||
561 | * @param \Aimeos\MW\View\Iface $view View object |
||
562 | */ |
||
563 | protected function deleteProducts( \Aimeos\MW\View\Iface $view ) |
||
564 | { |
||
565 | $controller = \Aimeos\Controller\Frontend::create( $this->getContext(), 'basket' ); |
||
566 | $products = (array) $view->param( 'b_position', [] ); |
||
567 | |||
568 | foreach( $products as $position ) { |
||
569 | $controller->deleteProduct( $position ); |
||
570 | } |
||
571 | |||
572 | $this->clearCached(); |
||
573 | } |
||
574 | |||
575 | |||
576 | protected function getAttributeMap( array $values ) |
||
577 | { |
||
578 | $list = []; |
||
579 | $confIds = ( isset( $values['id'] ) ? array_filter( (array) $values['id'] ) : [] ); |
||
580 | $confQty = ( isset( $values['qty'] ) ? array_filter( (array) $values['qty'] ) : [] ); |
||
581 | |||
582 | foreach( $confIds as $idx => $id ) |
||
583 | { |
||
584 | if( isset( $confQty[$idx] ) && $confQty[$idx] > 0 ) { |
||
585 | $list[$id] = $confQty[$idx]; |
||
586 | } |
||
587 | } |
||
588 | |||
589 | return $list; |
||
590 | } |
||
591 | |||
592 | |||
593 | /** |
||
594 | * Edits the products specified by the view parameters to the basket. |
||
595 | * |
||
596 | * @param \Aimeos\MW\View\Iface $view View object |
||
597 | */ |
||
598 | protected function updateProducts( \Aimeos\MW\View\Iface $view ) |
||
620 | } |
||
621 | } |
||
622 |