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