Completed
Push — master ( b9f1e5...5ff555 )
by Aimeos
11:02
created

Standard::getSubClient()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 8.48
c 0
b 0
f 0
cc 1
nc 1
nop 2

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