Passed
Push — master ( 9a0039...2f021f )
by Aimeos
07:58
created

Standard   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
eloc 41
c 2
b 0
f 0
dl 0
loc 183
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 7 2
A get() 0 3 1
A __construct() 0 7 1
A parse() 0 7 2
A product() 0 7 2
A search() 0 6 1
A compare() 0 4 1
A __clone() 0 3 1
A sort() 0 21 4
A slice() 0 5 1
A getManager() 0 3 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Stock;
12
13
14
/**
15
 * Default implementation of the stock 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
27
28
	/**
29
	 * Common initialization for controller classes
30
	 *
31
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
32
	 */
33
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
34
	{
35
		parent::__construct( $context );
36
37
		$this->manager = \Aimeos\MShop::create( $context, 'stock' );
38
		$this->filter = $this->manager->filter( true );
39
		$this->addExpression( $this->filter->getConditions() );
40
	}
41
42
43
	/**
44
	 * Clones objects in controller and resets values
45
	 */
46
	public function __clone()
47
	{
48
		$this->filter = clone $this->filter;
49
	}
50
51
52
	/**
53
	 * Adds the IDs of the products for filtering
54
	 *
55
	 * @param array|string $ids Codes of the products
56
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
57
	 * @since 2021.01
58
	 */
59
	public function product( $ids ) : Iface
60
	{
61
		if( !empty( $ids ) ) {
62
			$this->addExpression( $this->filter->compare( '==', 'stock.productid', $ids ) );
63
		}
64
65
		return $this;
66
	}
67
68
69
	/**
70
	 * Adds generic condition for filtering
71
	 *
72
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
73
	 * @param string $key Search key defined by the stock manager, e.g. "stock.dateback"
74
	 * @param array|string $value Value or list of values to compare to
75
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
76
	 * @since 2019.04
77
	 */
78
	public function compare( string $operator, string $key, $value ) : Iface
79
	{
80
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
81
		return $this;
82
	}
83
84
85
	/**
86
	 * Returns the stock item for the given stock ID
87
	 *
88
	 * @param string $id Unique stock ID
89
	 * @return \Aimeos\MShop\Stock\Item\Iface Stock item
90
	 * @since 2019.04
91
	 */
92
	public function get( string $id ) : \Aimeos\MShop\Stock\Item\Iface
93
	{
94
		return $this->manager->get( $id, [], true );
95
	}
96
97
98
	/**
99
	 * Parses the given array and adds the conditions to the list of conditions
100
	 *
101
	 * @param array $conditions List of conditions, e.g. ['>' => ['stock.dateback' => '2000-01-01 00:00:00']]
102
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
103
	 * @since 2019.04
104
	 */
105
	public function parse( array $conditions ) : Iface
106
	{
107
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
108
			$this->addExpression( $cond );
109
		}
110
111
		return $this;
112
	}
113
114
115
	/**
116
	 * Returns the stock items filtered by the previously assigned conditions
117
	 *
118
	 * @param int &$total Parameter where the total number of found stock items will be stored in
119
	 * @return \Aimeos\Map Ordered list of stock items implementing \Aimeos\MShop\Stock\Item\Iface
120
	 * @since 2019.04
121
	 */
122
	public function search( int &$total = null ) : \Aimeos\Map
123
	{
124
		$this->filter->setSortations( $this->getSortations() );
125
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) );
126
127
		return $this->manager->search( $this->filter, [], $total );
128
	}
129
130
131
	/**
132
	 * Sets the start value and the number of returned stock items for slicing the list of found stock items
133
	 *
134
	 * @param integer $start Start value of the first stock item in the list
135
	 * @param integer $limit Number of returned stock items
136
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
137
	 * @since 2019.04
138
	 */
139
	public function slice( int $start, int $limit ) : Iface
140
	{
141
		$maxsize = $this->getContext()->config()->get( 'controller/frontend/common/max-size', 250 );
142
		$this->filter->slice( $start, min( $limit, $maxsize ) );
143
		return $this;
144
	}
145
146
147
	/**
148
	 * Sets the sorting of the result list
149
	 *
150
	 * @param string|null $key Sorting of the result list like "stock.type", null for no sorting
151
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
152
	 * @since 2019.04
153
	 */
154
	public function sort( string $key = null ) : Iface
155
	{
156
		$list = $this->splitKeys( $key );
157
158
		foreach( $list as $sortkey )
159
		{
160
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
161
			$sortkey = ltrim( $sortkey, '+-' );
162
163
			switch( $sortkey )
164
			{
165
				case 'stock':
166
					$this->addExpression( $this->filter->sort( $direction, 'stock.type' ) );
167
					$this->addExpression( $this->filter->sort( $direction, 'stock.stocklevel' ) );
168
					break;
169
				default:
170
					$this->addExpression( $this->filter->sort( $direction, $sortkey ) );
171
				}
172
		}
173
174
		return $this;
175
	}
176
177
178
	/**
179
	 * Adds stock types for filtering
180
	 *
181
	 * @param array|string $types Stock type codes
182
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
183
	 * @since 2019.04
184
	 */
185
	public function type( $types ) : Iface
186
	{
187
		if( !empty( $types ) ) {
188
			$this->addExpression( $this->filter->compare( '==', 'stock.type', $types ) );
189
		}
190
191
		return $this;
192
	}
193
194
195
	/**
196
	 * Returns the manager used by the controller
197
	 *
198
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
199
	 */
200
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
201
	{
202
		return $this->manager;
203
	}
204
}
205