Conditions | 10 |
Paths | 326 |
Total Lines | 174 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 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 |
||
314 | public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true ) |
||
315 | { |
||
316 | $iface = '\\Aimeos\\MShop\\Customer\\Item\\Iface'; |
||
317 | if( !( $item instanceof $iface ) ) { |
||
318 | throw new \Aimeos\MShop\Customer\Exception( sprintf( 'Object is not of required type "%1$s"', $iface ) ); |
||
319 | } |
||
320 | |||
321 | if( !$item->isModified() ) { return; } |
||
322 | |||
323 | $context = $this->getContext(); |
||
324 | $dbm = $context->getDatabaseManager(); |
||
325 | $dbname = $this->getResourceName(); |
||
326 | $conn = $dbm->acquire( $dbname ); |
||
327 | |||
328 | try |
||
329 | { |
||
330 | $id = $item->getId(); |
||
331 | $billingAddress = $item->getPaymentAddress(); |
||
1 ignored issue
–
show
|
|||
332 | |||
333 | if( $id === null ) |
||
334 | { |
||
335 | /** mshop/customer/manager/typo3/insert |
||
336 | * Inserts a new customer record into the database table |
||
337 | * |
||
338 | * Items with no ID yet (i.e. the ID is NULL) will be created in |
||
339 | * the database and the newly created ID retrieved afterwards |
||
340 | * using the "newid" SQL statement. |
||
341 | * |
||
342 | * The SQL statement must be a string suitable for being used as |
||
343 | * prepared statement. It must include question marks for binding |
||
344 | * the values from the customer item to the statement before they are |
||
345 | * sent to the database server. The number of question marks must |
||
346 | * be the same as the number of columns listed in the INSERT |
||
347 | * statement. The order of the columns must correspond to the |
||
348 | * order in the saveItems() method, so the correct values are |
||
349 | * bound to the columns. |
||
350 | * |
||
351 | * The SQL statement should conform to the ANSI standard to be |
||
352 | * compatible with most relational database systems. This also |
||
353 | * includes using double quotes for table and column names. |
||
354 | * |
||
355 | * @param string SQL statement for inserting records |
||
356 | * @since 2014.03 |
||
357 | * @category Developer |
||
358 | * @see mshop/customer/manager/typo3/update |
||
359 | * @see mshop/customer/manager/typo3/newid |
||
360 | * @see mshop/customer/manager/typo3/delete |
||
361 | * @see mshop/customer/manager/typo3/search |
||
362 | * @see mshop/customer/manager/typo3/count |
||
363 | */ |
||
364 | $path = 'mshop/customer/manager/typo3/insert'; |
||
365 | } |
||
366 | else |
||
367 | { |
||
368 | /** mshop/customer/manager/typo3/update |
||
369 | * Updates an existing customer record in the database |
||
370 | * |
||
371 | * Items which already have an ID (i.e. the ID is not NULL) will |
||
372 | * be updated in the database. |
||
373 | * |
||
374 | * The SQL statement must be a string suitable for being used as |
||
375 | * prepared statement. It must include question marks for binding |
||
376 | * the values from the customer item to the statement before they are |
||
377 | * sent to the database server. The order of the columns must |
||
378 | * correspond to the order in the saveItems() method, so the |
||
379 | * correct values are bound to the columns. |
||
380 | * |
||
381 | * The SQL statement should conform to the ANSI standard to be |
||
382 | * compatible with most relational database systems. This also |
||
383 | * includes using double quotes for table and column names. |
||
384 | * |
||
385 | * @param string SQL statement for updating records |
||
386 | * @since 2014.03 |
||
387 | * @category Developer |
||
388 | * @see mshop/customer/manager/typo3/insert |
||
389 | * @see mshop/customer/manager/typo3/newid |
||
390 | * @see mshop/customer/manager/typo3/delete |
||
391 | * @see mshop/customer/manager/typo3/search |
||
392 | * @see mshop/customer/manager/typo3/count |
||
393 | */ |
||
394 | $path = 'mshop/customer/manager/typo3/update'; |
||
395 | } |
||
396 | |||
397 | $stmt = $this->getCachedStatement( $conn, $path ); |
||
398 | |||
399 | $address = $billingAddress->getAddress1(); |
||
400 | |||
401 | if( ( $part = $billingAddress->getAddress2() ) != '' ) { |
||
402 | $address .= ' ' . $part; |
||
403 | } |
||
404 | |||
405 | if( ( $part = $billingAddress->getAddress3() ) != '' ) { |
||
406 | $address .= ' ' . $part; |
||
407 | } |
||
408 | |||
409 | // TYPO3 fe_users.static_info_country is a three letter ISO code instead a two letter one |
||
410 | $stmt->bind( 1, $item->getLabel() ); |
||
1 ignored issue
–
show
|
|||
411 | $stmt->bind( 2, $item->getCode() ); |
||
1 ignored issue
–
show
|
|||
412 | $stmt->bind( 3, $this->plugins['customer.salutation']->translate( $billingAddress->getSalutation() ), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
413 | $stmt->bind( 4, $billingAddress->getCompany() ); |
||
414 | $stmt->bind( 5, $billingAddress->getVatID() ); |
||
415 | $stmt->bind( 6, $billingAddress->getTitle() ); |
||
416 | $stmt->bind( 7, $billingAddress->getFirstname() ); |
||
417 | $stmt->bind( 8, $billingAddress->getLastname() ); |
||
418 | $stmt->bind( 9, $address ); |
||
419 | $stmt->bind( 10, $billingAddress->getPostal() ); |
||
420 | $stmt->bind( 11, $billingAddress->getCity() ); |
||
421 | $stmt->bind( 12, $billingAddress->getState() ); |
||
422 | $stmt->bind( 13, $billingAddress->getLanguageId() ); |
||
423 | $stmt->bind( 14, $billingAddress->getTelephone() ); |
||
424 | $stmt->bind( 15, $billingAddress->getEmail() ); |
||
425 | $stmt->bind( 16, $billingAddress->getTelefax() ); |
||
426 | $stmt->bind( 17, $billingAddress->getWebsite() ); |
||
427 | $stmt->bind( 18, $this->plugins['customer.birthday']->translate( $item->getBirthday() ), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
1 ignored issue
–
show
|
|||
428 | $stmt->bind( 19, $this->plugins['customer.status']->translate( $item->getStatus() ), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
1 ignored issue
–
show
|
|||
429 | $stmt->bind( 20, $item->getPassword() ); |
||
1 ignored issue
–
show
|
|||
430 | $stmt->bind( 21, time(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // Modification time |
||
431 | $stmt->bind( 22, $billingAddress->getCountryId() ); |
||
432 | $stmt->bind( 23, implode( ',', $item->getGroups() ) ); |
||
1 ignored issue
–
show
|
|||
433 | |||
434 | if( $id !== null ) { |
||
435 | $stmt->bind( 24, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
436 | $item->setId( $id ); |
||
437 | } else { |
||
438 | $stmt->bind( 24, time(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // Creation time |
||
439 | $stmt->bind( 25, $this->pid, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // TYPO3 PID value |
||
440 | } |
||
441 | |||
442 | $stmt->execute()->finish(); |
||
443 | |||
444 | if( $id === null && $fetch === true ) |
||
445 | { |
||
446 | /** mshop/customer/manager/typo3/newid |
||
447 | * Retrieves the ID generated by the database when inserting a new record |
||
448 | * |
||
449 | * As soon as a new record is inserted into the database table, |
||
450 | * the database server generates a new and unique identifier for |
||
451 | * that record. This ID can be used for retrieving, updating and |
||
452 | * deleting that specific record from the table again. |
||
453 | * |
||
454 | * For MySQL: |
||
455 | * SELECT LAST_INSERT_ID() |
||
456 | * For PostgreSQL: |
||
457 | * SELECT currval('seq_mcus_id') |
||
458 | * For SQL Server: |
||
459 | * SELECT SCOPE_IDENTITY() |
||
460 | * For Oracle: |
||
461 | * SELECT "seq_mcus_id".CURRVAL FROM DUAL |
||
462 | * |
||
463 | * There's no way to retrive the new ID by a SQL statements that |
||
464 | * fits for most database servers as they implement their own |
||
465 | * specific way. |
||
466 | * |
||
467 | * @param string SQL statement for retrieving the last inserted record ID |
||
468 | * @since 2014.03 |
||
469 | * @category Developer |
||
470 | * @see mshop/customer/manager/typo3/insert |
||
471 | * @see mshop/customer/manager/typo3/update |
||
472 | * @see mshop/customer/manager/typo3/delete |
||
473 | * @see mshop/customer/manager/typo3/search |
||
474 | * @see mshop/customer/manager/typo3/count |
||
475 | */ |
||
476 | $path = 'mshop/customer/manager/typo3/newid'; |
||
477 | $item->setId( $this->newId( $conn, $path ) ); |
||
478 | } |
||
479 | |||
480 | $dbm->release( $conn, $dbname ); |
||
481 | } |
||
482 | catch( \Exception $e ) |
||
483 | { |
||
484 | $dbm->release( $conn, $dbname ); |
||
485 | throw $e; |
||
486 | } |
||
487 | } |
||
488 | |||
631 | } |