Standard   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 329
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 47
dl 0
loc 329
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A compare() 0 4 1
A getPath() 0 20 5
A find() 0 3 1
A __construct() 0 6 1
A __clone() 0 4 1
A parse() 0 7 2
A search() 0 10 1
A getTree() 0 8 1
A root() 0 4 2
A getManager() 0 3 1
A sort() 0 11 3
A slice() 0 5 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2024
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Site;
12
13
14
/**
15
 * Default implementation of the site frontend controller.
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Frontend\Base
22
	implements Iface, \Aimeos\Controller\Frontend\Common\Iface
23
{
24
	/** controller/frontend/site/name
25
	 * Class name of the used site frontend controller implementation
26
	 *
27
	 * Each default frontend controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Frontend\Site\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Frontend\Site\Mysite
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/frontend/site/name = Mysite
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MySite"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2021.04
55
	 * @category Developer
56
	 */
57
58
	/** controller/frontend/site/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the site frontend controllers
60
	 *
61
	 * Decorators extend the functionality of a class by adding new aspects
62
	 * (e.g. log what is currently done), executing the methods of the underlying
63
	 * class only in certain conditions (e.g. only for logged in users) or
64
	 * modify what is returned to the caller.
65
	 *
66
	 * This option allows you to remove a decorator added via
67
	 * "controller/frontend/common/decorators/default" before they are wrapped
68
	 * around the frontend controller.
69
	 *
70
	 *  controller/frontend/site/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\Controller\Frontend\Common\Decorator\*") added via
74
	 * "controller/frontend/common/decorators/default" for the site frontend controller.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2021.04
78
	 * @category Developers
79
	 * @see controller/frontend/common/decorators/default
80
	 * @see controller/frontend/site/decorators/global
81
	 * @see controller/frontend/site/decorators/local
82
	 */
83
84
	/** controller/frontend/site/decorators/global
85
	 * Adds a list of globally available decorators only to the site frontend controllers
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\Controller\Frontend\Common\Decorator\*") around the frontend controller.
94
	 *
95
	 *  controller/frontend/site/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator1" only to the frontend controller.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2021.04
102
	 * @category Developers
103
	 * @see controller/frontend/common/decorators/default
104
	 * @see controller/frontend/site/decorators/excludes
105
	 * @see controller/frontend/site/decorators/local
106
	 */
107
108
	/** controller/frontend/site/decorators/local
109
	 * Adds a list of local decorators only to the site frontend controllers
110
	 *
111
	 * Decorators extend the functionality of a class by adding new aspects
112
	 * (e.g. log what is currently done), executing the methods of the underlying
113
	 * class only in certain conditions (e.g. only for logged in users) or
114
	 * modify what is returned to the caller.
115
	 *
116
	 * This option allows you to wrap local decorators
117
	 * ("\Aimeos\Controller\Frontend\Site\Decorator\*") around the frontend controller.
118
	 *
119
	 *  controller/frontend/site/decorators/local = array( 'decorator2' )
120
	 *
121
	 * This would add the decorator named "decorator2" defined by
122
	 * "\Aimeos\Controller\Frontend\Site\Decorator\Decorator2" only to the frontend
123
	 * controller.
124
	 *
125
	 * @param array List of decorator names
126
	 * @since 2021.04
127
	 * @category Developers
128
	 * @see controller/frontend/common/decorators/default
129
	 * @see controller/frontend/site/decorators/excludes
130
	 * @see controller/frontend/site/decorators/global
131
	 */
132
133
134
	 private \Aimeos\MShop\Common\Manager\Iface $manager;
135
	 private \Aimeos\Base\Criteria\Iface $filter;
136
	private ?string $root = null;
137
138
139
	/**
140
	 * Common initialization for controller classes
141
	 *
142
	 * @param \Aimeos\MShop\ContextIface $context Common MShop context object
143
	 */
144
	public function __construct( \Aimeos\MShop\ContextIface $context )
145
	{
146
		parent::__construct( $context );
147
148
		$this->manager = \Aimeos\MShop::create( $context, 'locale/site' );
149
		$this->filter = $this->manager->filter( true );
150
	}
151
152
153
	/**
154
	 * Clones objects in controller
155
	 */
156
	public function __clone()
157
	{
158
		$this->filter = clone $this->filter;
159
		parent::__clone();
160
	}
161
162
163
	/**
164
	 * Adds generic condition for filtering attributes
165
	 *
166
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
167
	 * @param string $key Search key defined by the site manager, e.g. "site.status"
168
	 * @param array|string $value Value or list of values to compare to
169
	 * @return \Aimeos\Controller\Frontend\Site\Iface Site controller for fluent interface
170
	 * @since 2021.04
171
	 */
172
	public function compare( string $operator, string $key, $value ) : Iface
173
	{
174
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
175
		return $this;
176
	}
177
178
179
	/**
180
	 * Returns the category for the given site code
181
	 *
182
	 * @param string $code Unique site code
183
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface Site item
184
	 * @since 2021.04
185
	 */
186
	public function find( string $code ) : \Aimeos\MShop\Locale\Item\Site\Iface
187
	{
188
		return $this->manager->find( $code, [], null, null, null );
189
	}
190
191
192
	/**
193
	 * Returns the category for the given site ID
194
	 *
195
	 * @param string $id Unique site ID
196
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface Site item
197
	 * @since 2021.04
198
	 */
199
	public function get( string $id ) : \Aimeos\MShop\Locale\Item\Site\Iface
200
	{
201
		return $this->manager->get( $id, [], null );
202
	}
203
204
205
	/**
206
	 * Returns the list of sites up to the root node including the node given by its ID
207
	 *
208
	 * @param string $id Current category ID
209
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface[] Associative list of sites
210
	 * @since 2021.04
211
	 */
212
	public function getPath( string $id )
213
	{
214
		$list = $this->manager->getPath( $id, [] );
215
216
		if( $list->isAvailable()->search( false ) ) {
217
			throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Site is not available' ) );
218
		}
219
220
		if( $this->root )
221
		{
222
			foreach( $list as $key => $item )
223
			{
224
				if( $key == $this->root ) {
225
					break;
226
				}
227
				unset( $list[$key] );
228
			}
229
		}
230
231
		return $list;
232
	}
233
234
235
	/**
236
	 * Returns the sites filtered by the previously assigned conditions
237
	 *
238
	 * @param int $level Tree level constant, e.g. ONE, LIST or TREE
239
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface Site tree
240
	 * @since 2021.04
241
	 */
242
	public function getTree( int $level = Iface::TREE ) : \Aimeos\MShop\Locale\Item\Site\Iface
243
	{
244
		$filter = clone $this->filter;
245
246
		$this->addExpression( $filter->getConditions() );
247
		$filter->add( $filter->and( $this->getConditions() ) );
248
249
		return $this->manager->getTree( $this->root, [], $level, $filter );
250
	}
251
252
253
	/**
254
	 * Parses the given array and adds the conditions to the list of conditions
255
	 *
256
	 * @param array $conditions List of conditions, e.g. ['>' => ['site.status' => 0]]
257
	 * @return \Aimeos\Controller\Frontend\Site\Iface Site controller for fluent interface
258
	 * @since 2021.04
259
	 */
260
	public function parse( array $conditions ) : Iface
261
	{
262
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
263
			$this->addExpression( $cond );
264
		}
265
266
		return $this;
267
	}
268
269
270
	/**
271
	 * Sets the site ID of node that is used as root node
272
	 *
273
	 * @param string|null $id Site ID
274
	 * @return \Aimeos\Controller\Frontend\Site\Iface Site controller for fluent interface
275
	 * @since 2021.04
276
	 */
277
	public function root( string $id = null ) : Iface
278
	{
279
		$this->root = ( $id ? $id : null );
280
		return $this;
281
	}
282
283
284
	/**
285
	 * Returns the sites filtered by the previously assigned conditions
286
	 *
287
	 * @param int &$total Parameter where the total number of found sites will be stored in
288
	 * @return \Aimeos\Map Ordered list of site items implementing \Aimeos\MShop\Locale\Item\Site\Iface
289
	 * @since 2021.04
290
	 */
291
	public function search( int &$total = null ) : \Aimeos\Map
292
	{
293
		$filter = clone $this->filter;
294
295
		$this->addExpression( $filter->getConditions() );
296
297
		$filter->add( $filter->and( $this->getConditions() ) );
298
		$filter->setSortations( $this->getSortations() );
299
300
		return $this->manager->search( $filter, [], $total );
301
	}
302
303
304
	/**
305
	 * Sets the start value and the number of returned products for slicing the list of found products
306
	 *
307
	 * @param int $start Start value of the first product in the list
308
	 * @param int $limit Number of returned products
309
	 * @return \Aimeos\Controller\Frontend\Site\Iface Site controller for fluent interface
310
	 * @since 2021.04
311
	 */
312
	public function slice( int $start, int $limit ) : Iface
313
	{
314
		$maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 );
315
		$this->filter->slice( $start, min( $limit, $maxsize ) );
316
		return $this;
317
	}
318
319
320
	/**
321
	 * Sets the sorting of the result list
322
	 *
323
	 * @param string|null $key Search key for sorting of the result list and null for no sorting
324
	 * @return \Aimeos\Controller\Frontend\Site\Iface Site controller for fluent interface
325
	 * @since 2021.04
326
	 */
327
	public function sort( ?string $key = null ) : Iface
328
	{
329
		$list = $this->splitKeys( $key );
330
331
		foreach( $list as $sortkey )
332
		{
333
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
334
			$this->addExpression( $this->filter->sort( $direction, ltrim( $sortkey, '+-' ) ) );
335
		}
336
337
		return $this;
338
	}
339
340
341
	/**
342
	 * Returns the manager used by the controller
343
	 *
344
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
345
	 */
346
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
347
	{
348
		return $this->manager;
349
	}
350
}
351