Completed
Push — master ( d26f8a...61077e )
by Aimeos
07:27
created

Standard::editAddressItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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
		$context = $this->getContext();
34
		$config = $context->getConfig();
35
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
36
37
		$values = $this->addItemDefaults( $values );
38
		$item = $manager->createItem();
39
		$item->fromArray( $values );
40
		$item->setId( null );
41
42
		/** controller/frontend/customer/groupids
43
		 * List of groups new customers should be assigned to
44
		 *
45
		 * Newly created customers will be assigned automatically to the groups
46
		 * given by their IDs. This is especially useful if those groups limit
47
		 * functionality for those users.
48
		 *
49
		 * @param array List of group IDs
50
		 * @since 2017.07
51
		 * @category User
52
		 * @category Developer
53
		 */
54
		$gids = $config->get( 'client/html/checkout/standard/order/account/standard/groupids', [] ); // @deprecated
55
		$item->setGroups( (array) $config->get( 'controller/frontend/customer/groupids', $gids ) );
56
57
		$item = $manager->saveItem( $item );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $item is correct as $manager->saveItem($item) (which targets Aimeos\MShop\Common\Manager\Iface::saveItem()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
59
		$msg = $item->toArray();
0 ignored issues
show
Bug introduced by
The method toArray cannot be called on $item (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
60
		$msg['customer.password'] = $values['customer.password'];
61
		$context->getMessageQueue( 'mq-email', 'customer/email/account' )->add( json_encode( $msg ) );
62
63
		return $item;
64
	}
65
66
67
	/**
68
	 * Creates a new customer item object pre-filled with the given values but not yet stored
69
	 *
70
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item
71
	 */
72
	public function createItem( array $values = [] )
73
	{
74
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
75
76
		$item = $manager->createItem();
77
		$item->fromArray( $values );
78
		$item->setId( null );
79
		$item->setStatus( 1 );
80
81
		return $item;
82
	}
83
84
85
	/**
86
	 * Deletes a customer item that belongs to the current authenticated user
87
	 *
88
	 * @param string $id Unique customer ID
89
	 * @since 2017.04
90
	 */
91
	public function deleteItem( $id )
92
	{
93
		$this->checkUser( $id );
94
95
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
96
		$manager->deleteItem( $id );
97
	}
98
99
100
	/**
101
	 * Updates the customer item identified by its ID
102
	 *
103
	 * @param string $id Unique customer ID
104
	 * @param array $values Values added to the customer item like "customer.birthday" or "customer.city"
105
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item
106
	 * @since 2017.04
107
	 */
108
	public function editItem( $id, array $values )
109
	{
110
		$this->checkUser( $id );
111
112
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
113
		$item = $manager->getItem( $id, [], true );
114
115
		unset( $values['customer.id'] );
116
		$item->fromArray( $values );
117
118
		return $manager->saveItem( $item );
119
	}
120
121
122
	/**
123
	 * Returns the customer item for the given customer ID
124
	 *
125
	 * @param string|null $id Unique customer ID or null for current customer item
126
	 * @param string[] $domains Domain names of items that are associated with the customers and that should be fetched too
127
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items
128
	 * @since 2017.04
129
	 */
130
	public function getItem( $id = null, array $domains = [] )
131
	{
132
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' );
133
134
		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...
135
			return $manager->getItem( $this->getContext()->getUserId(), $domains, true );
136
		}
137
138
		$this->checkUser( $id );
139
140
		return $manager->getItem( $id, $domains, true );
141
	}
142
143
144
	/**
145
	 * Returns the customer item for the given customer code (usually e-mail address)
146
	 *
147
	 * This method doesn't check if the customer item belongs to the logged in user!
148
	 *
149
	 * @param string $code Unique customer code
150
	 * @param string[] $domains Domain names of items that are associated with the customers and that should be fetched too
151
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items
152
	 * @since 2017.04
153
	 */
154
	public function findItem( $code, array $domains = [] )
155
	{
156
		return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' )->findItem( $code, $domains );
157
	}
158
159
160
	/**
161
	 * Stores a modified customer item
162
	 *
163
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item
164
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item including the generated ID
165
	 */
166
	public function saveItem( \Aimeos\MShop\Customer\Item\Iface $item )
167
	{
168
		return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' )->saveItem( $item );
169
	}
170
171
172
	/**
173
	 * Creates and returns a new item object
174
	 *
175
	 * @param array $values Values added to the newly created customer item like "customer.birthday"
176
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
177
	 * @since 2017.04
178
	 */
179
	public function addAddressItem( array $values )
180
	{
181
		$context = $this->getContext();
182
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/address' );
183
184
		$item = $manager->createItem();
185
		$item->fromArray( $values );
186
		$item->setId( null );
187
		$item->setParentId( $context->getUserId() );
188
189
		return $manager->saveItem( $item );
190
	}
191
192
193
	/**
194
	 * Creates a new customer address item object pre-filled with the given values but not yet stored
195
	 *
196
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
197
	 */
198
	public function createAddressItem( array $values = [] )
199
	{
200
		$context = $this->getContext();
201
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/address' );
202
203
		$item = $manager->createItem();
204
		$item->fromArray( $values );
205
		$item->setId( null );
206
207
		$item->setParentId( $context->getUserId() );
208
209
		return $item;
210
	}
211
212
213
	/**
214
	 * Deletes a customer item that belongs to the current authenticated user
215
	 *
216
	 * @param string $id Unique customer address ID
217
	 * @since 2017.04
218
	 */
219
	public function deleteAddressItem( $id )
220
	{
221
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
222
223
		$this->checkUser( $manager->getItem( $id, [], true )->getParentId() );
224
225
		$manager->deleteItem( $id );
226
	}
227
228
229
	/**
230
	 * Saves a modified customer item object
231
	 *
232
	 * @param string $id Unique customer address ID
233
	 * @param array $values Values added to the customer item like "customer.address.city"
234
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
235
	 * @since 2017.04
236
	 */
237
	public function editAddressItem( $id, array $values )
238
	{
239
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
240
241
		$item = $manager->getItem( $id, [], true );
242
		$this->checkUser( $item->getParentId() );
243
244
		unset( $values['customer.address.id'] );
245
		$item->fromArray( $values );
246
247
		return $manager->saveItem( $item );
248
	}
249
250
251
	/**
252
	 * Returns the customer item for the given customer ID
253
	 *
254
	 * @param string $id Unique customer address ID
255
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
256
	 * @since 2017.04
257
	 */
258
	public function getAddressItem( $id )
259
	{
260
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' );
261
262
		$item = $manager->getItem( $id );
263
		$this->checkUser( $item->getParentId() );
264
265
		return $item;
266
	}
267
268
269
	/**
270
	 * Stores a modified customer address item
271
	 *
272
	 * @param \Aimeos\MShop\Customer\Item\Address\Iface $item Customer address item
273
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item including the generated ID
274
	 */
275
	public function saveAddressItem( \Aimeos\MShop\Customer\Item\Address\Iface $item )
276
	{
277
		return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' )->saveItem( $item );
278
	}
279
280
281
	/**
282
	 * Creates and returns a new list item object
283
	 *
284
	 * @param array $values Values added to the newly created customer item like "customer.lists.refid"
285
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Customer lists item
286
	 * @since 2017.06
287
	 */
288
	public function addListItem( array $values )
289
	{
290
		$context = $this->getContext();
291
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
292
293
		if( !isset( $values['customer.lists.typeid'] ) )
294
		{
295
			if( !isset( $values['customer.lists.type'] ) ) {
296
				throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists type code' ) );
297
			}
298
299
			if( !isset( $values['customer.lists.domain'] ) ) {
300
				throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists domain' ) );
301
			}
302
303
			$typeManager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists/type' );
304
			$typeItem = $typeManager->findItem( $values['customer.lists.type'], [], $values['customer.lists.domain'] );
305
			$values['customer.lists.typeid'] = $typeItem->getId();
306
		}
307
308
		$item = $manager->createItem();
309
		$item->fromArray( $values );
310
		$item->setId( null );
311
		$item->setParentId( $context->getUserId() );
312
313
		return $manager->saveItem( $item );
314
	}
315
316
317
	/**
318
	 * Returns a new customer lists filter criteria object
319
	 *
320
	 * @return \Aimeos\MW\Criteria\Iface New filter object
321
	 * @since 2017.06
322
	 */
323
	public function createListsFilter()
324
	{
325
		$context = $this->getContext();
326
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
327
328
		$filter = $manager->createSearch();
329
		$filter->setConditions( $filter->compare( '==', 'customer.lists.parentid', $context->getUserId() ) );
330
331
		return $filter;
332
	}
333
334
335
	/**
336
	 * Deletes a customer item that belongs to the current authenticated user
337
	 *
338
	 * @param string $id Unique customer address ID
339
	 * @since 2017.06
340
	 */
341
	public function deleteListItem( $id )
342
	{
343
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/lists' );
344
345
		$this->checkUser( $manager->getItem( $id )->getParentId() );
346
347
		$manager->deleteItem( $id );
348
	}
349
350
351
	/**
352
	 * Saves a modified customer lists item object
353
	 *
354
	 * @param string $id Unique customer lists ID
355
	 * @param array $values Values added to the customer lists item like "customer.lists.refid"
356
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Customer lists item
357
	 * @since 2017.06
358
	 */
359
	public function editListItem( $id, array $values )
360
	{
361
		$context = $this->getContext();
362
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
363
364
		$item = $manager->getItem( $id, [], true );
365
		$this->checkUser( $item->getParentId() );
366
367
		if( !isset( $values['customer.lists.typeid'] ) )
368
		{
369
			if( !isset( $values['customer.lists.type'] ) ) {
370
				throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists type code' ) );
371
			}
372
373
			if( !isset( $values['customer.lists.domain'] ) ) {
374
				throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'No customer lists domain' ) );
375
			}
376
377
			$typeManager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists/type' );
378
			$typeItem = $typeManager->findItem( $values['customer.lists.type'], [], $values['customer.lists.domain'] );
379
			$values['customer.lists.typeid'] = $typeItem->getId();
380
		}
381
382
		unset( $values['customer.lists.id'] );
383
		$item->fromArray( $values );
384
385
		return $manager->saveItem( $item );
386
	}
387
388
389
	/**
390
	 * Returns the customer item for the given customer ID
391
	 *
392
	 * @param string $id Unique customer address ID
393
	 * @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item
394
	 * @since 2017.06
395
	 */
396
	public function getListItem( $id )
397
	{
398
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/lists' );
399
		$item = $manager->getItem( $id );
400
401
		$this->checkUser( $item->getParentId() );
402
403
		return $item;
404
	}
405
406
407
	/**
408
	 * Returns the customer lists items filtered by the given criteria
409
	 *
410
	 * @param \Aimeos\MW\Criteria\Iface $filter Criteria object which contains the filter conditions
411
	 * @param integer &$total Parameter where the total number of found attributes will be stored in
412
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface[] Customer list items
413
	 * @since 2017.06
414
	 */
415
	public function searchListItems( \Aimeos\MW\Criteria\Iface $filter, &$total = null )
416
	{
417
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/lists' );
418
419
		return $manager->searchItems( $filter, [], $total );
420
	}
421
422
423
	/**
424
	 * Adds the default values for customer items if not yet available
425
	 *
426
	 * @param string[] $values Associative list of customer keys (e.g. "customer.label") and their values
427
	 * @return string[] Associative list of customer key/value pairs with default values set
428
	 */
429
	protected function addItemDefaults( array $values )
430
	{
431
		if( !isset( $values['customer.label'] ) || $values['customer.label'] == '' )
432
		{
433
			$label = '';
434
435
			if( isset( $values['customer.lastname'] ) ) {
436
				$label = $values['customer.lastname'];
437
			}
438
439
			if( isset( $values['customer.firstname'] ) && $values['customer.firstname'] != '' ) {
440
				$label = $values['customer.firstname'] . ' ' . $label;
441
			}
442
443
			if( isset( $values['customer.company'] ) && $values['customer.company'] != '' ) {
444
				$label .= ' (' . $values['customer.company'] . ')';
445
			}
446
447
			$values['customer.label'] = $label;
448
		}
449
450
		if( !isset( $values['customer.code'] ) ) {
451
			$values['customer.code'] = $values['customer.email'];
452
		}
453
454
		if( !isset( $values['customer.status'] ) ) {
455
			$values['customer.status'] = 1;
456
		}
457
458
		if( !isset( $values['customer.password'] ) ) {
459
			$values['customer.password'] = substr( md5( microtime( true ) . getmypid() . rand() ), -8 );
460
		}
461
462
		return $values;
463
	}
464
465
466
	/**
467
	 * Checks if the current user is allowed to retrieve the customer data for the given ID
468
	 *
469
	 * @param string $id Unique customer ID
470
	 * @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed
471
	 */
472
	protected function checkUser( $id )
473
	{
474
		if( $id != $this->getContext()->getUserId() )
475
		{
476
			$msg = sprintf( 'Not allowed to access customer data for ID "%1$s"', $id );
477
			throw new \Aimeos\Controller\Frontend\Customer\Exception( $msg );
478
		}
479
	}
480
}
481