Completed
Push — master ( 9109fd...709bda )
by Aimeos
02:33
created

Standard   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 188
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __clone() 0 4 1
A code() 0 8 2
A compare() 0 5 1
A get() 0 4 1
A find() 0 4 1
A parse() 0 5 1
A search() 0 5 1
A slice() 0 5 1
A sort() 0 27 5
A type() 0 8 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2018
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 $conditions = [];
25
	private $filter;
26
	private $manager;
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, 'stock' );
39
		$this->filter = $this->manager->createSearch( true );
40
		$this->conditions[] = $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 the SKUs of the products for filtering
55
	 *
56
	 * @param array|string $codes Codes of the products
57
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
58
	 * @since 2019.04
59
	 */
60
	public function code( $codes )
61
	{
62
		if( !empty( $codes ) ) {
63
			$this->conditions[] = $this->filter->compare( '==', 'stock.productcode', $codes );
64
		}
65
66
		return $this;
67
	}
68
69
70
	/**
71
	 * Adds generic condition for filtering
72
	 *
73
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
74
	 * @param string $key Search key defined by the stock manager, e.g. "stock.dateback"
75
	 * @param array|string $value Value or list of values to compare to
76
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
77
	 * @since 2019.04
78
	 */
79
	public function compare( $operator, $key, $value )
80
	{
81
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
82
		return $this;
83
	}
84
85
86
	/**
87
	 * Returns the stock item for the given stock ID
88
	 *
89
	 * @param string $id Unique stock ID
90
	 * @return \Aimeos\MShop\Stock\Item\Iface Stock item
91
	 * @since 2019.04
92
	 */
93
	public function get( $id )
94
	{
95
		return $this->manager->getItem( $id, [], true );
96
	}
97
98
99
	/**
100
	 * Returns the stock item for the given SKU and type
101
	 *
102
	 * @param string $code Unique stock code
103
	 * @param string $type Type assigned to the stock item
104
	 * @return \Aimeos\MShop\Stock\Item\Iface Stock item
105
	 * @since 2019.04
106
	 */
107
	public function find( $code, $type )
108
	{
109
		return $this->manager->findItem( $code, [], 'product', $type, true );
110
	}
111
112
113
	/**
114
	 * Parses the given array and adds the conditions to the list of conditions
115
	 *
116
	 * @param array $conditions List of conditions, e.g. ['>' => ['stock.dateback' => '2000-01-01 00:00:00']]
117
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
118
	 * @since 2019.04
119
	 */
120
	public function parse( array $conditions )
121
	{
122
		$this->conditions[] = $this->filter->toConditions( $conditions );
123
		return $this;
124
	}
125
126
127
	/**
128
	 * Returns the stock items filtered by the previously assigned conditions
129
	 *
130
	 * @param integer &$total Parameter where the total number of found stock items will be stored in
131
	 * @return \Aimeos\MShop\Stock\Item\Iface[] Ordered list of stock items
132
	 * @since 2019.04
133
	 */
134
	public function search( &$total = null )
135
	{
136
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
137
		return $this->manager->searchItems( $this->filter, [], $total );
138
	}
139
140
141
	/**
142
	 * Sets the start value and the number of returned stock items for slicing the list of found stock items
143
	 *
144
	 * @param integer $start Start value of the first stock item in the list
145
	 * @param integer $limit Number of returned stock items
146
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
147
	 * @since 2019.04
148
	 */
149
	public function slice( $start, $limit )
150
	{
151
		$this->filter->setSlice( $start, $limit );
152
		return $this;
153
	}
154
155
156
	/**
157
	 * Sets the sorting of the result list
158
	 *
159
	 * @param string|null $key Sorting of the result list like "stock.type", null for no sorting
160
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
161
	 * @since 2019.04
162
	 */
163
	public function sort( $key = null )
164
	{
165
		$direction = '+';
166
167
		if( $key != null && $key[0] === '-' )
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $key of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
168
		{
169
			$key = substr( $key, 1 );
170
			$direction = '-';
171
		}
172
173
		switch( $key )
174
		{
175
			case null:
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $key of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
176
				$this->filter->setSortations( [] );
177
				break;
178
			case 'stock':
179
				$this->filter->setSortations( [
180
					$this->filter->sort( $direction, 'stock.type' ),
181
					$this->filter->sort( $direction, 'stock.stocklevel' )
182
				] );
183
				break;
184
			default:
185
				$this->filter->setSortations( [$this->filter->sort( $direction, $key )] );
186
		}
187
188
		return $this;
189
	}
190
191
192
	/**
193
	 * Adds stock types for filtering
194
	 *
195
	 * @param array|string $types Stock type codes
196
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
197
	 * @since 2019.04
198
	 */
199
	public function type( $types )
200
	{
201
		if( !empty( $types ) ) {
202
			$this->conditions[] = $this->filter->compare( '==', 'stock.type', $types );
203
		}
204
205
		return $this;
206
	}
207
}
208