Conditions | 7 |
Paths | 94 |
Total Lines | 176 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
289 | public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true ) |
||
290 | { |
||
291 | self::checkClass( '\\Aimeos\\MShop\\Customer\\Item\\Iface', $item ); |
||
292 | |||
293 | if( !$item->isModified() ) |
||
294 | { |
||
295 | $item = $this->savePropertyItems( $item, 'customer' ); |
||
296 | $item = $this->saveAddressItems( $item, 'customer' ); |
||
|
|||
297 | return $this->saveListItems( $item, 'customer' ); |
||
298 | } |
||
299 | |||
300 | $context = $this->getContext(); |
||
301 | $dbm = $context->getDatabaseManager(); |
||
302 | $dbname = $this->getResourceName(); |
||
303 | $conn = $dbm->acquire( $dbname ); |
||
304 | |||
305 | try |
||
306 | { |
||
307 | $id = $item->getId(); |
||
308 | $date = date( 'Y-m-d H:i:s' ); |
||
309 | $billingAddress = $item->getPaymentAddress(); |
||
310 | |||
311 | if( $id === null ) |
||
312 | { |
||
313 | /** mshop/customer/manager/laravel/insert |
||
314 | * Inserts a new customer record into the database table |
||
315 | * |
||
316 | * Items with no ID yet (i.e. the ID is NULL) will be created in |
||
317 | * the database and the newly created ID retrieved afterwards |
||
318 | * using the "newid" SQL statement. |
||
319 | * |
||
320 | * The SQL statement must be a string suitable for being used as |
||
321 | * prepared statement. It must include question marks for binding |
||
322 | * the values from the customer item to the statement before they are |
||
323 | * sent to the database server. The number of question marks must |
||
324 | * be the same as the number of columns listed in the INSERT |
||
325 | * statement. The order of the columns must correspond to the |
||
326 | * order in the saveItems() method, so the correct values are |
||
327 | * 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 inserting records |
||
334 | * @since 2015.01 |
||
335 | * @category Developer |
||
336 | * @see mshop/customer/manager/laravel/update |
||
337 | * @see mshop/customer/manager/laravel/newid |
||
338 | * @see mshop/customer/manager/laravel/delete |
||
339 | * @see mshop/customer/manager/laravel/search |
||
340 | * @see mshop/customer/manager/laravel/count |
||
341 | */ |
||
342 | $path = 'mshop/customer/manager/laravel/insert'; |
||
343 | } |
||
344 | else |
||
345 | { |
||
346 | /** mshop/customer/manager/laravel/update |
||
347 | * Updates an existing customer record in the database |
||
348 | * |
||
349 | * Items which already have an ID (i.e. the ID is not NULL) will |
||
350 | * be updated in the database. |
||
351 | * |
||
352 | * The SQL statement must be a string suitable for being used as |
||
353 | * prepared statement. It must include question marks for binding |
||
354 | * the values from the customer item to the statement before they are |
||
355 | * sent to the database server. The order of the columns must |
||
356 | * correspond to the order in the saveItems() method, so the |
||
357 | * correct values are bound to the columns. |
||
358 | * |
||
359 | * The SQL statement should conform to the ANSI standard to be |
||
360 | * compatible with most relational database systems. This also |
||
361 | * includes using double quotes for table and column names. |
||
362 | * |
||
363 | * @param string SQL statement for updating records |
||
364 | * @since 2015.01 |
||
365 | * @category Developer |
||
366 | * @see mshop/customer/manager/laravel/insert |
||
367 | * @see mshop/customer/manager/laravel/newid |
||
368 | * @see mshop/customer/manager/laravel/delete |
||
369 | * @see mshop/customer/manager/laravel/search |
||
370 | * @see mshop/customer/manager/laravel/count |
||
371 | */ |
||
372 | $path = 'mshop/customer/manager/laravel/update'; |
||
373 | } |
||
374 | |||
375 | $stmt = $this->getCachedStatement( $conn, $path ); |
||
376 | |||
377 | $stmt->bind( 1, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
378 | $stmt->bind( 2, $item->getCode() ); |
||
379 | $stmt->bind( 3, $billingAddress->getCompany() ); |
||
380 | $stmt->bind( 4, $billingAddress->getVatID() ); |
||
381 | $stmt->bind( 5, $billingAddress->getSalutation() ); |
||
382 | $stmt->bind( 6, $billingAddress->getTitle() ); |
||
383 | $stmt->bind( 7, $billingAddress->getFirstname() ); |
||
384 | $stmt->bind( 8, $billingAddress->getLastname() ); |
||
385 | $stmt->bind( 9, $billingAddress->getAddress1() ); |
||
386 | $stmt->bind( 10, $billingAddress->getAddress2() ); |
||
387 | $stmt->bind( 11, $billingAddress->getAddress3() ); |
||
388 | $stmt->bind( 12, $billingAddress->getPostal() ); |
||
389 | $stmt->bind( 13, $billingAddress->getCity() ); |
||
390 | $stmt->bind( 14, $billingAddress->getState() ); |
||
391 | $stmt->bind( 15, $billingAddress->getCountryId() ); |
||
392 | $stmt->bind( 16, $billingAddress->getLanguageId() ); |
||
393 | $stmt->bind( 17, $billingAddress->getTelephone() ); |
||
394 | $stmt->bind( 18, $billingAddress->getTelefax() ); |
||
395 | $stmt->bind( 19, $billingAddress->getWebsite() ); |
||
396 | $stmt->bind( 20, $billingAddress->getEmail() ); |
||
397 | $stmt->bind( 21, $billingAddress->getLongitude(), \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT ); |
||
398 | $stmt->bind( 22, $billingAddress->getLatitude(), \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT ); |
||
399 | $stmt->bind( 23, $item->getLabel() ); |
||
400 | $stmt->bind( 24, $item->getBirthday() ); |
||
401 | $stmt->bind( 25, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
402 | $stmt->bind( 26, $item->getDateVerified() ); |
||
403 | $stmt->bind( 27, $item->getPassword() ); |
||
404 | $stmt->bind( 28, $date ); // Modification time |
||
405 | $stmt->bind( 29, $context->getEditor() ); |
||
406 | |||
407 | if( $id !== null ) { |
||
408 | $stmt->bind( 30, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
409 | $item->setId( $id ); |
||
410 | } else { |
||
411 | $stmt->bind( 30, $date ); // Creation time |
||
412 | } |
||
413 | |||
414 | $stmt->execute()->finish(); |
||
415 | |||
416 | if( $id === null && $fetch === true ) |
||
417 | { |
||
418 | /** mshop/customer/manager/laravel/newid |
||
419 | * Retrieves the ID generated by the database when inserting a new record |
||
420 | * |
||
421 | * As soon as a new record is inserted into the database table, |
||
422 | * the database server generates a new and unique identifier for |
||
423 | * that record. This ID can be used for retrieving, updating and |
||
424 | * deleting that specific record from the table again. |
||
425 | * |
||
426 | * For MySQL: |
||
427 | * SELECT LAST_INSERT_ID() |
||
428 | * For PostgreSQL: |
||
429 | * SELECT currval('seq_mcus_id') |
||
430 | * For SQL Server: |
||
431 | * SELECT SCOPE_IDENTITY() |
||
432 | * For Oracle: |
||
433 | * SELECT "seq_mcus_id".CURRVAL FROM DUAL |
||
434 | * |
||
435 | * There's no way to retrive the new ID by a SQL statements that |
||
436 | * fits for most database servers as they implement their own |
||
437 | * specific way. |
||
438 | * |
||
439 | * @param string SQL statement for retrieving the last inserted record ID |
||
440 | * @since 2015.01 |
||
441 | * @category Developer |
||
442 | * @see mshop/customer/manager/laravel/insert |
||
443 | * @see mshop/customer/manager/laravel/update |
||
444 | * @see mshop/customer/manager/laravel/delete |
||
445 | * @see mshop/customer/manager/laravel/search |
||
446 | * @see mshop/customer/manager/laravel/count |
||
447 | */ |
||
448 | $path = 'mshop/customer/manager/laravel/newid'; |
||
449 | $item->setId( $this->newId( $conn, $path ) ); |
||
450 | } |
||
451 | |||
452 | $dbm->release( $conn, $dbname ); |
||
453 | } |
||
454 | catch( \Exception $e ) |
||
455 | { |
||
456 | $dbm->release( $conn, $dbname ); |
||
457 | throw $e; |
||
458 | } |
||
459 | |||
460 | $this->addGroups( $item ); |
||
461 | |||
462 | $item = $this->savePropertyItems( $item, 'customer' ); |
||
463 | $item = $this->saveAddressItems( $item, 'customer' ); |
||
464 | return $this->saveListItems( $item, 'customer' ); |
||
465 | } |
||
529 |