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

Standard::init()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 24
nc 36
nop 0
dl 0
loc 41
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

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, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Account\Favorite;
13
14
15
/**
16
 * Default implementation of account favorite HTML client.
17
 *
18
 * @package Client
19
 * @subpackage Html
20
 */
21
class Standard
22
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
23
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
24
{
25
	/** client/html/account/favorite/subparts
26
	 * List of HTML sub-clients rendered within the account favorite 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/account/favorite/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 body( string $uid = '' ) : string
70
	{
71
		$context = $this->getContext();
72
		$view = $this->getView();
73
74
		try
75
		{
76
			$view = $this->view = $this->view ?? $this->getObject()->data( $view );
77
78
			$html = '';
79
			foreach( $this->getSubClients() as $subclient ) {
80
				$html .= $subclient->setView( $view )->body( $uid );
81
			}
82
			$view->favoriteBody = $html;
83
		}
84
		catch( \Aimeos\Client\Html\Exception $e )
85
		{
86
			$error = array( $context->translate( 'client', $e->getMessage() ) );
87
			$view->favoriteErrorList = array_merge( $view->get( 'favoriteErrorList', [] ), $error );
88
		}
89
		catch( \Aimeos\Controller\Frontend\Exception $e )
90
		{
91
			$error = array( $context->translate( 'controller/frontend', $e->getMessage() ) );
92
			$view->favoriteErrorList = array_merge( $view->get( 'favoriteErrorList', [] ), $error );
93
		}
94
		catch( \Aimeos\MShop\Exception $e )
95
		{
96
			$error = array( $context->translate( 'mshop', $e->getMessage() ) );
97
			$view->favoriteErrorList = array_merge( $view->get( 'favoriteErrorList', [] ), $error );
98
		}
99
		catch( \Exception $e )
100
		{
101
			$error = array( $context->translate( 'client', 'A non-recoverable error occured' ) );
102
			$view->favoriteErrorList = array_merge( $view->get( 'favoriteErrorList', [] ), $error );
103
			$this->logException( $e );
104
		}
105
106
		/** client/html/account/favorite/template-body
107
		 * Relative path to the HTML body template of the account favorite 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 2014.03
123
		 * @category Developer
124
		 * @see client/html/account/favorite/template-header
125
		 */
126
		$tplconf = 'client/html/account/favorite/template-body';
127
		$default = 'account/favorite/body-standard';
128
129
		return $view->render( $view->config( $tplconf, $default ) );
130
	}
131
132
133
	/**
134
	 * Returns the HTML string for insertion into the header.
135
	 *
136
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
137
	 * @return string|null String including HTML tags for the header on error
138
	 */
139
	public function header( string $uid = '' ) : ?string
140
	{
141
		$view = $this->getView();
142
143
		try
144
		{
145
			$view = $this->view = $this->view ?? $this->getObject()->data( $view );
146
147
			$html = '';
148
			foreach( $this->getSubClients() as $subclient ) {
149
				$html .= $subclient->setView( $view )->header( $uid );
150
			}
151
			$view->favoriteHeader = $html;
152
153
			/** client/html/account/favorite/template-header
154
			 * Relative path to the HTML header template of the account favorite client.
155
			 *
156
			 * The template file contains the HTML code and processing instructions
157
			 * to generate the HTML code that is inserted into the HTML page header
158
			 * of the rendered page in the frontend. The configuration string is the
159
			 * path to the template file relative to the templates directory (usually
160
			 * in client/html/templates).
161
			 *
162
			 * You can overwrite the template file configuration in extensions and
163
			 * provide alternative templates. These alternative templates should be
164
			 * named like the default one but with the string "standard" replaced by
165
			 * an unique name. You may use the name of your project for this. If
166
			 * you've implemented an alternative client class as well, "standard"
167
			 * should be replaced by the name of the new class.
168
			 *
169
			 * @param string Relative path to the template creating code for the HTML page head
170
			 * @since 2014.03
171
			 * @category Developer
172
			 * @see client/html/account/favorite/template-body
173
			 */
174
			$tplconf = 'client/html/account/favorite/template-header';
175
			$default = 'account/favorite/header-standard';
176
177
			return $view->render( $view->config( $tplconf, $default ) );
178
		}
179
		catch( \Exception $e )
180
		{
181
			$this->logException( $e );
182
		}
183
184
		return null;
185
	}
186
187
188
	/**
189
	 * Returns the sub-client given by its name.
190
	 *
191
	 * @param string $type Name of the client type
192
	 * @param string|null $name Name of the sub-client (Default if null)
193
	 * @return \Aimeos\Client\Html\Iface Sub-client object
194
	 */
195
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
196
	{
197
		/** client/html/account/favorite/decorators/excludes
198
		 * Excludes decorators added by the "common" option from the account favorite html client
199
		 *
200
		 * Decorators extend the functionality of a class by adding new aspects
201
		 * (e.g. log what is currently done), executing the methods of the underlying
202
		 * class only in certain conditions (e.g. only for logged in users) or
203
		 * modify what is returned to the caller.
204
		 *
205
		 * This option allows you to remove a decorator added via
206
		 * "client/html/common/decorators/default" before they are wrapped
207
		 * around the html client.
208
		 *
209
		 *  client/html/account/favorite/decorators/excludes = array( 'decorator1' )
210
		 *
211
		 * This would remove the decorator named "decorator1" from the list of
212
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
213
		 * "client/html/common/decorators/default" to the html client.
214
		 *
215
		 * @param array List of decorator names
216
		 * @since 2014.05
217
		 * @category Developer
218
		 * @see client/html/common/decorators/default
219
		 * @see client/html/account/favorite/decorators/global
220
		 * @see client/html/account/favorite/decorators/local
221
		 */
222
223
		/** client/html/account/favorite/decorators/global
224
		 * Adds a list of globally available decorators only to the account favorite html client
225
		 *
226
		 * Decorators extend the functionality of a class by adding new aspects
227
		 * (e.g. log what is currently done), executing the methods of the underlying
228
		 * class only in certain conditions (e.g. only for logged in users) or
229
		 * modify what is returned to the caller.
230
		 *
231
		 * This option allows you to wrap global decorators
232
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
233
		 *
234
		 *  client/html/account/favorite/decorators/global = array( 'decorator1' )
235
		 *
236
		 * This would add the decorator named "decorator1" defined by
237
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
238
		 *
239
		 * @param array List of decorator names
240
		 * @since 2014.05
241
		 * @category Developer
242
		 * @see client/html/common/decorators/default
243
		 * @see client/html/account/favorite/decorators/excludes
244
		 * @see client/html/account/favorite/decorators/local
245
		 */
246
247
		/** client/html/account/favorite/decorators/local
248
		 * Adds a list of local decorators only to the account favorite html client
249
		 *
250
		 * Decorators extend the functionality of a class by adding new aspects
251
		 * (e.g. log what is currently done), executing the methods of the underlying
252
		 * class only in certain conditions (e.g. only for logged in users) or
253
		 * modify what is returned to the caller.
254
		 *
255
		 * This option allows you to wrap local decorators
256
		 * ("\Aimeos\Client\Html\Account\Decorator\*") around the html client.
257
		 *
258
		 *  client/html/account/favorite/decorators/local = array( 'decorator2' )
259
		 *
260
		 * This would add the decorator named "decorator2" defined by
261
		 * "\Aimeos\Client\Html\Account\Decorator\Decorator2" only to the html client.
262
		 *
263
		 * @param array List of decorator names
264
		 * @since 2014.05
265
		 * @category Developer
266
		 * @see client/html/common/decorators/default
267
		 * @see client/html/account/favorite/decorators/excludes
268
		 * @see client/html/account/favorite/decorators/global
269
		 */
270
		return $this->createSubClient( 'account/favorite/' . $type, $name );
271
	}
272
273
274
	/**
275
	 * Processes the input, e.g. store given values.
276
	 *
277
	 * A view must be available and this method doesn't generate any output
278
	 * besides setting view variables if necessary.
279
	 */
280
	public function init()
281
	{
282
		$view = $this->getView();
283
		$context = $this->getContext();
284
		$ids = (array) $view->param( 'fav_id', [] );
285
286
		try
287
		{
288
			if( $context->getUserId() != null && !empty( $ids ) && $view->request()->getMethod() === 'POST' )
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $context->getUserId() of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
289
			{
290
				switch( $view->param( 'fav_action' ) )
291
				{
292
					case 'add':
293
						$this->addFavorites( $ids ); break;
294
					case 'delete':
295
						$this->deleteFavorites( $ids ); break;
296
				}
297
			}
298
299
			parent::init();
300
		}
301
		catch( \Aimeos\MShop\Exception $e )
302
		{
303
			$error = array( $context->translate( 'mshop', $e->getMessage() ) );
304
			$view->favoriteErrorList = array_merge( $view->get( 'favoriteErrorList', [] ), $error );
305
		}
306
		catch( \Aimeos\Controller\Frontend\Exception $e )
307
		{
308
			$error = array( $context->translate( 'controller/frontend', $e->getMessage() ) );
309
			$view->favoriteErrorList = array_merge( $view->get( 'favoriteErrorList', [] ), $error );
310
		}
311
		catch( \Aimeos\Client\Html\Exception $e )
312
		{
313
			$error = array( $context->translate( 'client', $e->getMessage() ) );
314
			$view->favoriteErrorList = array_merge( $view->get( 'favoriteErrorList', [] ), $error );
315
		}
316
		catch( \Exception $e )
317
		{
318
			$error = array( $context->translate( 'client', 'A non-recoverable error occured' ) );
319
			$view->favoriteErrorList = array_merge( $view->get( 'favoriteErrorList', [] ), $error );
320
			$this->logException( $e );
321
		}
322
	}
323
324
325
	/**
326
	 * Adds new product favorite references to the given customer
327
	 *
328
	 * @param array $ids List of product IDs
329
	 */
330
	protected function addFavorites( array $ids )
331
	{
332
		$context = $this->getContext();
333
334
		/** client/html/account/favorite/maxitems
335
		 * Maximum number of products that can be favorites
336
		 *
337
		 * This option limits the number of products users can add to their
338
		 * favorite list. It must be a positive integer value greater than 0.
339
		 *
340
		 * @param integer Number of products
341
		 * @since 2019.04
342
		 * @category User
343
		 * @category Developer
344
		 */
345
		$max = $context->getConfig()->get( 'client/html/account/favorite/maxitems', 100 );
346
347
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'customer' );
348
		$item = $cntl->uses( ['product' => ['favorite']] )->get();
349
350
		if( count( $item->getRefItems( 'product', null, 'favorite' ) ) + count( $ids ) > $max )
351
		{
352
			$msg = sprintf( $context->translate( 'client', 'You can only save up to %1$s products as favorites' ), $max );
353
			throw new \Aimeos\Client\Html\Exception( $msg );
354
		}
355
356
		foreach( $ids as $id )
357
		{
358
			if( ( $listItem = $item->getListItem( 'product', 'favorite', $id ) ) === null ) {
359
				$listItem = $cntl->createListItem();
360
			}
361
			$cntl->addListItem( 'product', $listItem->setType( 'favorite' )->setRefId( $id ) );
362
		}
363
364
		$cntl->store();
365
	}
366
367
368
	/**
369
	 * Removes product favorite references from the customer
370
	 *
371
	 * @param array $ids List of product IDs
372
	 */
373
	protected function deleteFavorites( array $ids )
374
	{
375
		$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
376
		$item = $cntl->uses( ['product' => ['favorite']] )->get();
377
378
		foreach( $ids as $id )
379
		{
380
			if( ( $listItem = $item->getListItem( 'product', 'favorite', $id ) ) !== null ) {
381
				$cntl->deleteListItem( 'product', $listItem );
382
			}
383
		}
384
385
		$cntl->store();
386
	}
387
388
389
	/**
390
	 * Returns the list of sub-client names configured for the client.
391
	 *
392
	 * @return array List of HTML client names
393
	 */
394
	protected function getSubClientNames() : array
395
	{
396
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
397
	}
398
399
400
	/**
401
	 * Returns the sanitized page from the parameters for the product list.
402
	 *
403
	 * @param \Aimeos\MW\View\Iface $view View instance with helper for retrieving the required parameters
404
	 * @return int Page number starting from 1
405
	 */
406
	protected function getProductListPage( \Aimeos\MW\View\Iface $view ) : int
407
	{
408
		$page = (int) $view->param( 'fav_page', 1 );
409
		return ( $page < 1 ? 1 : $page );
410
	}
411
412
413
	/**
414
	 * Returns the sanitized page size from the parameters for the product list.
415
	 *
416
	 * @param \Aimeos\MW\View\Iface $view View instance with helper for retrieving the required parameters
417
	 * @return int Page size
418
	 */
419
	protected function getProductListSize( \Aimeos\MW\View\Iface $view ) : int
420
	{
421
		/** client/html/account/favorite/size
422
		 * The number of products shown in a list page for favorite products
423
		 *
424
		 * Limits the number of products that is shown in the list pages to the
425
		 * given value. If more products are available, the products are split
426
		 * into bunches which will be shown on their own list page. The user is
427
		 * able to move to the next page (or previous one if it's not the first)
428
		 * to display the next (or previous) products.
429
		 *
430
		 * The value must be an integer number from 1 to 100. Negative values as
431
		 * well as values above 100 are not allowed. The value can be overwritten
432
		 * per request if the "l_size" parameter is part of the URL.
433
		 *
434
		 * @param integer Number of products
435
		 * @since 2014.09
436
		 * @category User
437
		 * @category Developer
438
		 * @see client/html/catalog/lists/size
439
		 */
440
		$defaultSize = $this->getContext()->getConfig()->get( 'client/html/account/favorite/size', 48 );
441
442
		$size = (int) $view->param( 'fav-size', $defaultSize );
443
		return ( $size < 1 || $size > 100 ? $defaultSize : $size );
444
	}
445
446
447
	/**
448
	 * Sets the necessary parameter values in the view.
449
	 *
450
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
451
	 * @param array &$tags Result array for the list of tags that are associated to the output
452
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
453
	 * @return \Aimeos\MW\View\Iface Modified view object
454
	 */
455
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
456
	{
457
		$context = $this->getContext();
458
459
		/** client/html/account/favorite/domains
460
		 * A list of domain names whose items should be available in the account favorite view template
461
		 *
462
		 * The templates rendering product details usually add the images,
463
		 * prices and texts associated to the product item. If you want to
464
		 * display additional or less content, you can configure your own
465
		 * list of domains (attribute, media, price, product, text, etc. are
466
		 * domains) whose items are fetched from the storage. Please keep
467
		 * in mind that the more domains you add to the configuration, the
468
		 * more time is required for fetching the content!
469
		 *
470
		 * @param array List of domain names
471
		 * @since 2014.09
472
		 * @category Developer
473
		 * @see client/html/catalog/domains
474
		 */
475
		$domains = $context->getConfig()->get( 'client/html/account/favorite/domains', ['text', 'price', 'media'] );
476
		$domains['product'] = ['favorite'];
477
478
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'customer' );
479
		$listItems = $cntl->uses( $domains )->get()->getListItems( 'product', 'favorite' );
480
		$total = count( $listItems );
481
482
		$size = $this->getProductListSize( $view );
483
		$current = $this->getProductListPage( $view );
484
		$last = ( $total != 0 ? ceil( $total / $size ) : 1 );
485
486
		$view->favoriteItems = $listItems;
487
		$view->favoritePageFirst = 1;
488
		$view->favoritePagePrev = ( $current > 1 ? $current - 1 : 1 );
489
		$view->favoritePageNext = ( $current < $last ? $current + 1 : $last );
490
		$view->favoritePageLast = $last;
491
		$view->favoritePageCurr = $current;
492
493
		return parent::data( $view, $tags, $expire );
494
	}
495
}
496