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

Standard::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2021
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
	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, 'subscription' );
38
		$this->filter = $this->manager->filter();
39
		$this->addExpression( $this->filter->compare( '==', 'order.base.customerid', $context->getUserId() ) );
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
	 * Cancels an active subscription
54
	 *
55
	 * @param string $id Unique subscription ID
56
	 * @return \Aimeos\MShop\Subscription\Item\Iface Canceled subscription item
57
	 */
58
	public function cancel( string $id ) : \Aimeos\MShop\Subscription\Item\Iface
59
	{
60
		$item = $this->manager->get( $id );
61
		$item = $item->setDateEnd( $item->getDateNext() )
62
			->setReason( \Aimeos\MShop\Subscription\Item\Iface::REASON_CANCEL );
63
64
		return $this->manager->save( $item );
65
	}
66
67
68
	/**
69
	 * Adds generic condition for filtering
70
	 *
71
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
72
	 * @param string $key Search key defined by the subscription manager, e.g. "subscription.status"
73
	 * @param array|string $value Value or list of values to compare to
74
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
75
	 * @since 2019.04
76
	 */
77
	public function compare( string $operator, string $key, $value ) : Iface
78
	{
79
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
80
		return $this;
81
	}
82
83
84
	/**
85
	 * Returns the subscription item for the given ID
86
	 *
87
	 * @param string $id Unique subscription ID
88
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription object
89
	 */
90
	public function get( string $id ) : \Aimeos\MShop\Subscription\Item\Iface
91
	{
92
		$context = $this->getContext();
93
94
		$filter = $this->manager->filter( true );
95
		$expr = [
96
			$filter->compare( '==', 'subscription.id', $id ),
97
			$filter->compare( '==', 'order.base.customerid', $context->getUserId() ),
98
			$filter->getConditions(),
99
		];
100
		$filter->setConditions( $filter->and( $expr ) );
101
102
		if( ( $item = $this->manager->search( $filter )->first() ) === null )
103
		{
104
			$msg = 'Invalid subscription ID "%1$s" for customer ID "%2$s"';
105
			throw new \Aimeos\Controller\Frontend\Subscription\Exception( sprintf( $msg, $id, $context->getUserId() ) );
106
		}
107
108
		return $item;
109
	}
110
111
112
	/**
113
	 * Returns the available interval attribute items
114
	 *
115
	 * @return \Aimeos\Map Associative list of intervals as keys and items implementing \Aimeos\MShop\Attribute\Item\Iface
116
	 */
117
	public function getIntervals() : \Aimeos\Map
118
	{
119
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute' );
120
121
		$search = $manager->filter( true );
122
		$expr = array(
123
			$search->compare( '==', 'attribute.domain', 'product' ),
124
			$search->compare( '==', 'attribute.type', 'interval' ),
125
			$search->getConditions(),
126
		);
127
		$search->setConditions( $search->and( $expr ) );
128
		$search->slice( 0, 10000 );
129
130
		$list = [];
131
132
		foreach( $manager->search( $search, ['text'] ) as $attrItem ) {
133
			$list[$attrItem->getCode()] = $attrItem;
134
		}
135
136
		return map( $list );
137
	}
138
139
140
	/**
141
	 * Parses the given array and adds the conditions to the list of conditions
142
	 *
143
	 * @param array $conditions List of conditions, e.g. ['>' => ['subscription.interval' => 'P0Y1M0W0D']]
144
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
145
	 * @since 2019.04
146
	 */
147
	public function parse( array $conditions ) : Iface
148
	{
149
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
150
			$this->addExpression( $cond );
151
		}
152
153
		return $this;
154
	}
155
156
157
	/**
158
	 * Saves the modified subscription item
159
	 *
160
	 * @param \Aimeos\MShop\Subscription\Item\Iface $item Subscription object
161
	 * @return \Aimeos\MShop\Subscription\Item\Iface Saved subscription item
162
	 */
163
	public function save( \Aimeos\MShop\Subscription\Item\Iface $item ) : \Aimeos\MShop\Subscription\Item\Iface
164
	{
165
		return $this->manager->save( $item );
166
	}
167
168
	/**
169
	 * Returns the subscriptions filtered by the previously assigned conditions
170
	 *
171
	 * @param int &$total Parameter where the total number of found subscriptions will be stored in
172
	 * @return \Aimeos\Map Ordered list of subscription items implementing \Aimeos\MShop\Subscription\Item\Iface
173
	 * @since 2019.04
174
	 */
175
	public function search( int &$total = null ) : \Aimeos\Map
176
	{
177
		$this->filter->setSortations( $this->getSortations() );
178
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) );
179
180
		return $this->manager->search( $this->filter, [], $total );
181
	}
182
183
184
	/**
185
	 * Sets the start value and the number of returned subscription items for slicing the list of found subscription items
186
	 *
187
	 * @param int $start Start value of the first subscription item in the list
188
	 * @param int $limit Number of returned subscription items
189
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
190
	 * @since 2019.04
191
	 */
192
	public function slice( int $start, int $limit ) : Iface
193
	{
194
		$maxsize = $this->getContext()->config()->get( 'controller/frontend/common/max-size', 250 );
195
		$this->filter->slice( $start, min( $limit, $maxsize ) );
196
		return $this;
197
	}
198
199
200
	/**
201
	 * Sets the sorting of the result list
202
	 *
203
	 * @param string|null $key Sorting key of the result list like "interval", null for no sorting
204
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
205
	 * @since 2019.04
206
	 */
207
	public function sort( string $key = null ) : Iface
208
	{
209
		$list = $this->splitKeys( $key );
210
211
		foreach( $list as $sortkey )
212
		{
213
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
214
			$sortkey = ltrim( $sortkey, '+-' );
215
216
			switch( $sortkey )
217
			{
218
				case 'interval':
219
					$this->addExpression( $this->filter->sort( $direction, 'subscription.interval' ) ); break;
220
				default:
221
					$this->addExpression( $this->filter->sort( $direction, $sortkey ) );
222
			}
223
		}
224
225
		return $this;
226
	}
227
228
229
	/**
230
	 * Returns the manager used by the controller
231
	 *
232
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
233
	 */
234
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
235
	{
236
		return $this->manager;
237
	}
238
}
239