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