Passed
Push — master ( 45752d...ccdd85 )
by Aimeos
24:15 queued 21:34
created

Standard::uses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2024
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
	/** controller/frontend/stock/name
25
	 * Class name of the used stock 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\Stock\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Frontend\Stock\Mystock
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/frontend/stock/name = Mystock
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 "MyStock"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2017.03
55
	 * @category Developer
56
	 */
57
58
	/** controller/frontend/stock/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the stock 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/stock/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 stock frontend controller.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2017.03
78
	 * @category Developers
79
	 * @see controller/frontend/common/decorators/default
80
	 * @see controller/frontend/stock/decorators/global
81
	 * @see controller/frontend/stock/decorators/local
82
	 */
83
84
	/** controller/frontend/stock/decorators/global
85
	 * Adds a list of globally available decorators only to the stock 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/stock/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 2017.03
102
	 * @category Developers
103
	 * @see controller/frontend/common/decorators/default
104
	 * @see controller/frontend/stock/decorators/excludes
105
	 * @see controller/frontend/stock/decorators/local
106
	 */
107
108
	/** controller/frontend/stock/decorators/local
109
	 * Adds a list of local decorators only to the stock 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\Stock\Decorator\*") around the frontend controller.
118
	 *
119
	 *  controller/frontend/stock/decorators/local = array( 'decorator2' )
120
	 *
121
	 * This would add the decorator named "decorator2" defined by
122
	 * "\Aimeos\Controller\Frontend\Stock\Decorator\Decorator2" only to the frontend
123
	 * controller.
124
	 *
125
	 * @param array List of decorator names
126
	 * @since 2017.03
127
	 * @category Developers
128
	 * @see controller/frontend/common/decorators/default
129
	 * @see controller/frontend/stock/decorators/excludes
130
	 * @see controller/frontend/stock/decorators/global
131
	 */
132
133
134
	 private \Aimeos\MShop\Common\Manager\Iface $manager;
135
	 private \Aimeos\Base\Criteria\Iface $filter;
136
	private array $domains = [];
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, 'stock' );
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 the IDs of the products for filtering
165
	 *
166
	 * @param array|string $ids Codes of the products
167
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
168
	 * @since 2021.01
169
	 */
170
	public function product( $ids ) : Iface
171
	{
172
		if( !empty( $ids ) ) {
173
			$this->addExpression( $this->filter->compare( '==', 'stock.productid', $ids ) );
174
		}
175
176
		return $this;
177
	}
178
179
180
	/**
181
	 * Adds generic condition for filtering
182
	 *
183
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
184
	 * @param string $key Search key defined by the stock manager, e.g. "stock.dateback"
185
	 * @param array|string $value Value or list of values to compare to
186
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
187
	 * @since 2019.04
188
	 */
189
	public function compare( string $operator, string $key, $value ) : Iface
190
	{
191
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
192
		return $this;
193
	}
194
195
196
	/**
197
	 * Returns the stock item for the given stock ID
198
	 *
199
	 * @param string $id Unique stock ID
200
	 * @return \Aimeos\MShop\Stock\Item\Iface Stock item
201
	 * @since 2019.04
202
	 */
203
	public function get( string $id ) : \Aimeos\MShop\Stock\Item\Iface
204
	{
205
		return $this->manager->get( $id, $this->domains, null );
206
	}
207
208
209
	/**
210
	 * Parses the given array and adds the conditions to the list of conditions
211
	 *
212
	 * @param array $conditions List of conditions, e.g. ['>' => ['stock.dateback' => '2000-01-01 00:00:00']]
213
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
214
	 * @since 2019.04
215
	 */
216
	public function parse( array $conditions ) : Iface
217
	{
218
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
219
			$this->addExpression( $cond );
220
		}
221
222
		return $this;
223
	}
224
225
226
	/**
227
	 * Returns the stock items filtered by the previously assigned conditions
228
	 *
229
	 * @param int &$total Parameter where the total number of found stock items will be stored in
230
	 * @return \Aimeos\Map Ordered list of stock items implementing \Aimeos\MShop\Stock\Item\Iface
231
	 * @since 2019.04
232
	 */
233
	public function search( int &$total = null ) : \Aimeos\Map
234
	{
235
		$filter = clone $this->filter;
236
237
		$this->addExpression( $filter->getConditions() );
238
239
		$filter->setSortations( $this->getSortations() );
240
		$filter->add( $filter->and( $this->getConditions() ) );
241
242
		return $this->manager->search( $filter, $this->domains, $total );
243
	}
244
245
246
	/**
247
	 * Sets the start value and the number of returned stock items for slicing the list of found stock items
248
	 *
249
	 * @param integer $start Start value of the first stock item in the list
250
	 * @param integer $limit Number of returned stock items
251
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
252
	 * @since 2019.04
253
	 */
254
	public function slice( int $start, int $limit ) : Iface
255
	{
256
		$maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 );
257
		$this->filter->slice( $start, min( $limit, $maxsize ) );
258
		return $this;
259
	}
260
261
262
	/**
263
	 * Sets the sorting of the result list
264
	 *
265
	 * @param string|null $key Sorting of the result list like "stock.type", null for no sorting
266
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
267
	 * @since 2019.04
268
	 */
269
	public function sort( string $key = null ) : Iface
270
	{
271
		$list = $this->splitKeys( $key );
272
273
		foreach( $list as $sortkey )
274
		{
275
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
276
			$sortkey = ltrim( $sortkey, '+-' );
277
278
			switch( $sortkey )
279
			{
280
				case 'stock':
281
					$this->addExpression( $this->filter->sort( $direction, 'stock.type' ) );
282
					$this->addExpression( $this->filter->sort( $direction, 'stock.stocklevel' ) );
283
					break;
284
				default:
285
					$this->addExpression( $this->filter->sort( $direction, $sortkey ) );
286
				}
287
		}
288
289
		return $this;
290
	}
291
292
293
	/**
294
	 * Adds stock types for filtering
295
	 *
296
	 * @param array|string $types Stock type codes
297
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
298
	 * @since 2019.04
299
	 */
300
	public function type( $types ) : Iface
301
	{
302
		if( !empty( $types ) ) {
303
			$this->addExpression( $this->filter->compare( '==', 'stock.type', $types ) );
304
		}
305
306
		return $this;
307
	}
308
309
310
	/**
311
	 * Sets the referenced domains that will be fetched too when retrieving items
312
	 *
313
	 * @param array $domains Domain names of the referenced items that should be fetched too
314
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
315
	 * @since 2024.10
316
	 */
317
	public function uses( array $domains ) : Iface
318
	{
319
		$this->domains = $domains;
320
		return $this;
321
	}
322
323
324
	/**
325
	 * Returns the manager used by the controller
326
	 *
327
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
328
	 */
329
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
330
	{
331
		return $this->manager;
332
	}
333
}
334