Completed
Push — master ( afa75e...0bae75 )
by Aimeos
02:14
created

Standard   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 122
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A cancel() 0 9 1
A createFilter() 0 4 1
A getItem() 0 23 2
A getIntervals() 0 20 2
A saveItem() 0 4 1
A searchItems() 0 13 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
	/**
25
	 * Cancels an active subscription
26
	 *
27
	 * @param string $id Unique subscription ID
28
	 * @return \Aimeos\MShop\Subscription\Item\Iface Canceled subscription item
29
	 */
30
	public function cancel( $id )
31
	{
32
		$item = $this->getItem( $id );
33
34
		$interval = \DateInterval::createFromDateString( $item->getInterval() );
35
		$item->setDateEnd( date_create( $item->getDateNext() )->add( $interval )->format( 'Y-m-d' ) );
36
37
		return $this->saveItem( $item );
38
	}
39
40
41
	/**
42
	 * Returns the filter for searching items
43
	 *
44
	 * @return \Aimeos\MW\Criteria\Iface Filter object
45
	 */
46
	public function createFilter()
47
	{
48
		return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'subscription' )->createSearch( true );
49
	}
50
51
52
	/**
53
	 * Returns the subscription item for the given ID
54
	 *
55
	 * @param string $id Unique subscription ID
56
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription object
57
	 */
58
	public function getItem( $id )
59
	{
60
		$context = $this->getContext();
61
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'subscription' );
62
63
		$filter = $manager->createSearch();
64
		$expr = [
65
			$filter->compare( '==', 'subscription.id', $id ),
66
			$filter->compare( '==', 'order.base.customerid', $context->getUserId() ),
67
			$filter->getConditions(),
68
		];
69
		$filter->setConditions( $filter->combine( '&&', $expr ) );
70
71
		$items = $this->searchItems( $filter );
72
73
		if( ( $item = reset( $items ) ) === false )
74
		{
75
			$msg = 'Invalid subscription ID "%1$s" for customer ID "%2$s"';
76
			throw new \Aimeos\Controller\Frontend\Subscription\Exception( sprintf( $msg, $id, $context->getUserId() ) );
77
		}
78
79
		return $item;
80
	}
81
82
83
	/**
84
	 * Returns the available interval attribute items
85
	 *
86
	 * @return \Aimeos\MShop\Attribute\Item\Iface[] Associative list of intervals as keys and interval attribute items as values
87
	 */
88
	public function getIntervals()
89
	{
90
		$intervals = [];
91
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'attribute' );
92
93
		$search = $manager->createSearch( true );
94
		$expr = array(
95
			$search->getConditions(),
96
			$search->compare( '==', 'attribute.domain', 'product' ),
97
			$search->compare( '==', 'attribute.type.code', 'interval' ),
98
		);
99
		$search->setConditions( $search->combine( '&&', $expr ) );
100
		$search->setSlice( 0, 0x7fffffff );
101
102
		foreach( $manager->searchItems( $search, ['text'] ) as $attrItem ) {
103
			$intervals[$attrItem->getCode()] = $attrItem;
104
		}
105
106
		return $intervals;
107
	}
108
109
	/**
110
	 * Saves the modified subscription item
111
	 *
112
	 * @param \Aimeos\MShop\Subscription\Item\Iface $item Subscription object
113
	 * @return \Aimeos\MShop\Subscription\Item\Iface Saved subscription item
114
	 */
115
	public function saveItem( \Aimeos\MShop\Subscription\Item\Iface $item )
116
	{
117
		return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'subscription' )->saveItem( $item );
118
	}
119
120
121
	/**
122
	 * Returns the subscription items based on the given filter that belong to the current user
123
	 *
124
	 * @param \Aimeos\MW\Criteria\Iface Filter object
125
	 * @param integer &$total|null Variable that will contain the total number of available items
126
	 * @return \Aimeos\MShop\Subscription\Item\Iface[] Associative list of IDs as keys and subscription objects as values
127
	 */
128
	public function searchItems( \Aimeos\MW\Criteria\Iface $filter, &$total = null )
129
	{
130
		$context = $this->getContext();
131
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'subscription' );
132
133
		$expr = [
134
			$filter->compare( '==', 'order.base.customerid', $context->getUserId() ),
135
			$filter->getConditions(),
136
		];
137
		$filter->setConditions( $filter->combine( '&&', $expr ) );
138
139
		return $manager->searchItems( $filter, [], $total );
140
	}
141
}
142