Passed
Push — master ( 928310...983cfc )
by Aimeos
04:45
created

Standard::getBody()   B

Complexity

Conditions 7
Paths 34

Size

Total Lines 63
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 25
c 1
b 0
f 0
nc 34
nop 1
dl 0
loc 63
rs 8.5866

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), 2016-2020
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Account\Review;
12
13
14
/**
15
 * Default implementation of account review HTML client.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
22
	implements \Aimeos\Client\Html\Iface
23
{
24
	/** client/html/account/review/standard/subparts
25
	 * List of HTML sub-clients rendered within the account review 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 2020.10
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/account/review/standard/subparts';
58
59
	/** client/html/account/review/address/name
60
	 * Name of the address part used by the account review client implementation
61
	 *
62
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Account\Review\Address\Myname".
63
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
64
	 *
65
	 * @param string Last part of the client class name
66
	 * @since 2020.10
67
	 * @category Developer
68
	 */
69
	private $subPartNames = ['todo'];
70
	private $view;
71
72
73
	/**
74
	 * Returns the HTML code for insertion into the body.
75
	 *
76
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
77
	 * @return string HTML code
78
	 */
79
	public function getBody( string $uid = '' ) : string
80
	{
81
		$context = $this->getContext();
82
		$view = $this->getView();
83
84
		try
85
		{
86
			if( !isset( $this->view ) ) {
87
				$view = $this->view = $this->getObject()->addData( $view );
88
			}
89
90
			$html = '';
91
			foreach( $this->getSubClients() as $subclient ) {
92
				$html .= $subclient->setView( $view )->getBody( $uid );
93
			}
94
			$view->reviewBody = $html;
95
		}
96
		catch( \Aimeos\Client\Html\Exception $e )
97
		{
98
			$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
99
			$view->reviewErrorList = array_merge( $view->get( 'reviewErrorList', [] ), $error );
100
		}
101
		catch( \Aimeos\Controller\Frontend\Exception $e )
102
		{
103
			$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
104
			$view->reviewErrorList = array_merge( $view->get( 'reviewErrorList', [] ), $error );
105
		}
106
		catch( \Aimeos\MShop\Exception $e )
107
		{
108
			$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
109
			$view->reviewErrorList = array_merge( $view->get( 'reviewErrorList', [] ), $error );
110
		}
111
		catch( \Exception $e )
112
		{
113
			$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
114
			$view->reviewErrorList = array_merge( $view->get( 'reviewErrorList', [] ), $error );
115
			$this->logException( $e );
116
		}
117
118
		/** client/html/account/review/standard/template-body
119
		 * Relative path to the HTML body template of the account review client.
120
		 *
121
		 * The template file contains the HTML code and processing instructions
122
		 * to generate the result shown in the body of the frontend. The
123
		 * configuration string is the path to the template file relative
124
		 * to the templates directory (usually in client/html/templates).
125
		 *
126
		 * You can overwrite the template file configuration in extensions and
127
		 * provide alternative templates. These alternative templates should be
128
		 * named like the default one but with the string "standard" replaced by
129
		 * an unique name. You may use the name of your project for this. If
130
		 * you've implemented an alternative client class as well, "standard"
131
		 * should be replaced by the name of the new class.
132
		 *
133
		 * @param string Relative path to the template creating code for the HTML page body
134
		 * @since 2020.10
135
		 * @category Developer
136
		 * @see client/html/account/review/standard/template-header
137
		 */
138
		$tplconf = 'client/html/account/review/standard/template-body';
139
		$default = 'account/review/body-standard';
140
141
		return $view->render( $view->config( $tplconf, $default ) );
142
	}
143
144
145
	/**
146
	 * Returns the HTML string for insertion into the header.
147
	 *
148
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
149
	 * @return string|null String including HTML tags for the header on error
150
	 */
151
	public function getHeader( string $uid = '' ) : ?string
152
	{
153
		$view = $this->getView();
154
155
		try
156
		{
157
			if( !isset( $this->view ) ) {
158
				$view = $this->view = $this->getObject()->addData( $view );
159
			}
160
161
			$html = '';
162
			foreach( $this->getSubClients() as $subclient ) {
163
				$html .= $subclient->setView( $view )->getHeader( $uid );
164
			}
165
			$view->reviewHeader = $html;
166
167
			/** client/html/account/review/standard/template-header
168
			 * Relative path to the HTML header template of the account review client.
169
			 *
170
			 * The template file contains the HTML code and processing instructions
171
			 * to generate the HTML code that is inserted into the HTML page header
172
			 * of the rendered page in the frontend. The configuration string is the
173
			 * path to the template file relative to the templates directory (usually
174
			 * in client/html/templates).
175
			 *
176
			 * You can overwrite the template file configuration in extensions and
177
			 * provide alternative templates. These alternative templates should be
178
			 * named like the default one but with the string "standard" replaced by
179
			 * an unique name. You may use the name of your project for this. If
180
			 * you've implemented an alternative client class as well, "standard"
181
			 * should be replaced by the name of the new class.
182
			 *
183
			 * @param string Relative path to the template creating code for the HTML page head
184
			 * @since 2020.10
185
			 * @category Developer
186
			 * @see client/html/account/review/standard/template-body
187
			 */
188
			$tplconf = 'client/html/account/review/standard/template-header';
189
			$default = 'account/review/header-standard';
190
191
			return $view->render( $view->config( $tplconf, $default ) );
192
		}
193
		catch( \Exception $e )
194
		{
195
			$this->logException( $e );
196
		}
197
198
		return null;
199
	}
200
201
202
	/**
203
	 * Returns the sub-client given by its name.
204
	 *
205
	 * @param string $type Name of the client type
206
	 * @param string|null $name Name of the sub-client (Default if null)
207
	 * @return \Aimeos\Client\Html\Iface Sub-client object
208
	 */
209
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
210
	{
211
		/** client/html/account/review/decorators/excludes
212
		 * Excludes decorators added by the "common" option from the account review html client
213
		 *
214
		 * Decorators extend the functionality of a class by adding new aspects
215
		 * (e.g. log what is currently done), executing the methods of the underlying
216
		 * class only in certain conditions (e.g. only for logged in users) or
217
		 * modify what is returned to the caller.
218
		 *
219
		 * This option allows you to remove a decorator added via
220
		 * "client/html/common/decorators/default" before they are wrapped
221
		 * around the html client.
222
		 *
223
		 *  client/html/account/review/decorators/excludes = array( 'decorator1' )
224
		 *
225
		 * This would remove the decorator named "decorator1" from the list of
226
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
227
		 * "client/html/common/decorators/default" to the html client.
228
		 *
229
		 * @param array List of decorator names
230
		 * @since 2020.10
231
		 * @category Developer
232
		 * @see client/html/common/decorators/default
233
		 * @see client/html/account/review/decorators/global
234
		 * @see client/html/account/review/decorators/local
235
		 */
236
237
		/** client/html/account/review/decorators/global
238
		 * Adds a list of globally available decorators only to the account review 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 wrap global decorators
246
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
247
		 *
248
		 *  client/html/account/review/decorators/global = array( 'decorator1' )
249
		 *
250
		 * This would add the decorator named "decorator1" defined by
251
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
252
		 *
253
		 * @param array List of decorator names
254
		 * @since 2020.10
255
		 * @category Developer
256
		 * @see client/html/common/decorators/default
257
		 * @see client/html/account/review/decorators/excludes
258
		 * @see client/html/account/review/decorators/local
259
		 */
260
261
		/** client/html/account/review/decorators/local
262
		 * Adds a list of local decorators only to the account review html client
263
		 *
264
		 * Decorators extend the functionality of a class by adding new aspects
265
		 * (e.g. log what is currently done), executing the methods of the underlying
266
		 * class only in certain conditions (e.g. only for logged in users) or
267
		 * modify what is returned to the caller.
268
		 *
269
		 * This option allows you to wrap local decorators
270
		 * ("\Aimeos\Client\Html\Account\Decorator\*") around the html client.
271
		 *
272
		 *  client/html/account/review/decorators/local = array( 'decorator2' )
273
		 *
274
		 * This would add the decorator named "decorator2" defined by
275
		 * "\Aimeos\Client\Html\Account\Decorator\Decorator2" only to the html client.
276
		 *
277
		 * @param array List of decorator names
278
		 * @since 2020.10
279
		 * @category Developer
280
		 * @see client/html/common/decorators/default
281
		 * @see client/html/account/review/decorators/excludes
282
		 * @see client/html/account/review/decorators/global
283
		 */
284
285
		return $this->createSubClient( 'account/review/' . $type, $name );
286
	}
287
288
289
	/**
290
	 * Processes the input, e.g. store given values.
291
	 *
292
	 * A view must be available and this method doesn't generate any output
293
	 * besides setting view variables if necessary.
294
	 */
295
	public function process()
296
	{
297
		$context = $this->getContext();
298
		$view = $this->getView();
299
300
		try
301
		{
302
			parent::process();
303
		}
304
		catch( \Aimeos\MShop\Exception $e )
305
		{
306
			$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
307
			$view->reviewErrorList = array_merge( $view->get( 'reviewErrorList', [] ), $error );
308
		}
309
		catch( \Aimeos\Controller\Frontend\Exception $e )
310
		{
311
			$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
312
			$view->reviewErrorList = array_merge( $view->get( 'reviewErrorList', [] ), $error );
313
		}
314
		catch( \Aimeos\Client\Html\Exception $e )
315
		{
316
			$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
317
			$view->reviewErrorList = array_merge( $view->get( 'reviewErrorList', [] ), $error );
318
		}
319
		catch( \Exception $e )
320
		{
321
			$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
322
			$view->reviewErrorList = array_merge( $view->get( 'reviewErrorList', [] ), $error );
323
			$this->logException( $e );
324
		}
325
	}
326
327
328
	/**
329
	 * Returns the list of sub-client names configured for the client.
330
	 *
331
	 * @return array List of HTML client names
332
	 */
333
	protected function getSubClientNames() : array
334
	{
335
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
336
	}
337
}
338