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

Standard::getItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 2
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Standard::cancel() 0 8 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
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 $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, 'subscription' );
39
		$this->filter = $this->manager->createSearch();
40
		$this->conditions[] = $this->filter->compare( '==', 'order.base.customerid', $context->getUserId() );
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
	 * Cancels an active subscription
55
	 *
56
	 * @param string $id Unique subscription ID
57
	 * @return \Aimeos\MShop\Subscription\Item\Iface Canceled subscription item
58
	 */
59
	public function cancel( $id )
60
	{
61
		$item = $this->manager->getItem( $id );
62
		$item = $item->setDateEnd( $item->getDateNext() )
63
			->setReason( \Aimeos\MShop\Subscription\Item\Iface::REASON_CANCEL );
64
65
		return $this->manager->saveItem( $item );
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 subscription manager, e.g. "subscription.status"
74
	 * @param array|string $value Value or list of values to compare to
75
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
76
	 * @since 2019.04
77
	 */
78
	public function compare( $operator, $key, $value )
79
	{
80
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
81
		return $this;
82
	}
83
84
85
	/**
86
	 * Returns the subscription item for the given ID
87
	 *
88
	 * @param string $id Unique subscription ID
89
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription object
90
	 */
91
	public function get( $id )
92
	{
93
		$context = $this->getContext();
94
95
		$filter = $this->manager->createSearch( true );
96
		$expr = [
97
			$filter->compare( '==', 'subscription.id', $id ),
98
			$filter->compare( '==', 'order.base.customerid', $context->getUserId() ),
99
			$filter->getConditions(),
100
		];
101
		$filter->setConditions( $filter->combine( '&&', $expr ) );
102
103
		$items = $this->manager->searchItems( $filter );
104
105
		if( ( $item = reset( $items ) ) === false )
106
		{
107
			$msg = 'Invalid subscription ID "%1$s" for customer ID "%2$s"';
108
			throw new \Aimeos\Controller\Frontend\Subscription\Exception( sprintf( $msg, $id, $context->getUserId() ) );
109
		}
110
111
		return $item;
112
	}
113
114
115
	/**
116
	 * Returns the available interval attribute items
117
	 *
118
	 * @return \Aimeos\MShop\Attribute\Item\Iface[] Associative list of intervals as keys and interval attribute items as values
119
	 */
120
	public function getIntervals()
121
	{
122
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute' );
123
124
		$search = $manager->createSearch( true );
125
		$expr = array(
126
			$search->compare( '==', 'attribute.domain', 'product' ),
127
			$search->compare( '==', 'attribute.type', 'interval' ),
128
			$search->getConditions(),
129
		);
130
		$search->setConditions( $search->combine( '&&', $expr ) );
131
		$search->setSlice( 0, 10000 );
132
133
		$list = [];
134
135
		foreach( $manager->searchItems( $search, ['text'] ) as $attrItem ) {
136
			$list[$attrItem->getCode()] = $attrItem;
137
		}
138
139
		return $list;
140
	}
141
142
143
	/**
144
	 * Parses the given array and adds the conditions to the list of conditions
145
	 *
146
	 * @param array $conditions List of conditions, e.g. ['>' => ['subscription.interval' => 'P0Y1M0W0D']]
147
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
148
	 * @since 2019.04
149
	 */
150
	public function parse( array $conditions )
151
	{
152
		$this->conditions[] = $this->filter->toConditions( $conditions );
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 )
164
	{
165
		return $this->manager->saveItem( $item );
166
	}
167
168
	/**
169
	 * Returns the subscriptions filtered by the previously assigned conditions
170
	 *
171
	 * @param integer &$total Parameter where the total number of found subscriptions will be stored in
172
	 * @return \Aimeos\MShop\Subscription\Item\Iface[] Ordered list of subscription items
173
	 * @since 2019.04
174
	 */
175
	public function search( &$total = null )
176
	{
177
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
178
		return $this->manager->searchItems( $this->filter, [], $total );
179
	}
180
181
182
	/**
183
	 * Sets the start value and the number of returned subscription items for slicing the list of found subscription items
184
	 *
185
	 * @param integer $start Start value of the first subscription item in the list
186
	 * @param integer $limit Number of returned subscription items
187
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
188
	 * @since 2019.04
189
	 */
190
	public function slice( $start, $limit )
191
	{
192
		$this->filter->setSlice( $start, $limit );
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Sets the sorting of the result list
199
	 *
200
	 * @param string|null $key Sorting key of the result list like "interval", null for no sorting
201
	 * @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface
202
	 * @since 2019.04
203
	 */
204
	public function sort( $key = null )
205
	{
206
		$direction = '+';
207
208
		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...
209
		{
210
			$key = substr( $key, 1 );
211
			$direction = '-';
212
		}
213
214
		switch( $key )
215
		{
216
			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...
217
				$this->filter->setSortations( [] );
218
				break;
219
			case 'interval':
220
				$this->filter->setSortations( [$this->filter->sort( $direction, 'subscription.interval' )] );
221
				break;
222
			default:
223
				$this->filter->setSortations( [$this->filter->sort( $direction, $key )] );
224
		}
225
226
		return $this;
227
	}
228
}
229