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