Passed
Push — master ( 0e4bb3...d970c3 )
by Aimeos
03:19
created

Standard::addData()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 89
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 22
c 1
b 0
f 0
nc 16
nop 3
dl 0
loc 89
rs 8.4444

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), 2020
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Catalog\Home;
12
13
/**
14
 * Implementation of catalog home section HTML clients for a configurable list of homes.
15
 *
16
 * @package Client
17
 * @subpackage Html
18
 */
19
class Standard
20
	extends \Aimeos\Client\Html\Catalog\Base
21
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
22
{
23
	/** client/html/catalog/home/standard/subparts
24
	 * List of HTML sub-clients rendered within the catalog home section
25
	 *
26
	 * The output of the frontend is composed of the code generated by the HTML
27
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
28
	 * that are responsible for rendering certain sub-parts of the output. The
29
	 * sub-clients can contain HTML clients themselves and therefore a
30
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
31
	 * the output that is placed inside the container of its parent.
32
	 *
33
	 * At first, always the HTML code generated by the parent is printed, then
34
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
35
	 * determines the order of the output of these sub-clients inside the parent
36
	 * container. If the configured list of clients is
37
	 *
38
	 *  array( "subclient1", "subclient2" )
39
	 *
40
	 * you can easily change the order of the output by reordering the subparts:
41
	 *
42
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
43
	 *
44
	 * You can also remove one or more parts if they shouldn't be rendered:
45
	 *
46
	 *  client/html/<clients>/subparts = array( "subclient1" )
47
	 *
48
	 * As the clients only generates structural HTML, the layout defined via CSS
49
	 * should support adding, removing or reordering content by a fluid like
50
	 * design.
51
	 *
52
	 * @param array List of sub-client names
53
	 * @since 2020.10
54
	 * @category Developer
55
	 */
56
	private $subPartPath = 'client/html/catalog/home/standard/subparts';
57
	private $subPartNames = [];
58
59
	private $tags = [];
60
	private $expire;
61
	private $view;
62
63
64
	/**
65
	 * Returns the HTML code for insertion into the body.
66
	 *
67
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
68
	 * @return string HTML code
69
	 */
70
	public function getBody( string $uid = '' ) : string
71
	{
72
		$context = $this->getContext();
73
74
		/** client/html/catalog/home/cache
75
		 * Enables or disables caching only for the catalog home component
76
		 *
77
		 * Disable caching for components can be useful if you would have too much
78
		 * entries to cache or if the component contains non-cacheable parts that
79
		 * can't be replaced using the modifyBody() and modifyHeader() methods.
80
		 *
81
		 * @param boolean True to enable caching, false to disable
82
		 * @category Developer
83
		 * @category User
84
		 * @see client/html/catalog/detail/cache
85
		 * @see client/html/catalog/filter/cache
86
		 * @see client/html/catalog/stage/cache
87
		 * @see client/html/catalog/list/cache
88
		 */
89
90
		/** client/html/catalog/home
91
		 * All parameters defined for the catalog home component and its subparts
92
		 *
93
		 * Please refer to the single settings for details.
94
		 *
95
		 * @param array Associative list of name/value settings
96
		 * @category Developer
97
		 * @see client/html/catalog#home
98
		 */
99
		$confkey = 'client/html/catalog/home';
100
101
		if( ( $html = $this->getCached( 'body', $uid, [], $confkey ) ) === null )
102
		{
103
			$view = $this->getView();
104
			$config = $this->getContext()->getConfig();
105
106
			/** client/html/catalog/home/standard/template-body
107
			 * Relative path to the HTML body template of the catalog home client.
108
			 *
109
			 * The template file contains the HTML code and processing instructions
110
			 * to generate the result shown in the body of the frontend. The
111
			 * configuration string is the path to the template file relative
112
			 * to the templates directory (usually in client/html/templates).
113
			 *
114
			 * You can overwrite the template file configuration in extensions and
115
			 * provide alternative templates. These alternative templates should be
116
			 * named like the default one but with the string "standard" replaced by
117
			 * an unique name. You may use the name of your project for this. If
118
			 * you've implemented an alternative client class as well, "standard"
119
			 * should be replaced by the name of the new class.
120
			 *
121
			 * @param string Relative path to the template creating code for the HTML page body
122
			 * @since 2020.10
123
			 * @category Developer
124
			 * @see client/html/catalog/home/standard/template-header
125
			 */
126
			$tplconf = 'client/html/catalog/home/standard/template-body';
127
			$default = 'catalog/home/body-standard';
128
129
			try
130
			{
131
				if( !isset( $this->view ) ) {
132
					$view = $this->view = $this->getObject()->addData( $view, $this->tags, $this->expire );
133
				}
134
135
				$html = '';
136
				foreach( $this->getSubClients() as $subclient ) {
137
					$html .= $subclient->setView( $view )->getBody( $uid );
138
				}
139
				$view->listBody = $html;
140
141
				$html = $view->render( $config->get( $tplconf, $default ) );
142
				$this->setCached( 'body', $uid, [], $confkey, $html, $this->tags, $this->expire );
143
144
				return $html;
145
			}
146
			catch( \Aimeos\Client\Html\Exception $e )
147
			{
148
				$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
149
				$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
150
			}
151
			catch( \Aimeos\Controller\Frontend\Exception $e )
152
			{
153
				$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
154
				$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
155
			}
156
			catch( \Aimeos\MShop\Exception $e )
157
			{
158
				$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
159
				$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
160
			}
161
			catch( \Exception $e )
162
			{
163
				$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
164
				$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
165
				$this->logException( $e );
166
			}
167
168
			$html = $view->render( $config->get( $tplconf, $default ) );
169
		}
170
		else
171
		{
172
			$html = $this->modifyBody( $html, $uid );
173
		}
174
175
		return $html;
176
	}
177
178
179
	/**
180
	 * Returns the HTML string for insertion into the header.
181
	 *
182
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
183
	 * @return string|null String including HTML tags for the header on error
184
	 */
185
	public function getHeader( string $uid = '' ) : ?string
186
	{
187
		$confkey = 'client/html/catalog/home';
188
189
		if( ( $html = $this->getCached( 'header', $uid, [], $confkey ) ) === null )
190
		{
191
			$view = $this->getView();
192
			$config = $this->getContext()->getConfig();
193
194
			/** client/html/catalog/home/standard/template-header
195
			 * Relative path to the HTML header template of the catalog home client.
196
			 *
197
			 * The template file contains the HTML code and processing instructions
198
			 * to generate the HTML code that is inserted into the HTML page header
199
			 * of the rendered page in the frontend. The configuration string is the
200
			 * path to the template file relative to the templates directory (usually
201
			 * in client/html/templates).
202
			 *
203
			 * You can overwrite the template file configuration in extensions and
204
			 * provide alternative templates. These alternative templates should be
205
			 * named like the default one but with the string "standard" replaced by
206
			 * an unique name. You may use the name of your project for this. If
207
			 * you've implemented an alternative client class as well, "standard"
208
			 * should be replaced by the name of the new class.
209
			 *
210
			 * @param string Relative path to the template creating code for the HTML page head
211
			 * @since 2020.10
212
			 * @category Developer
213
			 * @see client/html/catalog/home/standard/template-body
214
			 */
215
			$tplconf = 'client/html/catalog/home/standard/template-header';
216
			$default = 'catalog/home/header-standard';
217
218
			try
219
			{
220
				if( !isset( $this->view ) ) {
221
					$view = $this->view = $this->getObject()->addData( $view, $this->tags, $this->expire );
222
				}
223
224
				$html = '';
225
				foreach( $this->getSubClients() as $subclient ) {
226
					$html .= $subclient->setView( $view )->getHeader( $uid );
227
				}
228
				$view->listHeader = $html;
229
230
				$html = $view->render( $config->get( $tplconf, $default ) );
231
				$this->setCached( 'header', $uid, [], $confkey, $html, $this->tags, $this->expire );
232
233
				return $html;
234
			}
235
			catch( \Exception $e )
236
			{
237
				$this->logException( $e );
238
			}
239
		}
240
		else
241
		{
242
			$html = $this->modifyHeader( $html, $uid );
243
		}
244
245
		return $html;
246
	}
247
248
249
	/**
250
	 * Returns the sub-client given by its name.
251
	 *
252
	 * @param string $type Name of the client type
253
	 * @param string|null $name Name of the sub-client (Default if null)
254
	 * @return \Aimeos\Client\Html\Iface Sub-client object
255
	 */
256
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
257
	{
258
		/** client/html/catalog/home/decorators/excludes
259
		 * Excludes decorators added by the "common" option from the catalog home html client
260
		 *
261
		 * Decorators extend the functionality of a class by adding new aspects
262
		 * (e.g. log what is currently done), executing the methods of the underlying
263
		 * class only in certain conditions (e.g. only for logged in users) or
264
		 * modify what is returned to the caller.
265
		 *
266
		 * This option allows you to remove a decorator added via
267
		 * "client/html/common/decorators/default" before they are wrapped
268
		 * around the html client.
269
		 *
270
		 *  client/html/catalog/home/decorators/excludes = array( 'decorator1' )
271
		 *
272
		 * This would remove the decorator named "decorator1" from the list of
273
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
274
		 * "client/html/common/decorators/default" to the html client.
275
		 *
276
		 * @param array List of decorator names
277
		 * @since 2020.10
278
		 * @category Developer
279
		 * @see client/html/common/decorators/default
280
		 * @see client/html/catalog/home/decorators/global
281
		 * @see client/html/catalog/home/decorators/local
282
		 */
283
284
		/** client/html/catalog/home/decorators/global
285
		 * Adds a list of globally available decorators only to the catalog home html client
286
		 *
287
		 * Decorators extend the functionality of a class by adding new aspects
288
		 * (e.g. log what is currently done), executing the methods of the underlying
289
		 * class only in certain conditions (e.g. only for logged in users) or
290
		 * modify what is returned to the caller.
291
		 *
292
		 * This option allows you to wrap global decorators
293
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
294
		 *
295
		 *  client/html/catalog/home/decorators/global = array( 'decorator1' )
296
		 *
297
		 * This would add the decorator named "decorator1" defined by
298
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
299
		 *
300
		 * @param array List of decorator names
301
		 * @since 2020.10
302
		 * @category Developer
303
		 * @see client/html/common/decorators/default
304
		 * @see client/html/catalog/home/decorators/excludes
305
		 * @see client/html/catalog/home/decorators/local
306
		 */
307
308
		/** client/html/catalog/home/decorators/local
309
		 * Adds a list of local decorators only to the catalog home html client
310
		 *
311
		 * Decorators extend the functionality of a class by adding new aspects
312
		 * (e.g. log what is currently done), executing the methods of the underlying
313
		 * class only in certain conditions (e.g. only for logged in users) or
314
		 * modify what is returned to the caller.
315
		 *
316
		 * This option allows you to wrap local decorators
317
		 * ("\Aimeos\Client\Html\Catalog\Decorator\*") around the html client.
318
		 *
319
		 *  client/html/catalog/home/decorators/local = array( 'decorator2' )
320
		 *
321
		 * This would add the decorator named "decorator2" defined by
322
		 * "\Aimeos\Client\Html\Catalog\Decorator\Decorator2" only to the html client.
323
		 *
324
		 * @param array List of decorator names
325
		 * @since 2020.10
326
		 * @category Developer
327
		 * @see client/html/common/decorators/default
328
		 * @see client/html/catalog/home/decorators/excludes
329
		 * @see client/html/catalog/home/decorators/global
330
		 */
331
332
		return $this->createSubClient( 'catalog/home/' . $type, $name );
333
	}
334
335
336
	/**
337
	 * Processes the input, e.g. store given values.
338
	 *
339
	 * A view must be available and this method doesn't generate any output
340
	 * besides setting view variables if necessary.
341
	 */
342
	public function process()
343
	{
344
		$context = $this->getContext();
345
		$view = $this->getView();
346
347
		try
348
		{
349
			parent::process();
350
		}
351
		catch( \Aimeos\Client\Html\Exception $e )
352
		{
353
			$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
354
			$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
355
		}
356
		catch( \Aimeos\Controller\Frontend\Exception $e )
357
		{
358
			$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
359
			$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
360
		}
361
		catch( \Aimeos\MShop\Exception $e )
362
		{
363
			$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
364
			$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
365
		}
366
		catch( \Exception $e )
367
		{
368
			$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
369
			$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
370
			$this->logException( $e );
371
		}
372
	}
373
374
375
	/**
376
	 * Returns the list of sub-client names configured for the client.
377
	 *
378
	 * @return array List of HTML client names
379
	 */
380
	protected function getSubClientNames() : array
381
	{
382
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
383
	}
384
385
386
	/**
387
	 * Modifies the cached body content to replace content based on sessions or cookies.
388
	 *
389
	 * @param string $content Cached content
390
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
391
	 * @return string Modified body content
392
	 */
393
	public function modifyBody( string $content, string $uid ) : string
394
	{
395
		$content = parent::modifyBody( $content, $uid );
396
397
		return $this->replaceSection( $content, $this->getView()->csrf()->formfield(), 'catalog.lists.items.csrf' );
398
	}
399
400
	/**
401
	 * Sets the necessary parameter values in the view.
402
	 *
403
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
404
	 * @param array &$tags Result array for the list of tags that are associated to the output
405
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
406
	 * @return \Aimeos\MW\View\Iface Modified view object
407
	 */
408
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
409
	{
410
		$context = $this->getContext();
411
		$config = $context->getConfig();
412
413
		$productItems = map();
0 ignored issues
show
Unused Code introduced by
The assignment to $productItems is dead and can be removed.
Loading history...
414
		$domains = $config->get( 'client/html/catalog/domains', ['media', 'price', 'text', 'product' => ['promotion']] );
415
416
		/** client/html/catalog/home/domains
417
		 * A list of domain names whose items should be available in the catalog home view template
418
		 *
419
		 * The templates rendering home lists usually add the images, prices
420
		 * and texts associated to each home item. If you want to display additional
421
		 * content like the home attributes, you can configure your own list of
422
		 * domains (attribute, media, price, home, text, etc. are domains)
423
		 * whose items are fetched from the storage. Please keep in mind that
424
		 * the more domains you add to the configuration, the more time is required
425
		 * for fetching the content!
426
		 *
427
		 * This configuration option overwrites the "client/html/catalog/domains"
428
		 * option that allows to configure the domain names of the items fetched
429
		 * for all catalog related data.
430
		 *
431
		 * @param array List of domain names
432
		 * @since 2020.10
433
		 * @category Developer
434
		 * @see client/html/catalog/domains
435
		 * @see client/html/catalog/detail/domains
436
		 * @see client/html/catalog/stage/domains
437
		 * @see client/html/catalog/lists/domains
438
		 */
439
		$domains = $config->get( 'client/html/catalog/home/domains', $domains );
440
441
		if( $config->get( 'client/html/catalog/home/basket-add', false ) ) {
442
			$domains = array_merge_recursive( $domains, ['product' => ['default'], 'attribute'] );
443
		}
444
445
		$tree = \Aimeos\Controller\Frontend::create( $context, 'catalog' )->uses( $domains )
446
			->getTree( \Aimeos\Controller\Frontend\Catalog\Iface::LIST );
447
448
449
		$articles = map();
450
		$products = $tree->getRefItems( 'product', null, 'promotion' );
451
452
		foreach( $tree->getChildren() as $child ) {
453
			$products->union( $child->getRefItems( 'product', null, 'promotion' ) );
454
		}
455
456
		if( $config->get( 'client/html/catalog/home/basket-add', false ) )
457
		{
458
			foreach( $products as $product )
459
			{
460
				if( $product->getType() === 'select' ) {
461
					$articles->union( $product->getRefItems( 'product', 'default', 'default' ) );
462
				}
463
			}
464
		}
465
466
		/** client/html/catalog/home/stock/enable
467
		 * Enables or disables displaying product stock levels in product list views
468
		 *
469
		 * This configuration option allows shop owners to display product
470
		 * stock levels for each product in list views or to disable
471
		 * fetching product stock information.
472
		 *
473
		 * The stock information is fetched via AJAX and inserted via Javascript.
474
		 * This allows to cache product items by leaving out such highly
475
		 * dynamic content like stock levels which changes with each order.
476
		 *
477
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
478
		 * @since 2020.10
479
		 * @category User
480
		 * @category Developer
481
		 * @see client/html/catalog/detail/stock/enable
482
		 * @see client/html/catalog/stock/url/target
483
		 * @see client/html/catalog/stock/url/controller
484
		 * @see client/html/catalog/stock/url/action
485
		 * @see client/html/catalog/stock/url/config
486
		 */
487
		if( !$products->isEmpty() && (bool) $config->get( 'client/html/catalog/home/stock/enable', true ) === true ) {
488
			$view->homeStockUrl = $this->getStockUrl( $view, $products->union( $articles ) );
489
		}
490
491
		// Delete cache when products are added or deleted even when in "tag-all" mode
492
		$this->addMetaItems( $products->union( $articles ), $expire, $tags, ['product'] );
493
494
		$view->homeTree = $tree;
495
496
		return parent::addData( $view, $tags, $expire );
497
	}
498
}
499