Completed
Push — master ( bae9ca...97eeac )
by Aimeos
02:43
created

Standard::searchListItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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
22
	implements Iface, \Aimeos\Controller\Frontend\Common\Iface
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
		$item->setId( null );
38
		$manager->saveItem( $item );
39
40
		return $item;
41
	}
42
43
44
	/**
45
	 * Creates a new customer item object pre-filled with the given values but not yet stored
46
	 *
47
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item
48
	 */
49
	public function createItem( array $values = [] )
50
	{
51
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
52
53
		$item = $manager->createItem();
54
		$item->fromArray( $values );
55
		$item->setId( null );
56
		$item->setStatus( 1 );
57
58
		return $item;
59
	}
60
61
62
	/**
63
	 * Deletes a customer item that belongs to the current authenticated user
64
	 *
65
	 * @param string $id Unique customer ID
66
	 * @since 2017.04
67
	 */
68
	public function deleteItem( $id )
69
	{
70
		$this->checkUser( $id );
71
72
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
73
		$manager->deleteItem( $id );
74
	}
75
76
77
	/**
78
	 * Updates the customer item identified by its ID
79
	 *
80
	 * @param string $id Unique customer ID
81
	 * @param array $values Values added to the customer item like "customer.birthday" or "customer.city"
82
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item
83
	 * @since 2017.04
84
	 */
85
	public function editItem( $id, array $values )
86
	{
87
		$this->checkUser( $id );
88
89
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
90
91
		$item = $manager->getItem( $id, [], true );
92
		$item->fromArray( $values );
93
		$item->setId( $id );
94
		$manager->saveItem( $item );
95
96
		return $item;
97
	}
98
99
100
	/**
101
	 * Returns the customer item for the given customer ID
102
	 *
103
	 * @param string|null $id Unique customer ID or null for current customer item
104
	 * @param string[] $domains Domain names of items that are associated with the customers and that should be fetched too
105
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items
106
	 * @since 2017.04
107
	 */
108
	public function getItem( $id = null, array $domains = [] )
109
	{
110
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
111
112
		if( $id == null ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $id of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
113
			return $manager->getItem( $this->getContext()->getUserId(), $domains, true );
114
		}
115
116
		$this->checkUser( $id );
117
118
		return $manager->getItem( $id, $domains, true );
119
	}
120
121
122
	/**
123
	 * Returns the customer item for the given customer code (usually e-mail address)
124
	 *
125
	 * This method doesn't check if the customer item belongs to the logged in user!
126
	 *
127
	 * @param string $code Unique customer code
128
	 * @param string[] $domains Domain names of items that are associated with the customers and that should be fetched too
129
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items
130
	 * @since 2017.04
131
	 */
132
	public function findItem( $code, array $domains = [] )
133
	{
134
		return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' )->findItem( $code, $domains );
135
	}
136
137
138
	/**
139
	 * Stores a modified customer item
140
	 *
141
	 * @param \Aimeos\MShop\Customer\Item\Iface Customer item
142
	 */
143
	public function saveItem( \Aimeos\MShop\Customer\Item\Iface $item )
144
	{
145
		\Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' )->saveItem( $item );
146
	}
147
148
149
	/**
150
	 * Creates and returns a new item object
151
	 *
152
	 * @param array $values Values added to the newly created customer item like "customer.birthday"
153
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
154
	 * @since 2017.04
155
	 */
156
	public function addAddressItem( array $values )
157
	{
158
		$context = $this->getContext();
159
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/address' );
160
161
		$item = $manager->createItem();
162
		$item->fromArray( $values );
163
		$item->setId( null );
164
		$item->setParentId( $context->getUserId() );
165
166
		$manager->saveItem( $item );
167
168
		return $item;
169
	}
170
171
172
	/**
173
	 * Creates a new customer address item object pre-filled with the given values but not yet stored
174
	 *
175
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
176
	 */
177
	public function createAddressItem( array $values = [] )
178
	{
179
		$context = $this->getContext();
180
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/address' );
181
182
		$item = $manager->createItem();
183
		$item->fromArray( $values );
184
		$item->setId( null );
185
186
		$item->setParentId( $context->getUserId() );
187
188
		return $item;
189
	}
190
191
192
	/**
193
	 * Deletes a customer item that belongs to the current authenticated user
194
	 *
195
	 * @param string $id Unique customer address ID
196
	 * @since 2017.04
197
	 */
198
	public function deleteAddressItem( $id )
199
	{
200
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
201
202
		$this->checkUser( $manager->getItem( $id, [], true )->getParentId() );
203
204
		$manager->deleteItem( $id );
205
	}
206
207
208
	/**
209
	 * Saves a modified customer item object
210
	 *
211
	 * @param string $id Unique customer address ID
212
	 * @param array $values Values added to the customer item like "customer.address.city"
213
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
214
	 * @since 2017.04
215
	 */
216
	public function editAddressItem( $id, array $values )
217
	{
218
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
219
220
		$item = $manager->getItem( $id, [], true );
221
		$this->checkUser( $item->getParentId() );
222
223
		$item->fromArray( $values );
224
		$item->setId( $id );
225
226
		$manager->saveItem( $item );
227
228
		return $item;
229
	}
230
231
232
	/**
233
	 * Returns the customer item for the given customer ID
234
	 *
235
	 * @param string $id Unique customer address ID
236
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
237
	 * @since 2017.04
238
	 */
239
	public function getAddressItem( $id )
240
	{
241
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
242
243
		$item = $manager->getItem( $id );
244
		$this->checkUser( $item->getParentId() );
245
246
		return $item;
247
	}
248
249
250
	/**
251
	 * Stores a modified customer address item
252
	 *
253
	 * @param \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
254
	 */
255
	public function saveAddressItem( \Aimeos\MShop\Customer\Item\Address\Iface $item )
256
	{
257
		\Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' )->saveItem( $item );
258
	}
259
260
261
	/**
262
	 * Creates and returns a new list item object
263
	 *
264
	 * @param array $values Values added to the newly created customer item like "customer.lists.refid"
265
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Customer lists item
266
	 * @since 2017.06
267
	 */
268
	public function addListItem( array $values )
269
	{
270
		$context = $this->getContext();
271
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
272
273
		if( !isset( $values['customer.lists.typeid'] ) )
274
		{
275
			if( !isset( $values['customer.lists.type'] ) ) {
276
				throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists type code' ) );
277
			}
278
279
			if( !isset( $values['customer.lists.domain'] ) ) {
280
				throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists domain' ) );
281
			}
282
283
			$typeManager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists/type' );
284
			$typeItem = $typeManager->findItem( $values['customer.lists.type'], [], $values['customer.lists.domain'] );
285
			$values['customer.lists.typeid'] = $typeItem->getId();
286
		}
287
288
		$item = $manager->createItem();
289
		$item->fromArray( $values );
290
		$item->setId( null );
291
		$item->setParentId( $context->getUserId() );
292
293
		$manager->saveItem( $item );
294
295
		return $item;
296
	}
297
298
299
	/**
300
	 * Returns a new customer lists filter criteria object
301
	 *
302
	 * @return \Aimeos\MW\Criteria\Iface New filter object
303
	 * @since 2017.06
304
	 */
305
	public function createListsFilter()
306
	{
307
		$context = $this->getContext();
308
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
309
310
		$filter = $manager->createSearch();
311
		$filter->setConditions( $filter->compare( '==', 'customer.lists.parentid', $context->getUserId() ) );
312
313
		return $filter;
314
	}
315
316
317
	/**
318
	 * Deletes a customer item that belongs to the current authenticated user
319
	 *
320
	 * @param string $id Unique customer address ID
321
	 * @since 2017.06
322
	 */
323
	public function deleteListItem( $id )
324
	{
325
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/lists' );
326
327
		$this->checkUser( $manager->getItem( $id )->getParentId() );
328
329
		$manager->deleteItem( $id );
330
	}
331
332
333
	/**
334
	 * Saves a modified customer lists item object
335
	 *
336
	 * @param string $id Unique customer lists ID
337
	 * @param array $values Values added to the customer lists item like "customer.lists.refid"
338
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Customer lists item
339
	 * @since 2017.06
340
	 */
341
	public function editListItem( $id, array $values )
342
	{
343
		$context = $this->getContext();
344
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
345
346
		$item = $manager->getItem( $id, [], true );
347
		$this->checkUser( $item->getParentId() );
348
349
		if( !isset( $values['customer.lists.typeid'] ) )
350
		{
351
			if( !isset( $values['customer.lists.type'] ) ) {
352
				throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists type code' ) );
353
			}
354
355
			if( !isset( $values['customer.lists.domain'] ) ) {
356
				throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists domain' ) );
357
			}
358
359
			$typeManager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists/type' );
360
			$typeItem = $typeManager->findItem( $values['customer.lists.type'], [], $values['customer.lists.domain'] );
361
			$values['customer.lists.typeid'] = $typeItem->getId();
362
		}
363
364
		$item->fromArray( $values );
365
		$item->setId( $id );
366
367
		$manager->saveItem( $item );
368
369
		return $item;
370
	}
371
372
373
	/**
374
	 * Returns the customer item for the given customer ID
375
	 *
376
	 * @param string $id Unique customer address ID
377
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
378
	 * @since 2017.06
379
	 */
380
	public function getListItem( $id )
381
	{
382
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/lists' );
383
		$item = $manager->getItem( $id );
384
385
		$this->checkUser( $item->getParentId() );
386
387
		return $item;
388
	}
389
390
391
	/**
392
	 * Returns the customer lists items filtered by the given criteria
393
	 *
394
	 * @param \Aimeos\MW\Criteria\Iface $filter Criteria object which contains the filter conditions
395
	 * @param integer &$total Parameter where the total number of found attributes will be stored in
396
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface[] Customer list items
397
	 * @since 2017.06
398
	 */
399
	public function searchListItems( \Aimeos\MW\Criteria\Iface $filter, &$total = null )
400
	{
401
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/lists' );
402
403
		return $manager->searchItems( $filter, [], $total );
404
	}
405
406
407
	/**
408
	 * Checks if the current user is allowed to retrieve the customer data for the given ID
409
	 *
410
	 * @param string $id Unique customer ID
411
	 * @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed
412
	 */
413
	protected function checkUser( $id )
414
	{
415
		if( $id != $this->getContext()->getUserId() )
416
		{
417
			$msg = sprintf( 'Not allowed to access customer data for ID "%1$s"', $id );
418
			throw new \Aimeos\Controller\Frontend\Customer\Exception( $msg );
419
		}
420
	}
421
}
422