Passed
Push — master ( 365418...ec127b )
by Aimeos
02:19
created

Standard   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
eloc 41
c 1
b 0
f 0
dl 0
loc 199
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A compare() 0 4 1
A parse() 0 7 2
A search() 0 6 1
A __construct() 0 7 1
A getTree() 0 4 1
A root() 0 4 2
A getPath() 0 20 5
A find() 0 3 1
A sort() 0 11 3
A slice() 0 4 1
A __clone() 0 3 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
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
	private $filter;
25
	private $manager;
26
	private $root;
27
28
29
	/**
30
	 * Common initialization for controller classes
31
	 *
32
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
33
	 */
34
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
35
	{
36
		parent::__construct( $context );
37
38
		$this->manager = \Aimeos\MShop::create( $context, 'locale/site' );
39
		$this->filter = $this->manager->filter( true );
40
		$this->addExpression( $this->filter->getConditions() );
41
	}
42
43
44
	/**
45
	 * Clones objects in controller and resets values
46
	 */
47
	public function __clone()
48
	{
49
		$this->filter = clone $this->filter;
50
	}
51
52
53
	/**
54
	 * Adds generic condition for filtering attributes
55
	 *
56
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
57
	 * @param string $key Search key defined by the site manager, e.g. "site.status"
58
	 * @param array|string $value Value or list of values to compare to
59
	 * @return \Aimeos\Controller\Frontend\Site\Iface Site controller for fluent interface
60
	 * @since 2021.04
61
	 */
62
	public function compare( string $operator, string $key, $value ) : Iface
63
	{
64
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
65
		return $this;
66
	}
67
68
69
	/**
70
	 * Returns the category for the given site code
71
	 *
72
	 * @param string $code Unique site code
73
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface Site item
74
	 * @since 2021.04
75
	 */
76
	public function find( string $code ) : \Aimeos\MShop\Locale\Item\Site\Iface
77
	{
78
		return $this->manager->find( $code, [], null, null, true );
79
	}
80
81
82
	/**
83
	 * Returns the category for the given site ID
84
	 *
85
	 * @param string $id Unique site ID
86
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface Site item
87
	 * @since 2021.04
88
	 */
89
	public function get( string $id ) : \Aimeos\MShop\Locale\Item\Site\Iface
90
	{
91
		return $this->manager->get( $id, [], true );
92
	}
93
94
95
	/**
96
	 * Returns the list of sites up to the root node including the node given by its ID
97
	 *
98
	 * @param string $id Current category ID
99
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface[] Associative list of sites
100
	 * @since 2021.04
101
	 */
102
	public function getPath( string $id )
103
	{
104
		$list = $this->manager->getPath( $id, [] );
105
106
		if( $list->isAvailable()->search( false ) ) {
107
			throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Site is not available' ) );
108
		}
109
110
		if( $this->root )
111
		{
112
			foreach( $list as $key => $item )
113
			{
114
				if( $key == $this->root ) {
115
					break;
116
				}
117
				unset( $list[$key] );
118
			}
119
		}
120
121
		return $list;
122
	}
123
124
125
	/**
126
	 * Returns the sites filtered by the previously assigned conditions
127
	 *
128
	 * @param int $level Tree level constant, e.g. ONE, LIST or TREE
129
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface Site tree
130
	 * @since 2021.04
131
	 */
132
	public function getTree( int $level = Iface::TREE ) : \Aimeos\MShop\Locale\Item\Site\Iface
133
	{
134
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) );
135
		return $this->manager->getTree( $this->root, [], $level, $this->filter );
136
	}
137
138
139
	/**
140
	 * Parses the given array and adds the conditions to the list of conditions
141
	 *
142
	 * @param array $conditions List of conditions, e.g. ['>' => ['site.status' => 0]]
143
	 * @return \Aimeos\Controller\Frontend\Site\Iface Site controller for fluent interface
144
	 * @since 2021.04
145
	 */
146
	public function parse( array $conditions ) : Iface
147
	{
148
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
149
			$this->addExpression( $cond );
150
		}
151
152
		return $this;
153
	}
154
155
156
	/**
157
	 * Sets the site ID of node that is used as root node
158
	 *
159
	 * @param string|null $id Site ID
160
	 * @return \Aimeos\Controller\Frontend\Site\Iface Site controller for fluent interface
161
	 * @since 2021.04
162
	 */
163
	public function root( string $id = null ) : Iface
164
	{
165
		$this->root = ( $id ? $id : null );
166
		return $this;
167
	}
168
169
170
	/**
171
	 * Returns the sites filtered by the previously assigned conditions
172
	 *
173
	 * @param int &$total Parameter where the total number of found sites will be stored in
174
	 * @return \Aimeos\Map Ordered list of site items implementing \Aimeos\MShop\Locale\Item\Site\Iface
175
	 * @since 2021.04
176
	 */
177
	public function search( int &$total = null ) : \Aimeos\Map
178
	{
179
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) );
180
		$this->filter->setSortations( $this->getSortations() );
181
182
		return $this->manager->search( $this->filter, [], $total );
183
	}
184
185
186
	/**
187
	 * Sets the start value and the number of returned products for slicing the list of found products
188
	 *
189
	 * @param int $start Start value of the first product in the list
190
	 * @param int $limit Number of returned products
191
	 * @return \Aimeos\Controller\Frontend\Site\Iface Site controller for fluent interface
192
	 * @since 2021.04
193
	 */
194
	public function slice( int $start, int $limit ) : Iface
195
	{
196
		$this->filter->slice( $start, $limit );
197
		return $this;
198
	}
199
200
201
	/**
202
	 * Sets the sorting of the result list
203
	 *
204
	 * @param string|null $key Search key for sorting of the result list and null for no sorting
205
	 * @return \Aimeos\Controller\Frontend\Site\Iface Site controller for fluent interface
206
	 * @since 2021.04
207
	 */
208
	public function sort( ?string $key = null ) : Iface
209
	{
210
		$list = $this->splitKeys( $key );
211
212
		foreach( $list as $sortkey )
213
		{
214
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
215
			$this->addExpression( $this->filter->sort( $direction, ltrim( $sortkey, '+-' ) ) );
216
		}
217
218
		return $this;
219
	}
220
}
221