Passed
Branch master (4f99e3)
by Aimeos
04:48
created

Standard::getSubClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 77
rs 10
c 0
b 0
f 0

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