Passed
Push — master ( 25bd95...9e12c4 )
by Aimeos
02:39
created

Standard::data()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 68
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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