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