Standard::getManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2024
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Subscription;
12
13
14
/**
15
 * Default implementation of the subscription 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/subscription/name
25
	 * Class name of the used subscription 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\Subscription\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Frontend\Subscription\Mysubscription
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/frontend/subscription/name = Mysubscription
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 "MySubscription"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2018.04
55
	 * @category Developer
56
	 */
57
58
	/** controller/frontend/subscription/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the subscription 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/subscription/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 subscription frontend controller.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2018.04
78
	 * @category Developer
79
	 * @see controller/frontend/common/decorators/default
80
	 * @see controller/frontend/subscription/decorators/global
81
	 * @see controller/frontend/subscription/decorators/local
82
	 */
83
84
	/** controller/frontend/subscription/decorators/global
85
	 * Adds a list of globally available decorators only to the subscription 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/subscription/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 2018.04
102
	 * @category Developer
103
	 * @see controller/frontend/common/decorators/default
104
	 * @see controller/frontend/subscription/decorators/excludes
105
	 * @see controller/frontend/subscription/decorators/local
106
	 */
107
108
	/** controller/frontend/subscription/decorators/local
109
	 * Adds a list of local decorators only to the subscription 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\Subscription\Decorator\*") around the frontend controller.
118
	 *
119
	 *  controller/frontend/subscription/decorators/local = array( 'decorator2' )
120
	 *
121
	 * This would add the decorator named "decorator2" defined by
122
	 * "\Aimeos\Controller\Frontend\Catalog\Decorator\Decorator2" only to the frontend
123
	 * controller.
124
	 *
125
	 * @param array List of decorator names
126
	 * @since 2018.04
127
	 * @category Developer
128
	 * @see controller/frontend/common/decorators/default
129
	 * @see controller/frontend/subscription/decorators/excludes
130
	 * @see controller/frontend/subscription/decorators/global
131
	 */
132
133
134
	private array $domains = [];
135
	private \Aimeos\Base\Criteria\Iface $filter;
136
	private \Aimeos\MShop\Common\Manager\Iface $manager;
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, 'subscription' );
149
		$this->filter = $this->manager->filter();
150
		$this->addExpression( $this->filter->compare( '==', 'order.customerid', $context->user() ) );
151
	}
152
153
154
	/**
155
	 * Clones objects in controller
156
	 */
157
	public function __clone()
158
	{
159
		$this->filter = clone $this->filter;
160
		parent::__clone();
161
	}
162
163
164
	/**
165
	 * Cancels an active subscription
166
	 *
167
	 * @param string $id Unique subscription ID
168
	 * @return \Aimeos\MShop\Subscription\Item\Iface Canceled subscription item
169
	 */
170
	public function cancel( string $id ) : \Aimeos\MShop\Subscription\Item\Iface
171
	{
172
		$item = $this->object()->get( $id );
173
174
		$item = $item->setDateEnd( $item->getDateNext() ?: date( 'Y-m-d' ) )
175
			->setReason( \Aimeos\MShop\Subscription\Item\Iface::REASON_CANCEL );
176
177
		return $this->manager->save( $item );
178
	}
179
180
181
	/**
182
	 * Adds generic condition for filtering
183
	 *
184
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
185
	 * @param string $key Search key defined by the subscription manager, e.g. "subscription.status"
186
	 * @param array|string $value Value or list of values to compare to
187
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
188
	 * @since 2019.04
189
	 */
190
	public function compare( string $operator, string $key, $value ) : Iface
191
	{
192
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Returns the subscription item for the given ID
199
	 *
200
	 * @param string $id Unique subscription ID
201
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription object
202
	 */
203
	public function get( string $id ) : \Aimeos\MShop\Subscription\Item\Iface
204
	{
205
		$user = $this->context()->user();
206
207
		$filter = $this->manager->filter( null )->add( [
208
			'order.customerid' => $user,
209
			'subscription.id' => $id
210
		] );
211
212
		return $this->manager->search( $filter, $this->domains )->first( function() use ( $id, $user ) {
213
			$msg = 'Invalid subscription ID "%1$s" for customer ID "%2$s"';
214
			throw new \Aimeos\Controller\Frontend\Subscription\Exception( sprintf( $msg, $id, $user ) );
215
		} );
216
	}
217
218
219
	/**
220
	 * Returns the available interval attribute items
221
	 *
222
	 * @return \Aimeos\Map Associative list of intervals as keys and items implementing \Aimeos\MShop\Attribute\Item\Iface
223
	 */
224
	public function getIntervals() : \Aimeos\Map
225
	{
226
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute' );
227
228
		$search = $manager->filter( true )->add( [
229
			'attribute.domain' => 'product',
230
			'attribute.type' => 'interval'
231
		] )->slice( 0, 10000 );
232
233
		return $manager->search( $search, ['text'] )->col( null, 'attribute.code' );
234
	}
235
236
237
	/**
238
	 * Parses the given array and adds the conditions to the list of conditions
239
	 *
240
	 * @param array $conditions List of conditions, e.g. ['>' => ['subscription.interval' => 'P0Y1M0W0D']]
241
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
242
	 * @since 2019.04
243
	 */
244
	public function parse( array $conditions ) : Iface
245
	{
246
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
247
			$this->addExpression( $cond );
248
		}
249
250
		return $this;
251
	}
252
253
254
	/**
255
	 * Saves the modified subscription item
256
	 *
257
	 * @param \Aimeos\MShop\Subscription\Item\Iface $item Subscription object
258
	 * @return \Aimeos\MShop\Subscription\Item\Iface Saved subscription item
259
	 */
260
	public function save( \Aimeos\MShop\Subscription\Item\Iface $item ) : \Aimeos\MShop\Subscription\Item\Iface
261
	{
262
		return $this->manager->save( $item );
263
	}
264
265
	/**
266
	 * Returns the subscriptions filtered by the previously assigned conditions
267
	 *
268
	 * @param int &$total Parameter where the total number of found subscriptions will be stored in
269
	 * @return \Aimeos\Map Ordered list of subscription items implementing \Aimeos\MShop\Subscription\Item\Iface
270
	 * @since 2019.04
271
	 */
272
	public function search( int &$total = null ) : \Aimeos\Map
273
	{
274
		$filter = clone $this->filter;
275
276
		$filter->setSortations( $this->getSortations() );
277
		$filter->add( $filter->and( $this->getConditions() ) );
278
279
		return $this->manager->search( $filter, $this->domains, $total );
280
	}
281
282
283
	/**
284
	 * Sets the start value and the number of returned subscription items for slicing the list of found subscription items
285
	 *
286
	 * @param int $start Start value of the first subscription item in the list
287
	 * @param int $limit Number of returned subscription items
288
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
289
	 * @since 2019.04
290
	 */
291
	public function slice( int $start, int $limit ) : Iface
292
	{
293
		$maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 );
294
		$this->filter->slice( $start, min( $limit, $maxsize ) );
295
		return $this;
296
	}
297
298
299
	/**
300
	 * Sets the sorting of the result list
301
	 *
302
	 * @param string|null $key Sorting key of the result list like "interval", null for no sorting
303
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
304
	 * @since 2019.04
305
	 */
306
	public function sort( string $key = null ) : Iface
307
	{
308
		$list = $this->splitKeys( $key );
309
310
		foreach( $list as $sortkey )
311
		{
312
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
313
			$sortkey = ltrim( $sortkey, '+-' );
314
315
			switch( $sortkey )
316
			{
317
				case 'interval':
318
					$this->addExpression( $this->filter->sort( $direction, 'subscription.interval' ) ); break;
319
				default:
320
					$this->addExpression( $this->filter->sort( $direction, $sortkey ) );
321
			}
322
		}
323
324
		return $this;
325
	}
326
327
328
	/**
329
	 * Sets the referenced domains that will be fetched too when retrieving items
330
	 *
331
	 * @param array $domains Domain names of the referenced items that should be fetched too
332
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
333
	 * @since 2022.04
334
	 */
335
	public function uses( array $domains ) : Iface
336
	{
337
		$this->domains = $domains;
338
		return $this;
339
	}
340
341
342
	/**
343
	 * Returns the manager used by the controller
344
	 *
345
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
346
	 */
347
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
348
	{
349
		return $this->manager;
350
	}
351
}
352