Passed
Push — master ( dc5862...f6527a )
by Aimeos
04:15 queued 13s
created

Standard::data()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 103
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
nc 8
nop 3
dl 0
loc 103
rs 8.5386
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-2022
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
	private $tags = [];
24
	private $expire;
25
	private $view;
26
27
28
	/**
29
	 * Returns the HTML code for insertion into the body.
30
	 *
31
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
32
	 * @return string HTML code
33
	 */
34
	public function body( string $uid = '' ) : string
35
	{
36
		$context = $this->context();
0 ignored issues
show
Unused Code introduced by
The assignment to $context is dead and can be removed.
Loading history...
37
38
		/** client/html/catalog/product/cache
39
		 * Enables or disables caching only for the catalog product component
40
		 *
41
		 * Disable caching for components can be useful if you would have too much
42
		 * entries to cache or if the component contains non-cacheable parts that
43
		 * can't be replaced using the modify() method.
44
		 *
45
		 * @param boolean True to enable caching, false to disable
46
		 * @category Developer
47
		 * @category User
48
		 * @see client/html/catalog/detail/cache
49
		 * @see client/html/catalog/filter/cache
50
		 * @see client/html/catalog/stage/cache
51
		 * @see client/html/catalog/list/cache
52
		 */
53
54
		/** client/html/catalog/product
55
		 * All parameters defined for the catalog product component and its subparts
56
		 *
57
		 * Please refer to the single settings for details.
58
		 *
59
		 * @param array Associative list of name/value settings
60
		 * @category Developer
61
		 * @see client/html/catalog#product
62
		 */
63
		$confkey = 'client/html/catalog/product';
64
65
		if( $html = $this->cached( 'body', $uid, [], $confkey ) ) {
66
			return $this->modify( $html, $uid );
67
		}
68
69
		$config = $this->context()->config();
70
		$template = $config->get( 'client/html/catalog/product/template-body', 'catalog/product/body' );
71
72
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
73
		$html = $view->render( $template );
74
75
		return $this->cache( 'body', $uid, [], $confkey, $html, $this->tags, $this->expire );
76
	}
77
78
79
	/**
80
	 * Returns the HTML string for insertion into the header.
81
	 *
82
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
83
	 * @return string|null String including HTML tags for the header on error
84
	 */
85
	public function header( string $uid = '' ) : ?string
86
	{
87
		$confkey = 'client/html/catalog/product';
88
89
		if( $html = $this->cached( 'header', $uid, [], $confkey ) ) {
90
			return $this->modify( $html, $uid );
91
		}
92
93
		$config = $this->context()->config();
94
		$template = $config->get( 'client/html/catalog/product/template-header', 'catalog/product/header' );
95
96
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
97
		$html = $view->render( $template );
98
99
		return $this->cache( 'header', $uid, [], $confkey, $html, $this->tags, $this->expire );
100
	}
101
102
103
	/**
104
	 * Modifies the cached content to replace content based on sessions or cookies.
105
	 *
106
	 * @param string $content Cached content
107
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
108
	 * @return string Modified content
109
	 */
110
	public function modify( string $content, string $uid ) : string
111
	{
112
		return $this->replaceSection( $content, $this->view()->csrf()->formfield(), 'catalog.lists.items.csrf' );
113
	}
114
115
116
	/**
117
	 * Sets the necessary parameter values in the view.
118
	 *
119
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
120
	 * @param array &$tags Result array for the list of tags that are associated to the output
121
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
122
	 * @return \Aimeos\MW\View\Iface Modified view object
123
	 */
124
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
125
	{
126
		$context = $this->context();
127
		$config = $context->config();
128
		$productItems = map();
129
130
		/** client/html/catalog/product/domains
131
		 * A list of domain names whose items should be available in the catalog product view template
132
		 *
133
		 * The templates rendering product lists usually add the images, prices
134
		 * and texts associated to each product item. If you want to display additional
135
		 * content like the product attributes, you can configure your own list of
136
		 * domains (attribute, media, price, product, text, etc. are domains)
137
		 * whose items are fetched from the storage. Please keep in mind that
138
		 * the more domains you add to the configuration, the more time is required
139
		 * for fetching the content!
140
		 *
141
		 * This configuration option overwrites the "client/html/catalog/domains"
142
		 * option that allows to configure the domain names of the items fetched
143
		 * for all catalog related data.
144
		 *
145
		 * @param array List of domain names
146
		 * @since 2019.06
147
		 * @category Developer
148
		 * @see client/html/catalog/domains
149
		 * @see client/html/catalog/detail/domains
150
		 * @see client/html/catalog/stage/domains
151
		 * @see client/html/catalog/lists/domains
152
		 */
153
		$domains = ['media', 'media/property', 'price', 'text'];
154
		$domains = $config->get( 'client/html/catalog/domains', $domains );
155
		$domains = $config->get( 'client/html/catalog/product/domains', $domains );
156
157
		if( $config->get( 'client/html/catalog/product/basket-add', false ) ) {
158
			$domains = array_merge_recursive( $domains, ['product' => ['default'], 'attribute' => ['variant', 'custom', 'config']] );
159
		}
160
161
		/** client/html/catalog/product/product-codes
162
		 * List of codes of products to load for the current list.
163
		 * Should be set dynamically through some integration plugin,
164
		 * to allow a list of products with configurable products.
165
		 *
166
		 * @param string List of codes of products to load for the current list
167
		 * @since 2019.06
168
		 * @category Developer
169
		 */
170
		$productCodes = $config->get( 'client/html/catalog/product/product-codes', [] );
171
172
		$products = \Aimeos\Controller\Frontend::create( $context, 'product' )
173
			->compare( '==', 'product.code', $productCodes )
174
			->slice( 0, count( $productCodes ) )
175
			->uses( $domains )
176
			->search();
177
178
		// Sort products by the order given in the configuration "client/html/catalog/product/product-codes".
179
		$productCodesOrder = array_flip( $productCodes );
180
		$products->usort( function( $a, $b ) use ( $productCodesOrder ) {
181
			return $productCodesOrder[$a->getCode()] - $productCodesOrder[$b->getCode()];
182
		} );
183
184
		if( $config->get( 'client/html/catalog/product/basket-add', false ) )
185
		{
186
			foreach( $products as $product )
187
			{
188
				if( $product->getType() === 'select' ) {
189
					$productItems->union( $product->getRefItems( 'product', 'default', 'default' ) );
190
				}
191
			}
192
		}
193
194
		/** client/html/catalog/product/stock/enable
195
		 * Enables or disables displaying product stock levels in product list views
196
		 *
197
		 * This configuration option allows shop owners to display product
198
		 * stock levels for each product in list views or to disable
199
		 * fetching product stock information.
200
		 *
201
		 * The stock information is fetched via AJAX and inserted via Javascript.
202
		 * This allows to cache product items by leaving out such highly
203
		 * dynamic content like stock levels which changes with each order.
204
		 *
205
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
206
		 * @since 2019.06
207
		 * @category User
208
		 * @category Developer
209
		 * @see client/html/catalog/detail/stock/enable
210
		 * @see client/html/catalog/stock/url/target
211
		 * @see client/html/catalog/stock/url/controller
212
		 * @see client/html/catalog/stock/url/action
213
		 * @see client/html/catalog/stock/url/config
214
		 */
215
		if( !$products->isEmpty() && (bool) $config->get( 'client/html/catalog/product/stock/enable', true ) === true ) {
216
			$view->itemsStockUrl = $this->getStockUrl( $view, $products->union( $productItems ) );
217
		}
218
219
		// Delete cache when products are added or deleted even when in "tag-all" mode
220
		$this->addMetaItems( $products->union( $productItems ), $expire, $tags, ['product'] );
221
222
		$view->productItems = $products;
223
		$view->productTotal = count( $products );
224
		$view->productProductItems = $productItems;
225
226
		return parent::data( $view, $tags, $expire );
227
	}
228
229
230
	/** client/html/catalog/product/template-body
231
	 * Relative path to the HTML body template of the catalog product client.
232
	 *
233
	 * The template file contains the HTML code and processing instructions
234
	 * to generate the result shown in the body of the frontend. The
235
	 * configuration string is the path to the template file relative
236
	 * to the templates directory (usually in client/html/templates).
237
	 *
238
	 * You can overwrite the template file configuration in extensions and
239
	 * provide alternative templates. These alternative templates should be
240
	 * named like the default one but suffixed by
241
	 * an unique name. You may use the name of your project for this. If
242
	 * you've implemented an alternative client class as well, it
243
	 * should be suffixed by the name of the new class.
244
	 *
245
	 * @param string Relative path to the template creating code for the HTML page body
246
	 * @since 2019.06
247
	 * @category Developer
248
	 * @see client/html/catalog/product/template-header
249
	 */
250
251
	/** client/html/catalog/product/template-header
252
	 * Relative path to the HTML header template of the catalog product client.
253
	 *
254
	 * The template file contains the HTML code and processing instructions
255
	 * to generate the HTML code that is inserted into the HTML page header
256
	 * of the rendered page in the frontend. The configuration string is the
257
	 * path to the template file relative to the templates directory (usually
258
	 * in client/html/templates).
259
	 *
260
	 * You can overwrite the template file configuration in extensions and
261
	 * provide alternative templates. These alternative templates should be
262
	 * named like the default one but suffixed by
263
	 * an unique name. You may use the name of your project for this. If
264
	 * you've implemented an alternative client class as well, it
265
	 * should be suffixed by the name of the new class.
266
	 *
267
	 * @param string Relative path to the template creating code for the HTML page head
268
	 * @since 2019.06
269
	 * @category Developer
270
	 * @see client/html/catalog/product/template-body
271
	 */
272
}
273