Passed
Push — master ( 737c73...82bace )
by Aimeos
02:41
created

Standard::getBody()   B

Complexity

Conditions 8
Paths 31

Size

Total Lines 87
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 33
c 1
b 0
f 0
nc 31
nop 1
dl 0
loc 87
rs 8.1475

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Basket\Mass;
12
13
14
/**
15
 * Default implementation of mass order HTML client.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Basket\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/** client/html/basket/mass/standard/subparts
25
	 * List of HTML sub-clients rendered within the mass order section
26
	 *
27
	 * The output of the frontend is composed of the code generated by the HTML
28
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
29
	 * that are responsible for rendering certain sub-parts of the output. The
30
	 * sub-clients can contain HTML clients themselves and therefore a
31
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
32
	 * the output that is placed inside the container of its parent.
33
	 *
34
	 * At first, always the HTML code generated by the parent is printed, then
35
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
36
	 * determines the order of the output of these sub-clients inside the parent
37
	 * container. If the configured list of clients is
38
	 *
39
	 *  array( "subclient1", "subclient2" )
40
	 *
41
	 * you can easily change the order of the output by reordering the subparts:
42
	 *
43
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
44
	 *
45
	 * You can also remove one or more parts if they shouldn't be rendered:
46
	 *
47
	 *  client/html/<clients>/subparts = array( "subclient1" )
48
	 *
49
	 * As the clients only generates structural HTML, the layout defined via CSS
50
	 * should support adding, removing or reordering content by a fluid like
51
	 * design.
52
	 *
53
	 * @param array List of sub-client names
54
	 * @since 2019.10
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/basket/mass/standard/subparts';
58
	private $subPartNames = [];
59
	private $view;
60
61
62
	/**
63
	 * Returns the HTML code for insertion into the body.
64
	 *
65
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
66
	 * @return string HTML code
67
	 */
68
	public function getBody( $uid = '' )
69
	{
70
		$context = $this->getContext();
71
		$site = $context->getLocale()->getSiteId();
72
		$view = $this->getView();
73
74
		/** client/html/basket/mass
75
		 * All parameters defined for the mass order component and its subparts
76
		 *
77
		 * This returns all settings related to the mass order component.
78
		 * Please refer to the single settings for details.
79
		 *
80
		 * @param array Associative list of name/value settings
81
		 * @category Developer
82
		 * @see client/html/basket#mass
83
		 */
84
		$config = $context->getConfig()->get( 'client/html/basket/mass', [] );
85
		$key = $this->getParamHash( [], $uid . $site . ':basket:mass-body', $config );
86
87
		if( ( $html = $this->getBasketCached( $key ) ) === null )
88
		{
89
			try
90
			{
91
				if( !isset( $this->view ) ) {
92
					$view = $this->view = $this->getObject()->addData( $view );
93
				}
94
95
				$output = '';
96
				foreach( $this->getSubClients() as $subclient ) {
97
					$output .= $subclient->setView( $view )->getBody( $uid );
98
				}
99
				$view->massBody = $output;
100
			}
101
			catch( \Aimeos\Client\Html\Exception $e )
102
			{
103
				$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
104
				$view->massErrorList = array_merge($view->get( 'massErrorList', [] ), $error);
105
			}
106
			catch( \Aimeos\Controller\Frontend\Exception $e )
107
			{
108
				$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
109
				$view->massErrorList = array_merge($view->get( 'massErrorList', [] ), $error);
110
			}
111
			catch( \Aimeos\MShop\Exception $e )
112
			{
113
				$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
114
				$view->massErrorList = array_merge($view->get( 'massErrorList', [] ), $error);
115
			}
116
			catch( \Exception $e )
117
			{
118
				$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
119
				$view->massErrorList = array_merge($view->get( 'massErrorList', [] ), $error);
120
				$this->logException( $e );
121
			}
122
123
			/** client/html/basket/mass/standard/template-body
124
			 * Relative path to the HTML body template of the mass order client.
125
			 *
126
			 * The template file contains the HTML code and processing instructions
127
			 * to generate the result shown in the body of the frontend. The
128
			 * configuration string is the path to the template file relative
129
			 * to the templates directory (usually in client/html/templates).
130
			 *
131
			 * You can overwrite the template file configuration in extensions and
132
			 * provide alternative templates. These alternative templates should be
133
			 * named like the default one but with the string "standard" replaced by
134
			 * an unique name. You may use the name of your project for this. If
135
			 * you've implemented an alternative client class as well, "standard"
136
			 * should be replaced by the name of the new class.
137
			 *
138
			 * @param string Relative path to the template creating code for the HTML page body
139
			 * @since 2019.10
140
			 * @category Developer
141
			 * @see client/html/basket/mass/standard/template-header
142
			 */
143
			$tplconf = 'client/html/basket/mass/standard/template-body';
144
			$default = 'basket/mass/body-standard';
145
146
			$html = $view->render( $view->config( $tplconf, $default ) );
147
			$this->setBasketCached( $key, $html );
148
		}
149
		else
150
		{
151
			$html = $this->modifyBody( $html, $uid );
152
		}
153
154
		return $html;
155
	}
156
157
158
	/**
159
	 * Returns the HTML string for insertion into the header.
160
	 *
161
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
162
	 * @return string|null String including HTML tags for the header on error
163
	 */
164
	public function getHeader( $uid = '' )
165
	{
166
		$context = $this->getContext();
167
		$site = $context->getLocale()->getSiteId();
168
		$view = $this->getView();
169
170
		$config = $context->getConfig()->get( 'client/html/basket/mass', [] );
171
		$key = $this->getParamHash( [], $uid . $site . ':basket:mass-header', $config );
172
173
		if( ( $html = $this->getBasketCached( $key ) ) === null )
174
		{
175
			try
176
			{
177
				if( !isset( $this->view ) ) {
178
					$view = $this->view = $this->getObject()->addData( $view );
179
				}
180
181
				$output = '';
182
				foreach( $this->getSubClients() as $subclient ) {
183
					$output .= $subclient->setView( $view )->getHeader( $uid );
184
				}
185
				$view->massHeader = $output;
186
187
				/** client/html/basket/mass/standard/template-header
188
				 * Relative path to the HTML header template of the mass order client.
189
				 *
190
				 * The template file contains the HTML code and processing instructions
191
				 * to generate the HTML code that is inserted into the HTML page header
192
				 * of the rendered page in the frontend. The configuration string is the
193
				 * path to the template file relative to the templates directory (usually
194
				 * in client/html/templates).
195
				 *
196
				 * You can overwrite the template file configuration in extensions and
197
				 * provide alternative templates. These alternative templates should be
198
				 * named like the default one but with the string "standard" replaced by
199
				 * an unique name. You may use the name of your project for this. If
200
				 * you've implemented an alternative client class as well, "standard"
201
				 * should be replaced by the name of the new class.
202
				 *
203
				 * @param string Relative path to the template creating code for the HTML page head
204
				 * @since 2019.10
205
				 * @category Developer
206
				 * @see client/html/basket/mass/standard/template-body
207
				 */
208
				$tplconf = 'client/html/basket/mass/standard/template-header';
209
				$default = 'basket/mass/header-standard';
210
211
				$html = $view->render( $view->config( $tplconf, $default ) );
212
				$this->setBasketCached( $key, $html );
213
			}
214
			catch( \Exception $e )
215
			{
216
				$this->logException( $e );
217
			}
218
		}
219
		else
220
		{
221
			$html = $this->modifyHeader( $html, $uid );
222
		}
223
224
		return $html;
225
	}
226
227
228
	/**
229
	 * Returns the sub-client given by its name.
230
	 *
231
	 * @param string $type Name of the client type
232
	 * @param string|null $name Name of the sub-client (Default if null)
233
	 * @return \Aimeos\Client\Html\Iface Sub-client object
234
	 */
235
	public function getSubClient( $type, $name = null )
236
	{
237
		/** client/html/basket/mass/decorators/excludes
238
		 * Excludes decorators added by the "common" option from the mass order html client
239
		 *
240
		 * Decorators extend the functionality of a class by adding new aspects
241
		 * (e.g. log what is currently done), executing the methods of the underlying
242
		 * class only in certain conditions (e.g. only for logged in users) or
243
		 * modify what is returned to the caller.
244
		 *
245
		 * This option allows you to remove a decorator added via
246
		 * "client/html/common/decorators/default" before they are wrapped
247
		 * around the html client.
248
		 *
249
		 *  client/html/basket/mass/decorators/excludes = array( 'decorator1' )
250
		 *
251
		 * This would remove the decorator named "decorator1" from the list of
252
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
253
		 * "client/html/common/decorators/default" to the html client.
254
		 *
255
		 * @param array List of decorator names
256
		 * @since 2019.10
257
		 * @category Developer
258
		 * @see client/html/common/decorators/default
259
		 * @see client/html/basket/mass/decorators/global
260
		 * @see client/html/basket/mass/decorators/local
261
		 */
262
263
		/** client/html/basket/mass/decorators/global
264
		 * Adds a list of globally available decorators only to the mass order html client
265
		 *
266
		 * Decorators extend the functionality of a class by adding new aspects
267
		 * (e.g. log what is currently done), executing the methods of the underlying
268
		 * class only in certain conditions (e.g. only for logged in users) or
269
		 * modify what is returned to the caller.
270
		 *
271
		 * This option allows you to wrap global decorators
272
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
273
		 *
274
		 *  client/html/basket/mass/decorators/global = array( 'decorator1' )
275
		 *
276
		 * This would add the decorator named "decorator1" defined by
277
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
278
		 *
279
		 * @param array List of decorator names
280
		 * @since 2019.10
281
		 * @category Developer
282
		 * @see client/html/common/decorators/default
283
		 * @see client/html/basket/mass/decorators/excludes
284
		 * @see client/html/basket/mass/decorators/local
285
		 */
286
287
		/** client/html/basket/mass/decorators/local
288
		 * Adds a list of local decorators only to the mass order html client
289
		 *
290
		 * Decorators extend the functionality of a class by adding new aspects
291
		 * (e.g. log what is currently done), executing the methods of the underlying
292
		 * class only in certain conditions (e.g. only for logged in users) or
293
		 * modify what is returned to the caller.
294
		 *
295
		 * This option allows you to wrap local decorators
296
		 * ("\Aimeos\Client\Html\Basket\Decorator\*") around the html client.
297
		 *
298
		 *  client/html/basket/mass/decorators/local = array( 'decorator2' )
299
		 *
300
		 * This would add the decorator named "decorator2" defined by
301
		 * "\Aimeos\Client\Html\Basket\Decorator\Decorator2" only to the html client.
302
		 *
303
		 * @param array List of decorator names
304
		 * @since 2019.10
305
		 * @category Developer
306
		 * @see client/html/common/decorators/default
307
		 * @see client/html/basket/mass/decorators/excludes
308
		 * @see client/html/basket/mass/decorators/global
309
		 */
310
311
		return $this->createSubClient( 'basket/mass/' . $type, $name );
312
	}
313
314
315
	/**
316
	 * Returns the list of sub-client names configured for the client.
317
	 *
318
	 * @return array List of HTML client names
319
	 */
320
	protected function getSubClientNames()
321
	{
322
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
323
	}
324
}
325