Completed
Push — master ( ac66b3...ab3b6f )
by Aimeos
07:35 queued 01:23
created

Ezpublish::saveItem()   C

Complexity

Conditions 7
Paths 141

Size

Total Lines 102
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 102
rs 5.3042
cc 7
eloc 55
nc 141
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package MShop
7
 * @subpackage Customer
8
 */
9
10
11
namespace Aimeos\MShop\Customer\Manager;
12
13
14
/**
15
 * Customer class implementation for ezPublish
16
 *
17
 * @package MShop
18
 * @subpackage Customer
19
 */
20
class Ezpublish
21
	extends \Aimeos\MShop\Customer\Manager\Standard
22
{
23
	private $searchConfig = array(
24
		'customer.id' => array(
25
			'label' => 'Customer ID',
26
			'code' => 'customer.id',
27
			'internalcode' => 'ezu."contentobject_id"',
28
			'type' => 'integer',
29
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT
30
		),
31
		// customer.siteid is not available
32
		'customer.label' => array(
33
			'label' => 'Customer label',
34
			'code' => 'customer.label',
35
			'internalcode' => 'ezu."login"',
36
			'type' => 'string',
37
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR
38
		),
39
		'customer.code' => array(
40
			'label' => 'Customer username',
41
			'code' => 'customer.code',
42
			'internalcode' => 'ezu."login_normalized"',
43
			'type' => 'string',
44
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR
45
		),
46
		'customer.salutation' => array(
47
			'label' => 'Customer salutation',
48
			'code' => 'customer.salutation',
49
			'internalcode' => 'ezu."salutation"',
50
			'type' => 'string',
51
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
52
		),
53
		'customer.company'=> array(
54
			'label' => 'Customer company',
55
			'code' => 'customer.company',
56
			'internalcode' => 'ezu."company"',
57
			'type' => 'string',
58
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
59
		),
60
		'customer.vatid'=> array(
61
			'label' => 'Customer VAT ID',
62
			'code' => 'customer.vatid',
63
			'internalcode' => 'ezu."vatid"',
64
			'type' => 'string',
65
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
66
		),
67
		'customer.title' => array(
68
			'label' => 'Customer title',
69
			'code' => 'customer.title',
70
			'internalcode' => 'ezu."title"',
71
			'type' => 'string',
72
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
73
		),
74
		'customer.firstname' => array(
75
			'label' => 'Customer firstname',
76
			'code' => 'customer.firstname',
77
			'internalcode' => 'ezu."firstname"',
78
			'type' => 'string',
79
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
80
		),
81
		'customer.lastname' => array(
82
			'label' => 'Customer lastname',
83
			'code' => 'customer.lastname',
84
			'internalcode' => 'ezu."lastname"',
85
			'type' => 'string',
86
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
87
		),
88
		'customer.address1' => array(
89
			'label' => 'Customer address part one',
90
			'code' => 'customer.address1',
91
			'internalcode' => 'ezu."address1"',
92
			'type' => 'string',
93
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
94
		),
95
		'customer.address2' => array(
96
			'label' => 'Customer address part two',
97
			'code' => 'customer.address2',
98
			'internalcode' => 'ezu."address2"',
99
			'type' => 'string',
100
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
101
		),
102
		'customer.address3' => array(
103
			'label' => 'Customer address part three',
104
			'code' => 'customer.address3',
105
			'internalcode' => 'ezu."address3"',
106
			'type' => 'string',
107
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
108
		),
109
		'customer.postal' => array(
110
			'label' => 'Customer postal',
111
			'code' => 'customer.postal',
112
			'internalcode' => 'ezu."postal"',
113
			'type' => 'string',
114
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
115
		),
116
		'customer.city' => array(
117
			'label' => 'Customer city',
118
			'code' => 'customer.city',
119
			'internalcode' => 'ezu."city"',
120
			'type' => 'string',
121
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
122
		),
123
		'customer.state' => array(
124
			'label' => 'Customer state',
125
			'code' => 'customer.state',
126
			'internalcode' => 'ezu."state"',
127
			'type' => 'string',
128
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
129
		),
130
		'customer.languageid' => array(
131
			'label' => 'Customer language',
132
			'code' => 'customer.languageid',
133
			'internalcode' => 'ezu."langid"',
134
			'type' => 'string',
135
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
136
		),
137
		'customer.countryid' => array(
138
			'label' => 'Customer country',
139
			'code' => 'customer.countryid',
140
			'internalcode' => 'ezu."countryid"',
141
			'type' => 'string',
142
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
143
		),
144
		'customer.telephone' => array(
145
			'label' => 'Customer telephone',
146
			'code' => 'customer.telephone',
147
			'internalcode' => 'ezu."telephone"',
148
			'type' => 'string',
149
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
150
		),
151
		'customer.email' => array(
152
			'label' => 'Customer email',
153
			'code' => 'customer.email',
154
			'internalcode' => 'ezu."email"',
155
			'type' => 'string',
156
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
157
		),
158
		'customer.telefax' => array(
159
			'label' => 'Customer telefax',
160
			'code' => 'customer.telefax',
161
			'internalcode' => 'ezu."telefax"',
162
			'type' => 'string',
163
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
164
		),
165
		'customer.website' => array(
166
			'label' => 'Customer website',
167
			'code' => 'customer.website',
168
			'internalcode' => 'ezu."website"',
169
			'type' => 'string',
170
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
171
		),
172
		'customer.birthday' => array(
173
			'label' => 'Customer birthday',
174
			'code' => 'customer.birthday',
175
			'internalcode' => 'ezu."birthday"',
176
			'type' => 'string',
177
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
178
		),
179
		'customer.password'=> array(
180
			'label' => 'Customer password',
181
			'code' => 'customer.password',
182
			'internalcode' => 'ezu."password_hash"',
183
			'type' => 'string',
184
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
185
		),
186
		'customer.status'=> array(
187
			'label' => 'Customer status',
188
			'code' => 'customer.status',
189
			'internalcode' => 'ezu."status"',
190
			'type' => 'integer',
191
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT
192
		),
193
		'customer.dateverified'=> array(
194
			'label' => 'Customer verification date',
195
			'code' => 'customer.dateverified',
196
			'internalcode' => 'ezu."vdate"',
197
			'type' => 'date',
198
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
199
		),
200
		'customer.ctime'=> array(
201
			'label' => 'Customer creation time',
202
			'code' => 'customer.ctime',
203
			'internalcode' => 'ezu."ctime"',
204
			'type' => 'datetime',
205
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
206
		),
207
		'customer.mtime'=> array(
208
			'label' => 'Customer modification time',
209
			'code' => 'customer.mtime',
210
			'internalcode' => 'ezu."mtime"',
211
			'type' => 'datetime',
212
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
213
		),
214
		'customer.editor'=> array(
215
			'label'=>'Customer editor',
216
			'code'=>'customer.editor',
217
			'internalcode' => 'ezu."editor"',
218
			'type'=> 'string',
219
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
220
		),
221
	);
222
223
	private $addressManager;
224
225
226
	/**
227
	 * Removes old entries from the storage.
228
	 *
229
	 * @param array $siteids List of IDs for sites whose entries should be deleted
230
	 */
231
	public function cleanup( array $siteids )
232
	{
233
		$path = 'mshop/customer/manager/submanagers';
234
		foreach( $this->getContext()->getConfig()->get( $path, array( 'address', 'lists' ) ) as $domain ) {
235
			$this->getSubManager( $domain )->cleanup( $siteids );
236
		}
237
	}
238
239
240
	/**
241
	 * Removes multiple items specified by ids in the array.
242
	 *
243
	 * @param array $ids List of IDs
244
	 */
245
	public function deleteItems( array $ids )
246
	{
247
		$path = 'mshop/customer/manager/ezpublish/delete';
248
		$this->deleteItemsBase( $ids, $path, false, 'contentobject_id' );
249
	}
250
251
252
	/**
253
	 * Returns the list attributes that can be used for searching.
254
	 *
255
	 * @param boolean $withsub Return also attributes of sub-managers if true
256
	 * @return array List of attribute items implementing \Aimeos\MW\Criteria\Attribute\Iface
257
	 */
258
	public function getSearchAttributes( $withsub = true )
259
	{
260
		$path = 'mshop/customer/manager/submanagers';
261
262
		return $this->getSearchAttributesBase( $this->searchConfig, $path, array( 'address', 'lists' ), $withsub );
263
	}
264
265
266
	/**
267
	 * Instantiates a new customer item object.
268
	 *
269
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item object
270
	 */
271
	public function createItem()
272
	{
273
		return $this->createItemBase();
274
	}
275
276
277
	/**
278
	 * Saves a customer item object.
279
	 *
280
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
281
	 * @param boolean $fetch True if the new ID should be returned in the item
282
	 */
283
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
284
	{
285
		$iface = '\\Aimeos\\MShop\\Customer\\Item\\Iface';
286
		if( !( $item instanceof $iface ) ) {
287
			throw new \Aimeos\MShop\Customer\Exception( sprintf( 'Object is not of required type "%1$s"', $iface ) );
288
		}
289
290
		if( !$item->isModified() ) { return; }
291
292
		$context = $this->getContext();
293
294
		$class = '\Aimeos\MShop\Context\Item\Ezpublish';
295
		if( !is_a( $context, $class ) ) {
296
			throw new \Aimeos\MShop\Customer\Exception( sprintf( 'Object is not of required type "%1$s"', $class ) );
297
		}
298
299
		if( $item->getId() === null )
300
		{
301
			$fcn = $context->getEzUser();
302
			$contentId = $fcn( $item->getCode(), $item->getPaymentAddress()->getEmail(), $item->getPassword() );
303
			$item->setId( $contentId );
304
		}
305
306
		$dbm = $context->getDatabaseManager();
307
		$dbname = $this->getResourceName();
308
		$conn = $dbm->acquire( $dbname );
309
310
		try
311
		{
312
			$date = date( 'Y-m-d H:i:s' );
313
			$ctime = ( $item->getTimeCreated() ? $item->getTimeCreated() : $date );
314
			$billingAddress = $item->getPaymentAddress();
315
316
			/** mshop/customer/manager/ezpublish/update
317
			 * Updates an existing customer record in the database
318
			 *
319
			 * Items which already have an ID (i.e. the ID is not NULL) will
320
			 * be updated in the database.
321
			 *
322
			 * The SQL statement must be a string suitable for being used as
323
			 * prepared statement. It must include question marks for binding
324
			 * the values from the customer item to the statement before they are
325
			 * sent to the database server. The order of the columns must
326
			 * correspond to the order in the saveItems() method, so the
327
			 * correct values are bound to the columns.
328
			 *
329
			 * The SQL statement should conform to the ANSI standard to be
330
			 * compatible with most relational database systems. This also
331
			 * includes using double quotes for table and column names.
332
			 *
333
			 * @param string SQL statement for updating records
334
			 * @since 2016.07
335
			 * @category Developer
336
			 * @see mshop/customer/manager/ezpublish/insert
337
			 * @see mshop/customer/manager/ezpublish/newid
338
			 * @see mshop/customer/manager/ezpublish/delete
339
			 * @see mshop/customer/manager/ezpublish/search
340
			 * @see mshop/customer/manager/ezpublish/count
341
			 */
342
			$path = 'mshop/customer/manager/ezpublish/update';
343
344
			$stmt = $this->getCachedStatement( $conn, $path );
345
346
			$stmt->bind( 1, $item->getCode() ); // login normalized
347
			$stmt->bind( 2, $item->getCode() ); // login
348
			$stmt->bind( 3, $billingAddress->getCompany() );
349
			$stmt->bind( 4, $billingAddress->getVatID() );
350
			$stmt->bind( 5, $billingAddress->getSalutation() );
351
			$stmt->bind( 6, $billingAddress->getTitle() );
352
			$stmt->bind( 7, $billingAddress->getFirstname() );
353
			$stmt->bind( 8, $billingAddress->getLastname() );
354
			$stmt->bind( 9, $billingAddress->getAddress1() );
355
			$stmt->bind( 10, $billingAddress->getAddress2() );
356
			$stmt->bind( 11, $billingAddress->getAddress3() );
357
			$stmt->bind( 12, $billingAddress->getPostal() );
358
			$stmt->bind( 13, $billingAddress->getCity() );
359
			$stmt->bind( 14, $billingAddress->getState() );
360
			$stmt->bind( 15, $billingAddress->getCountryId() );
361
			$stmt->bind( 16, $billingAddress->getLanguageId() );
362
			$stmt->bind( 17, $billingAddress->getTelephone() );
363
			$stmt->bind( 18, $billingAddress->getEmail() );
364
			$stmt->bind( 19, $billingAddress->getTelefax() );
365
			$stmt->bind( 20, $billingAddress->getWebsite() );
366
			$stmt->bind( 21, $item->getBirthday() );
367
			$stmt->bind( 22, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
368
			$stmt->bind( 23, $item->getDateVerified() );
369
			$stmt->bind( 24, $item->getPassword() );
370
			$stmt->bind( 25, $date ); // Modification time
371
			$stmt->bind( 26, $context->getEditor() );
372
			$stmt->bind( 27, $ctime ); // Creation time
373
			$stmt->bind( 28, $item->getId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
374
375
			$stmt->execute()->finish();
376
377
			$dbm->release( $conn, $dbname );
378
		}
379
		catch( \Exception $e )
380
		{
381
			$dbm->release( $conn, $dbname );
382
			throw $e;
383
		}
384
	}
385
386
387
	/**
388
	 * Returns the item objects matched by the given search criteria.
389
	 *
390
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
391
	 * @param integer &$total Number of items that are available in total
392
	 * @return array List of items implementing \Aimeos\MShop\Customer\Item\Iface
393
	 * @throws \Aimeos\MShop\Customer\Exception If creating items failed
394
	 */
395
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = array(), &$total = null )
396
	{
397
		$dbm = $this->getContext()->getDatabaseManager();
398
		$dbname = $this->getResourceName();
399
		$conn = $dbm->acquire( $dbname );
400
		$map = array();
401
402
		try
403
		{
404
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
405
			$cfgPathSearch = 'mshop/customer/manager/ezpublish/search';
406
			$cfgPathCount = 'mshop/customer/manager/ezpublish/count';
407
			$required = array( 'customer' );
408
409
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
410
			while( ( $row = $results->fetch() ) !== false ) {
411
				$map[ $row['customer.id'] ] = $row;
412
			}
413
414
			$dbm->release( $conn, $dbname );
415
		}
416
		catch( \Exception $e )
417
		{
418
			$dbm->release( $conn, $dbname  );
419
			throw $e;
420
		}
421
422
		return $this->buildItems( $map, $ref, 'customer' );
423
	}
424
425
426
	/**
427
	 * Returns a new manager for customer extensions
428
	 *
429
	 * @param string $manager Name of the sub manager type in lower case
430
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
431
	 * @return mixed Manager for different extensions, e.g stock, tags, locations, etc.
432
	 */
433
	public function getSubManager( $manager, $name = null )
434
	{
435
		return $this->getSubManagerBase( 'customer', $manager, ( $name === null ? 'Ezpublish' : $name ) );
436
	}
437
438
439
	/**
440
	 * Creates a new customer item.
441
	 *
442
	 * @param array $values List of attributes for customer item
443
	 * @param array $listItems List items associated to the customer item
444
	 * @param array $refItems Items referenced by the customer item via the list items
445
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item
446
	 */
447
	protected function createItemBase( array $values = array(), array $listItems = array(), array $refItems = array() )
448
	{
449
		if( !isset( $this->addressManager ) ) {
450
			$this->addressManager = $this->getSubManager( 'address' );
451
		}
452
453
		$helper = $this->getPasswordHelper();
454
		$address = $this->addressManager->createItem();
455
456
		return new \Aimeos\MShop\Customer\Item\Standard( $address, $values, $listItems, $refItems, null, $helper );
457
	}
458
}
459