Completed
Push — master ( 9d3f5c...f136ad )
by Aimeos
02:22
created

Standard   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 0
loc 247
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A addItem() 0 10 1
A createItem() 0 10 1
A deleteItem() 0 7 1
A editItem() 0 12 1
A getItem() 0 12 2
A findItem() 0 4 1
A saveItem() 0 4 1
A addAddressItem() 0 12 1
A createAddressItem() 0 11 1
A deleteAddressItem() 0 8 1
A editAddressItem() 0 12 1
A getAddressItem() 0 9 1
A saveAddressItem() 0 4 1
A checkUser() 0 8 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Customer;
12
13
14
/**
15
 * Default implementation of the customer frontend controller
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Frontend\Base
2 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 Iface, \Aimeos\Controller\Frontend\Common\Iface
1 ignored issue
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	/**
25
	 * Adds and returns a new customer item object
26
	 *
27
	 * @param array $values Values added to the newly created customer item like "customer.birthday"
28
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item
29
	 * @since 2017.04
30
	 */
31
	public function addItem( array $values )
32
	{
33
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
34
35
		$item = $manager->createItem();
36
		$item->fromArray( $values );
37
		$manager->saveItem( $item );
38
39
		return $item;
40
	}
41
42
43
	/**
44
	 * Creates a new customer item object pre-filled with the given values but not yet stored
45
	 *
46
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item
47
	 */
48
	public function createItem( array $values = [] )
49
	{
50
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
51
52
		$item = $manager->createItem();
53
		$item->fromArray( $values );
54
		$item->setStatus( 1 );
55
56
		return $item;
57
	}
58
59
60
	/**
61
	 * Deletes a customer item that belongs to the current authenticated user
62
	 *
63
	 * @param string $id Unique customer ID
64
	 * @since 2017.04
65
	 */
66
	public function deleteItem( $id )
67
	{
68
		$this->checkUser( $id );
69
70
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
71
		$manager->deleteItem( $id );
72
	}
73
74
75
	/**
76
	 * Updates the customer item identified by its ID
77
	 *
78
	 * @param string $id Unique customer ID
79
	 * @param array $values Values added to the customer item like "customer.birthday" or "customer.city"
80
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item
81
	 * @since 2017.04
82
	 */
83
	public function editItem( $id, array $values )
84
	{
85
		$this->checkUser( $id );
86
87
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
88
89
		$item = $manager->getItem( $id, [], true );
0 ignored issues
show
Unused Code introduced by
The call to Iface::getItem() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
90
		$item->fromArray( $values );
91
		$manager->saveItem( $item );
92
93
		return $item;
94
	}
95
96
97
	/**
98
	 * Returns the customer item for the given customer ID
99
	 *
100
	 * @param string|null $id Unique customer ID or null for current customer item
101
	 * @param string[] $domains Domain names of items that are associated with the customers and that should be fetched too
102
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items
103
	 * @since 2017.04
104
	 */
105
	public function getItem( $id = null, array $domains = [] )
106
	{
107
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
108
109
		if( $id === null ) {
110
			return $manager->getItem( $this->getContext()->getUserId(), $domains, true );
0 ignored issues
show
Unused Code introduced by
The call to Iface::getItem() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
111
		}
112
113
		$this->checkUser( $id );
114
115
		return $manager->getItem( $id, $domains, true );
0 ignored issues
show
Unused Code introduced by
The call to Iface::getItem() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116
	}
117
118
119
	/**
120
	 * Returns the customer item for the given customer code (usually e-mail address)
121
	 *
122
	 * This method doesn't check if the customer item belongs to the logged in user!
123
	 *
124
	 * @param string $code Unique customer code
125
	 * @param string[] $domains Domain names of items that are associated with the customers and that should be fetched too
126
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items
127
	 * @since 2017.04
128
	 */
129
	public function findItem( $code, array $domains = [] )
130
	{
131
		return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' )->findItem( $code, $domains );
132
	}
133
134
135
	/**
136
	 * Stores a modified customer item
137
	 *
138
	 * @param \Aimeos\MShop\Customer\Item\Iface Customer item
139
	 */
140
	public function saveItem( \Aimeos\MShop\Customer\Item\Iface $item )
141
	{
142
		\Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' )->saveItem( $item );
143
	}
144
145
146
	/**
147
	 * Creates and returns a new item object
148
	 *
149
	 * @param array $values Values added to the newly created customer item like "customer.birthday"
150
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer address item
151
	 * @since 2017.04
152
	 */
153
	public function addAddressItem( array $values )
154
	{
155
		$context = $this->getContext();
156
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/address' );
157
158
		$item = $manager->createItem();
159
		$item->fromArray( $values );
160
		$item->setParentId( $context->getUserId() );
161
		$manager->saveItem( $item );
162
163
		return $item;
164
	}
165
166
167
	/**
168
	 * Creates a new customer address item object pre-filled with the given values but not yet stored
169
	 *
170
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
171
	 */
172
	public function createAddressItem( array $values = [] )
173
	{
174
		$context = $this->getContext();
175
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/address' );
176
177
		$item = $manager->createItem();
178
		$item->fromArray( $values );
179
		$item->setParentId( $context->getUserId() );
180
181
		return $item;
182
	}
183
184
185
	/**
186
	 * Deletes a customer item that belongs to the current authenticated user
187
	 *
188
	 * @param string $id Unique customer address ID
189
	 * @since 2017.04
190
	 */
191
	public function deleteAddressItem( $id )
192
	{
193
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
194
195
		$this->checkUser( $manager->getItem( $id, [], true )->getParentId() );
0 ignored issues
show
Unused Code introduced by
The call to Iface::getItem() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
196
197
		$manager->deleteItem( $id );
198
	}
199
200
201
	/**
202
	 * Saves a modified customer item object
203
	 *
204
	 * @param string $id Unique customer address ID
205
	 * @param array $values Values added to the customer item like "customer.address.city"
206
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer address item
207
	 * @since 2017.04
208
	 */
209
	public function editAddressItem( $id, array $values )
210
	{
211
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
212
213
		$item = $manager->getItem( $id, [], true );
0 ignored issues
show
Unused Code introduced by
The call to Iface::getItem() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
214
		$this->checkUser( $item->getParentId() );
215
216
		$item->fromArray( $values );
217
		$manager->saveItem( $item );
218
219
		return $item;
220
	}
221
222
223
	/**
224
	 * Returns the customer item for the given customer ID
225
	 *
226
	 * @param string $id Unique customer address ID
227
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
228
	 * @since 2017.04
229
	 */
230
	public function getAddressItem( $id )
231
	{
232
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
233
		$item = $manager->getItem( $id );
234
235
		$this->checkUser( $item->getParentId() );
236
237
		return $item;
238
	}
239
240
241
	/**
242
	 * Stores a modified customer address item
243
	 *
244
	 * @param \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
245
	 */
246
	public function saveAddressItem( \Aimeos\MShop\Customer\Item\Address\Iface $item )
247
	{
248
		\Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' )->saveItem( $item );
249
	}
250
251
252
	/**
253
	 * Checks if the current user is allowed to retrieve the customer data for the given ID
254
	 *
255
	 * @param string $id Unique customer ID
256
	 * @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed
257
	 */
258
	protected function checkUser( $id )
259
	{
260
		if( $id !== $this->getContext()->getUserId() )
261
		{
262
			$msg = sprintf( 'Not allowed to access customer data for ID "%1$s"', $id );
263
			throw new \Aimeos\Controller\Frontend\Customer\Exception( $msg );
264
		}
265
	}
266
}
267