Passed
Push — master ( 0f1828...ed6c8e )
by Aimeos
04:20
created

Standard::body()   B

Complexity

Conditions 6
Paths 19

Size

Total Lines 61
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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