Completed
Push — master ( 90127a...6c75b7 )
by Aimeos
19:38
created

Standard   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 91
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A begin() 0 17 2
A end() 0 17 2
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 Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Subscription\Process\Processor\Cgroup;
12
13
14
/**
15
 * Customer group processor for subscriptions
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Subscription\Process\Processor\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\Controller\Common\Subscription\Process\Processor\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	/** controller/common/subscription/export/csv/processor/cgroup/name
25
	 * Name of the customer group processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Subscription\Process\Processor\Cgroup\Myname".
28
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
29
	 *
30
	 * @param string Last part of the processor class name
31
	 * @since 2018.04
32
	 * @category Developer
33
	 */
34
35
	private $groupIds;
36
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
42
	 */
43
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
44
	{
45
		parent::__construct( $context );
46
47
		$config = $context->getConfig();
48
49
		/** controller/common/subscription/process/processor/cgroup/groupids
50
		 * List of group IDs that should be added to the customer account
51
		 *
52
		 * After customers bought a subscription, the list of group IDs will be
53
		 * added to their accounts. When the subscription period ends, they will
54
		 * be removed from the customer accounts again.
55
		 *
56
		 * @param array List of customer group IDs
57
		 * @since 2018.04
58
		 * @category Developer
59
		 */
60
		$this->groupIds = (array) $config->get( 'controller/common/subscription/process/processor/cgroup/groupids', [] );
61
	}
62
63
64
	/**
65
	 * Processes the initial subscription
66
	 *
67
	 * @param \Aimeos\MShop\Subscription\Item\Iface $subscription Subscription item
68
	 */
69
	public function begin( \Aimeos\MShop\Subscription\Item\Iface $subscription )
70
	{
71
		if( empty( $this->groupIds ) ) {
72
			return;
73
		}
74
75
		$context = $this->getContext();
76
77
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
78
		$baseManager = \Aimeos\MShop\Factory::createManager( $context, 'order/base' );
79
80
		$baseItem = $baseManager->getItem( $subscription->getOrderBaseId() );
81
		$item = $manager->getItem( $baseItem->getCustomerId(), ['customer/group'] );
82
83
		$item->setGroups( array_unique( array_merge( $item->getGroups(), $this->groupIds ) ) );
84
		$manager->saveItem( $item );
85
	}
86
87
88
	/**
89
	 * Processes the end of the subscription
90
	 *
91
	 * @param \Aimeos\MShop\Subscription\Item\Iface $subscription Subscription item
92
	 */
93
	public function end( \Aimeos\MShop\Subscription\Item\Iface $subscription )
94
	{
95
		if( empty( $this->groupIds ) ) {
96
			return;
97
		}
98
99
		$context = $this->getContext();
100
101
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
102
		$baseManager = \Aimeos\MShop\Factory::createManager( $context, 'order/base' );
103
104
		$baseItem = $baseManager->getItem( $subscription->getOrderBaseId() );
105
		$item = $manager->getItem( $baseItem->getCustomerId(), ['customer/group'] );
106
107
		$item->setGroups( array_diff( $item->getGroups(), $this->groupIds ) );
108
		$manager->saveItem( $item );
109
	}
110
}
111