Passed
Push — master ( c66389...2cab20 )
by Aimeos
02:51
created

Standard::domains()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 51
rs 10

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-2022
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
	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/home/cache
37
		 * Enables or disables caching only for the catalog home 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/home
51
		 * All parameters defined for the catalog home 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#home
57
		 */
58
		$confkey = 'client/html/catalog/home';
59
		$config = $this->context()->config();
60
61
		if( $html = $this->cached( 'body', $uid, [], $confkey ) ) {
62
			return $this->modify( $html, $uid );
63
		}
64
65
		$template = $config->get( 'client/html/catalog/home/template-body', 'catalog/home/body' );
66
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
67
		$html = $view->render( $template );
68
69
		return $this->cache( 'body', $uid, [], $confkey, $html, $this->tags, $this->expire );
70
	}
71
72
73
	/**
74
	 * Returns the HTML string for insertion into the header.
75
	 *
76
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
77
	 * @return string|null String including HTML tags for the header on error
78
	 */
79
	public function header( string $uid = '' ) : ?string
80
	{
81
		$confkey = 'client/html/catalog/home';
82
		$config = $this->context()->config();
83
84
		if( $html = $this->cached( 'header', $uid, [], $confkey ) ) {
85
			return $this->modify( $html, $uid );
86
		}
87
88
		$template = $config->get( 'client/html/catalog/home/template-header', 'catalog/home/header' );
89
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
90
		$html = $view->render( $template );
91
92
		return $this->cache( 'header', $uid, [], $confkey, $html, $this->tags, $this->expire );
93
	}
94
95
96
	/**
97
	 * Sets the necessary parameter values in the view.
98
	 *
99
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
100
	 * @param array &$tags Result array for the list of tags that are associated to the output
101
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
102
	 * @return \Aimeos\Base\View\Iface Modified view object
103
	 */
104
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\Base\View\Iface
105
	{
106
		$context = $this->context();
107
		$config = $context->config();
0 ignored issues
show
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
108
109
		$tree = \Aimeos\Controller\Frontend::create( $context, 'catalog' )->uses( $this->domains() )
110
			->getTree( \Aimeos\Controller\Frontend\Catalog\Iface::LIST );
111
112
		// Delete cache when products are added or deleted even when in "tag-all" mode
113
		$this->addMetaItems( $tree, $expire, $tags, ['catalog', 'product'] );
114
115
		$products = map( $tree->getChildren() )->getRefItems( 'product', null, 'promotion' )->flat( 1 );
116
117
		$view->homeTree = $tree;
118
		$view->homeStockUrl = $this->stockUrl( $products );
119
120
		return parent::data( $view, $tags, $expire );
121
	}
122
123
124
	/**
125
	 * Returns the data domains fetched along with the products
126
	 *
127
	 * @return array List of domain names
128
	 */
129
	protected function domains() : array
130
	{
131
		$context = $this->context();
132
		$config = $context->config();
133
134
		/** client/html/catalog/home/domains
135
		 * A list of domain names whose items should be available in the catalog home view template
136
		 *
137
		 * The templates rendering home lists usually add the images, prices
138
		 * and texts associated to each home item. If you want to display additional
139
		 * content like the home attributes, you can configure your own list of
140
		 * domains (attribute, media, price, home, text, etc. are domains)
141
		 * whose items are fetched from the storage. Please keep in mind that
142
		 * the more domains you add to the configuration, the more time is required
143
		 * for fetching the content!
144
		 *
145
		 * This configuration option overwrites the "client/html/catalog/domains"
146
		 * option that allows to configure the domain names of the items fetched
147
		 * for all catalog related data.
148
		 *
149
		 * @param array List of domain names
150
		 * @since 2020.10
151
		 * @see client/html/catalog/domains
152
		 * @see client/html/catalog/detail/domains
153
		 * @see client/html/catalog/stage/domains
154
		 * @see client/html/catalog/lists/domains
155
		 */
156
		$domains = ['catalog', 'media', 'media/property', 'price', 'supplier', 'text', 'product' => ['promotion']];
157
		$domains = $config->get( 'client/html/catalog/domains', $domains );
158
		$domains = $config->get( 'client/html/catalog/home/domains', $domains );
159
160
		/** client/html/catalog/home/basket-add
161
		 * Display the "add to basket" button for each product item in the catalog home component
162
		 *
163
		 * Enables the button for adding products to the basket for the listed products.
164
		 * This works for all type of products, even for selection products with product
165
		 * variants and product bundles. By default, also optional attributes are
166
		 * displayed if they have been associated to a product.
167
		 *
168
		 * @param boolean True to display the button, false to hide it
169
		 * @since 2020.10
170
		 * @see client/html/catalog/lists/basket-add
171
		 * @see client/html/catalog/detail/basket-add
172
		 * @see client/html/basket/related/basket-add
173
		 * @see client/html/catalog/product/basket-add
174
		 */
175
		if( $config->get( 'client/html/catalog/home/basket-add', false ) ) {
176
			$domains = array_merge_recursive( $domains, ['attribute' => ['variant', 'custom', 'config']] );
177
		}
178
179
		return $domains;
180
	}
181
182
183
	/**
184
	 * Returns the list of stock URLs for the given products
185
	 *
186
	 * @param \Aimeos\Map $products List of products
187
	 * @return \Aimeos\Map List of stock URLs
188
	 */
189
	protected function stockUrl( \Aimeos\Map $products ) : \Aimeos\Map
190
	{
191
		$articles = map();
192
		$config = $this->context()->config();
193
194
		if( $config->get( 'client/html/catalog/home/basket-add', false ) )
195
		{
196
			foreach( $products as $product )
197
			{
198
				if( $product->getType() === 'select' ) {
199
					$articles->union( $product->getRefItems( 'product', 'default', 'default' ) );
200
				}
201
			}
202
		}
203
204
		/** client/html/catalog/home/stock/enable
205
		 * Enables or disables displaying product stock levels in product list views
206
		 *
207
		 * This configuration option allows shop owners to display product
208
		 * stock levels for each product in list views or to disable
209
		 * fetching product stock information.
210
		 *
211
		 * The stock information is fetched via AJAX and inserted via Javascript.
212
		 * This allows to cache product items by leaving out such highly
213
		 * dynamic content like stock levels which changes with each order.
214
		 *
215
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
216
		 * @since 2020.10
217
		 * @see client/html/catalog/detail/stock/enable
218
		 * @see client/html/catalog/stock/url/target
219
		 * @see client/html/catalog/stock/url/controller
220
		 * @see client/html/catalog/stock/url/action
221
		 * @see client/html/catalog/stock/url/config
222
		 */
223
		$enabled = $config->get( 'client/html/catalog/home/stock/enable', true );
224
225
		if( !$enabled || $products->isEmpty() ) {
226
			return map();
227
		}
228
229
		return $this->getStockUrl( $this->view(), $products->union( $articles ) );
230
	}
231
232
233
	/** client/html/catalog/home/template-body
234
	 * Relative path to the HTML body template of the catalog home client.
235
	 *
236
	 * The template file contains the HTML code and processing instructions
237
	 * to generate the result shown in the body of the frontend. The
238
	 * configuration string is the path to the template file relative
239
	 * to the templates directory (usually in client/html/templates).
240
	 *
241
	 * You can overwrite the template file configuration in extensions and
242
	 * provide alternative templates. These alternative templates should be
243
	 * named like the default one but suffixed by
244
	 * an unique name. You may use the name of your project for this. If
245
	 * you've implemented an alternative client class as well, it
246
	 * should be suffixed by the name of the new class.
247
	 *
248
	 * @param string Relative path to the template creating code for the HTML page body
249
	 * @since 2020.10
250
	 * @see client/html/catalog/home/template-header
251
	 */
252
253
	/** client/html/catalog/home/template-header
254
	 * Relative path to the HTML header template of the catalog home client.
255
	 *
256
	 * The template file contains the HTML code and processing instructions
257
	 * to generate the HTML code that is inserted into the HTML page header
258
	 * of the rendered page in the frontend. The configuration string is the
259
	 * path to the template file relative to the templates directory (usually
260
	 * in client/html/templates).
261
	 *
262
	 * You can overwrite the template file configuration in extensions and
263
	 * provide alternative templates. These alternative templates should be
264
	 * named like the default one but suffixed by
265
	 * an unique name. You may use the name of your project for this. If
266
	 * you've implemented an alternative client class as well, it
267
	 * should be suffixed by the name of the new class.
268
	 *
269
	 * @param string Relative path to the template creating code for the HTML page head
270
	 * @since 2020.10
271
	 * @see client/html/catalog/home/template-body
272
	 */
273
}
274