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