Passed
Pull Request — master (#73)
by
unknown
02:48
created

Standard::addData()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 99
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 25
nc 4
nop 3
dl 0
loc 99
rs 8.8977
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), 2019-2019
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Catalog\Product;
12
13
/**
14
 * Implementation of catalog product section HTML clients for a configurable list of products.
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/product/standard/subparts
24
	 * List of HTML sub-clients rendered within the catalog product 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 2019.06
54
	 * @category Developer
55
	 */
56
	private $subPartPath = 'client/html/catalog/product/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( $uid = '' )
71
	{
72
		$context = $this->getContext();
73
74
		/** client/html/catalog/product/cache
75
		 * Enables or disables caching only for the catalog product 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/product
91
		 * All parameters defined for the catalog product 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#product
98
		 */
99
		$confkey = 'client/html/catalog/product';
100
101
		if( ($html = $this->getCached( 'body', $uid, [], $confkey )) === null ) {
0 ignored issues
show
introduced by
The condition $html = $this->getCached...y(), $confkey) === null is always false.
Loading history...
102
			$view = $this->getView();
103
			$config = $this->getContext()->getConfig();
104
105
			/** client/html/catalog/product/standard/template-body
106
			 * Relative path to the HTML body template of the catalog product client.
107
			 *
108
			 * The template file contains the HTML code and processing instructions
109
			 * to generate the result shown in the body of the frontend. The
110
			 * configuration string is the path to the template file relative
111
			 * to the templates directory (usually in client/html/templates).
112
			 *
113
			 * You can overwrite the template file configuration in extensions and
114
			 * provide alternative templates. These alternative templates should be
115
			 * named like the default one but with the string "standard" replaced by
116
			 * an unique name. You may use the name of your project for this. If
117
			 * you've implemented an alternative client class as well, "standard"
118
			 * should be replaced by the name of the new class.
119
			 *
120
			 * @param string Relative path to the template creating code for the HTML page body
121
			 * @since 2019.06
122
			 * @category Developer
123
			 * @see client/html/catalog/product/standard/template-header
124
			 */
125
			$tplconf = 'client/html/catalog/product/standard/template-body';
126
			$default = 'catalog/product/body-standard';
127
128
			try {
129
				if( !isset( $this->view ) ) {
130
					$view = $this->view = $this->getObject()->addData( $view, $this->tags, $this->expire );
131
				}
132
133
				$html = '';
134
				foreach( $this->getSubClients() as $subclient ) {
135
					$html .= $subclient->setView( $view )->getBody( $uid );
136
				}
137
				$view->listBody = $html;
138
139
				$html = $view->render( $config->get( $tplconf, $default ) );
140
				$this->setCached( 'body', $uid, [], $confkey, $html, $this->tags, $this->expire );
141
142
				return $html;
143
			} catch( \Aimeos\Client\Html\Exception $e ) {
144
				$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
145
				$view->productErrorList = $view->get( 'productErrorList', [] ) + $error;
146
			} catch( \Aimeos\Controller\Frontend\Exception $e ) {
147
				$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
148
				$view->productErrorList = $view->get( 'productErrorList', [] ) + $error;
149
			} catch( \Aimeos\MShop\Exception $e ) {
150
				$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
151
				$view->productErrorList = $view->get( 'productErrorList', [] ) + $error;
152
			} catch( \Exception $e ) {
153
				$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
154
				$view->productErrorList = $view->get( 'productErrorList', [] ) + $error;
155
				$this->logException( $e );
156
			}
157
158
			$html = $view->render( $config->get( $tplconf, $default ) );
159
		} else {
160
			$html = $this->modifyBody( $html, $uid );
161
		}
162
163
		return $html;
164
	}
165
166
167
	/**
168
	 * Returns the HTML string for insertion into the header.
169
	 *
170
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
171
	 * @return string|null String including HTML tags for the header on error
172
	 */
173
	public function getHeader( $uid = '' )
174
	{
175
		$confkey = 'client/html/catalog/product';
176
177
		if( ($html = $this->getCached( 'header', $uid, [], $confkey )) === null ) {
0 ignored issues
show
introduced by
The condition $html = $this->getCached...y(), $confkey) === null is always false.
Loading history...
178
			$view = $this->getView();
179
			$config = $this->getContext()->getConfig();
180
181
			/** client/html/catalog/product/standard/template-header
182
			 * Relative path to the HTML header template of the catalog product client.
183
			 *
184
			 * The template file contains the HTML code and processing instructions
185
			 * to generate the HTML code that is inserted into the HTML page header
186
			 * of the rendered page in the frontend. The configuration string is the
187
			 * path to the template file relative to the templates directory (usually
188
			 * in client/html/templates).
189
			 *
190
			 * You can overwrite the template file configuration in extensions and
191
			 * provide alternative templates. These alternative templates should be
192
			 * named like the default one but with the string "standard" replaced by
193
			 * an unique name. You may use the name of your project for this. If
194
			 * you've implemented an alternative client class as well, "standard"
195
			 * should be replaced by the name of the new class.
196
			 *
197
			 * @param string Relative path to the template creating code for the HTML page head
198
			 * @since 2019.06
199
			 * @category Developer
200
			 * @see client/html/catalog/product/standard/template-body
201
			 */
202
			$tplconf = 'client/html/catalog/product/standard/template-header';
203
			$default = 'catalog/product/header-standard';
204
205
			try {
206
				if( !isset( $this->view ) ) {
207
					$view = $this->view = $this->getObject()->addData( $view, $this->tags, $this->expire );
208
				}
209
210
				$html = '';
211
				foreach( $this->getSubClients() as $subclient ) {
212
					$html .= $subclient->setView( $view )->getHeader( $uid );
213
				}
214
				$view->listHeader = $html;
215
216
				$html = $view->render( $config->get( $tplconf, $default ) );
217
				$this->setCached( 'header', $uid, [], $confkey, $html, $this->tags, $this->expire );
218
219
				return $html;
220
			} catch( \Exception $e ) {
221
				$this->logException( $e );
222
			}
223
		} else {
224
			$html = $this->modifyHeader( $html, $uid );
225
		}
226
227
		return $html;
228
	}
229
230
231
	/**
232
	 * Returns the sub-client given by its name.
233
	 *
234
	 * @param string $type Name of the client type
235
	 * @param string|null $name Name of the sub-client (Default if null)
236
	 * @return \Aimeos\Client\Html\Iface Sub-client object
237
	 */
238
	public function getSubClient( $type, $name = null )
239
	{
240
		/** client/html/catalog/product/decorators/excludes
241
		 * Excludes decorators added by the "common" option from the catalog product html client
242
		 *
243
		 * Decorators extend the functionality of a class by adding new aspects
244
		 * (e.g. log what is currently done), executing the methods of the underlying
245
		 * class only in certain conditions (e.g. only for logged in users) or
246
		 * modify what is returned to the caller.
247
		 *
248
		 * This option allows you to remove a decorator added via
249
		 * "client/html/common/decorators/default" before they are wrapped
250
		 * around the html client.
251
		 *
252
		 *  client/html/catalog/product/decorators/excludes = array( 'decorator1' )
253
		 *
254
		 * This would remove the decorator named "decorator1" from the list of
255
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
256
		 * "client/html/common/decorators/default" to the html client.
257
		 *
258
		 * @param array List of decorator names
259
		 * @since 2019.06
260
		 * @category Developer
261
		 * @see client/html/common/decorators/default
262
		 * @see client/html/catalog/product/decorators/global
263
		 * @see client/html/catalog/product/decorators/local
264
		 */
265
266
		/** client/html/catalog/product/decorators/global
267
		 * Adds a list of globally available decorators only to the catalog product html client
268
		 *
269
		 * Decorators extend the functionality of a class by adding new aspects
270
		 * (e.g. log what is currently done), executing the methods of the underlying
271
		 * class only in certain conditions (e.g. only for logged in users) or
272
		 * modify what is returned to the caller.
273
		 *
274
		 * This option allows you to wrap global decorators
275
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
276
		 *
277
		 *  client/html/catalog/product/decorators/global = array( 'decorator1' )
278
		 *
279
		 * This would add the decorator named "decorator1" defined by
280
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
281
		 *
282
		 * @param array List of decorator names
283
		 * @since 2019.06
284
		 * @category Developer
285
		 * @see client/html/common/decorators/default
286
		 * @see client/html/catalog/product/decorators/excludes
287
		 * @see client/html/catalog/product/decorators/local
288
		 */
289
290
		/** client/html/catalog/product/decorators/local
291
		 * Adds a list of local decorators only to the catalog product html client
292
		 *
293
		 * Decorators extend the functionality of a class by adding new aspects
294
		 * (e.g. log what is currently done), executing the methods of the underlying
295
		 * class only in certain conditions (e.g. only for logged in users) or
296
		 * modify what is returned to the caller.
297
		 *
298
		 * This option allows you to wrap local decorators
299
		 * ("\Aimeos\Client\Html\Catalog\Decorator\*") around the html client.
300
		 *
301
		 *  client/html/catalog/product/decorators/local = array( 'decorator2' )
302
		 *
303
		 * This would add the decorator named "decorator2" defined by
304
		 * "\Aimeos\Client\Html\Catalog\Decorator\Decorator2" only to the html client.
305
		 *
306
		 * @param array List of decorator names
307
		 * @since 2019.06
308
		 * @category Developer
309
		 * @see client/html/common/decorators/default
310
		 * @see client/html/catalog/product/decorators/excludes
311
		 * @see client/html/catalog/product/decorators/global
312
		 */
313
314
		return $this->createSubClient( 'catalog/product/' . $type, $name );
315
	}
316
317
318
	/**
319
	 * Processes the input, e.g. store given values.
320
	 * A view must be available and this method doesn't generate any output
321
	 * besides setting view variables.
322
	 */
323
	public function process()
324
	{
325
		$context = $this->getContext();
326
		$view = $this->getView();
327
328
		try {
329
			parent::process();
330
		} catch( \Aimeos\Client\Html\Exception $e ) {
331
			$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
332
			$view->productErrorList = $view->get( 'productErrorList', [] ) + $error;
333
		} catch( \Aimeos\Controller\Frontend\Exception $e ) {
334
			$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
335
			$view->productErrorList = $view->get( 'productErrorList', [] ) + $error;
336
		} catch( \Aimeos\MShop\Exception $e ) {
337
			$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
338
			$view->productErrorList = $view->get( 'productErrorList', [] ) + $error;
339
		} catch( \Exception $e ) {
340
			$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
341
			$view->productErrorList = $view->get( 'productErrorList', [] ) + $error;
342
			$this->logException( $e );
343
		}
344
	}
345
346
347
	/**
348
	 * Returns the list of sub-client names configured for the client.
349
	 *
350
	 * @return array List of HTML client names
351
	 */
352
	protected function getSubClientNames()
353
	{
354
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
355
	}
356
357
358
	/**
359
	 * Modifies the cached body content to replace content based on sessions or cookies.
360
	 *
361
	 * @param string $content Cached content
362
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
363
	 * @return string Modified body content
364
	 */
365
	public function modifyBody( $content, $uid )
366
	{
367
		$content = parent::modifyBody( $content, $uid );
368
369
		return $this->replaceSection( $content, $this->getView()->csrf()->formfield(), 'catalog.lists.items.csrf' );
370
	}
371
372
	/**
373
	 * Sets the necessary parameter values in the view.
374
	 *
375
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
376
	 * @param array &$tags Result array for the list of tags that are associated to the output
377
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
378
	 * @return \Aimeos\MW\View\Iface Modified view object
379
	 */
380
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], &$expire = null )
381
	{
382
		$context = $this->getContext();
383
		$config = $context->getConfig();
384
385
		$productItems = [];
386
		$domains = $config->get( 'client/html/catalog/domains', ['media', 'price', 'text'] );
387
388
		/** client/html/catalog/product/domains
389
		 * A list of domain names whose items should be available in the catalog product view template
390
		 *
391
		 * The templates rendering product lists usually add the images, prices
392
		 * and texts associated to each product item. If you want to display additional
393
		 * content like the product attributes, you can configure your own list of
394
		 * domains (attribute, media, price, product, text, etc. are domains)
395
		 * whose items are fetched from the storage. Please keep in mind that
396
		 * the more domains you add to the configuration, the more time is required
397
		 * for fetching the content!
398
		 *
399
		 * This configuration option overwrites the "client/html/catalog/domains"
400
		 * option that allows to configure the domain names of the items fetched
401
		 * for all catalog related data.
402
		 *
403
		 * @param array List of domain names
404
		 * @since 2019.06
405
		 * @category Developer
406
		 * @see client/html/catalog/domains
407
		 * @see client/html/catalog/detail/domains
408
		 * @see client/html/catalog/stage/domains
409
		 * @see client/html/catalog/lists/domains
410
		 */
411
		$domains = $config->get( 'client/html/catalog/product/domains', $domains );
412
413
		/** client/html/catalog/product/product-codes
414
		 * List of codes of products to load for the current list.
415
		 * Should be set dynamically through some integration plugin,
416
		 * to allow a list of products with configurable products.
417
		 *
418
		 * @param string List of codes of products to load for the current list
419
		 * @since 2019.06
420
		 * @category Developer
421
		 */
422
		$productCodes = $config->get( 'client/html/catalog/product/product-codes', [] );
423
424
		$products = \Aimeos\Controller\Frontend::create( $context, 'product' )
425
			->compare( '==', 'product.code', $productCodes )
426
			->slice( 0, count( $productCodes ) )
427
			->uses( $domains )
428
			->search();
429
430
		// Sort products by the order given in the configuration "client/html/catalog/product/product-codes".
431
		$productCodesOrder = array_flip( $productCodes );
432
		usort( $products, function( $a, $b ) use ( $productCodesOrder ) {
433
			return $productCodesOrder[$a->getCode()] - $productCodesOrder[$b->getCode()];
434
		} );
435
436
437
		if( $config->get( 'client/html/catalog/product/basket-add', false ) ) {
438
			foreach( $products as $product ) {
439
				if( $product->getType() === 'select' ) {
440
					$productItems += $product->getRefItems( 'product', 'default', 'default' );
441
				}
442
			}
443
		}
444
445
		/** client/html/catalog/product/stock/enable
446
		 * Enables or disables displaying product stock levels in product list views
447
		 *
448
		 * This configuration option allows shop owners to display product
449
		 * stock levels for each product in list views or to disable
450
		 * fetching product stock information.
451
		 *
452
		 * The stock information is fetched via AJAX and inserted via Javascript.
453
		 * This allows to cache product items by leaving out such highly
454
		 * dynamic content like stock levels which changes with each order.
455
		 *
456
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
457
		 * @since 2019.06
458
		 * @category User
459
		 * @category Developer
460
		 * @see client/html/catalog/detail/stock/enable
461
		 * @see client/html/catalog/stock/url/target
462
		 * @see client/html/catalog/stock/url/controller
463
		 * @see client/html/catalog/stock/url/action
464
		 * @see client/html/catalog/stock/url/config
465
		 */
466
467
		if( !empty( $products ) && (bool) $config->get( 'client/html/catalog/product/stock/enable', true ) === true ) {
468
			$view->itemsStockUrl = $this->getStockUrl( $view, $products + $productItems );
469
		}
470
471
		// Delete cache when products are added or deleted even when in "tag-all" mode
472
		$this->addMetaItems( $products + $productItems, $expire, $tags, ['product'] );
473
474
		$view->productItems = $products;
475
		$view->productTotal = count( $products );
476
		$view->productProductItems = $productItems;
477
478
		return parent::addData( $view, $tags, $expire );
479
	}
480
}
481