| Conditions | 7 |
| Paths | 17 |
| Total Lines | 166 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| 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 |
||
| 213 | protected function saveItem( \Aimeos\MShop\Customer\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Customer\Item\Iface |
||
| 214 | { |
||
| 215 | $item = $this->addGroups( $item ); |
||
| 216 | |||
| 217 | if( !$item->isModified() ) { |
||
| 218 | return $this->object()->saveRefs( $item, $fetch ); |
||
| 219 | } |
||
| 220 | |||
| 221 | $context = $this->context(); |
||
| 222 | $conn = $context->db( $this->getResourceName() ); |
||
| 223 | |||
| 224 | $id = $item->getId(); |
||
| 225 | $billingAddress = $item->getPaymentAddress(); |
||
| 226 | $columns = $this->object()->getSaveAttributes(); |
||
| 227 | |||
| 228 | if( $id === null ) |
||
| 229 | { |
||
| 230 | /** mshop/customer/manager/laravel/insert |
||
| 231 | * Inserts a new customer record into the database table |
||
| 232 | * |
||
| 233 | * Items with no ID yet (i.e. the ID is NULL) will be created in |
||
| 234 | * the database and the newly created ID retrieved afterwards |
||
| 235 | * using the "newid" SQL statement. |
||
| 236 | * |
||
| 237 | * The SQL statement must be a string suitable for being used as |
||
| 238 | * prepared statement. It must include question marks for binding |
||
| 239 | * the values from the customer item to the statement before they are |
||
| 240 | * sent to the database server. The number of question marks must |
||
| 241 | * be the same as the number of columns listed in the INSERT |
||
| 242 | * statement. The order of the columns must correspond to the |
||
| 243 | * order in the save() method, so the correct values are |
||
| 244 | * bound to the columns. |
||
| 245 | * |
||
| 246 | * The SQL statement should conform to the ANSI standard to be |
||
| 247 | * compatible with most relational database systems. This also |
||
| 248 | * includes using double quotes for table and column names. |
||
| 249 | * |
||
| 250 | * @param string SQL statement for inserting records |
||
| 251 | * @since 2015.01 |
||
| 252 | * @category Developer |
||
| 253 | * @see mshop/customer/manager/laravel/update |
||
| 254 | * @see mshop/customer/manager/laravel/newid |
||
| 255 | * @see mshop/customer/manager/laravel/delete |
||
| 256 | * @see mshop/customer/manager/laravel/search |
||
| 257 | * @see mshop/customer/manager/laravel/count |
||
| 258 | */ |
||
| 259 | $path = 'mshop/customer/manager/laravel/insert'; |
||
| 260 | $sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) ); |
||
|
|
|||
| 261 | } |
||
| 262 | else |
||
| 263 | { |
||
| 264 | /** mshop/customer/manager/laravel/update |
||
| 265 | * Updates an existing customer record in the database |
||
| 266 | * |
||
| 267 | * Items which already have an ID (i.e. the ID is not NULL) will |
||
| 268 | * be updated in the database. |
||
| 269 | * |
||
| 270 | * The SQL statement must be a string suitable for being used as |
||
| 271 | * prepared statement. It must include question marks for binding |
||
| 272 | * the values from the customer item to the statement before they are |
||
| 273 | * sent to the database server. The order of the columns must |
||
| 274 | * correspond to the order in the save() method, so the |
||
| 275 | * correct values are bound to the columns. |
||
| 276 | * |
||
| 277 | * The SQL statement should conform to the ANSI standard to be |
||
| 278 | * compatible with most relational database systems. This also |
||
| 279 | * includes using double quotes for table and column names. |
||
| 280 | * |
||
| 281 | * @param string SQL statement for updating records |
||
| 282 | * @since 2015.01 |
||
| 283 | * @category Developer |
||
| 284 | * @see mshop/customer/manager/laravel/insert |
||
| 285 | * @see mshop/customer/manager/laravel/newid |
||
| 286 | * @see mshop/customer/manager/laravel/delete |
||
| 287 | * @see mshop/customer/manager/laravel/search |
||
| 288 | * @see mshop/customer/manager/laravel/count |
||
| 289 | */ |
||
| 290 | $path = 'mshop/customer/manager/laravel/update'; |
||
| 291 | $sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false ); |
||
| 292 | } |
||
| 293 | |||
| 294 | $idx = 1; |
||
| 295 | $stmt = $this->getCachedStatement( $conn, $path, $sql ); |
||
| 296 | |||
| 297 | foreach( $columns as $name => $entry ) { |
||
| 298 | $stmt->bind( $idx++, $item->get( $name ), \Aimeos\Base\Criteria\SQL::type( $entry->getType() ) ); |
||
| 299 | } |
||
| 300 | |||
| 301 | $stmt->bind( $idx++, $item->getLabel() ); |
||
| 302 | $stmt->bind( $idx++, $item->getCode() ); |
||
| 303 | $stmt->bind( $idx++, $billingAddress->getCompany() ); |
||
| 304 | $stmt->bind( $idx++, $billingAddress->getVatID() ); |
||
| 305 | $stmt->bind( $idx++, $billingAddress->getSalutation() ); |
||
| 306 | $stmt->bind( $idx++, $billingAddress->getTitle() ); |
||
| 307 | $stmt->bind( $idx++, $billingAddress->getFirstname() ); |
||
| 308 | $stmt->bind( $idx++, $billingAddress->getLastname() ); |
||
| 309 | $stmt->bind( $idx++, $billingAddress->getAddress1() ); |
||
| 310 | $stmt->bind( $idx++, $billingAddress->getAddress2() ); |
||
| 311 | $stmt->bind( $idx++, $billingAddress->getAddress3() ); |
||
| 312 | $stmt->bind( $idx++, $billingAddress->getPostal() ); |
||
| 313 | $stmt->bind( $idx++, $billingAddress->getCity() ); |
||
| 314 | $stmt->bind( $idx++, $billingAddress->getState() ); |
||
| 315 | $stmt->bind( $idx++, $billingAddress->getCountryId() ); |
||
| 316 | $stmt->bind( $idx++, $billingAddress->getLanguageId() ); |
||
| 317 | $stmt->bind( $idx++, $billingAddress->getTelephone() ); |
||
| 318 | $stmt->bind( $idx++, $billingAddress->getTelefax() ); |
||
| 319 | $stmt->bind( $idx++, $billingAddress->getMobile() ); |
||
| 320 | $stmt->bind( $idx++, $billingAddress->getWebsite() ); |
||
| 321 | $stmt->bind( $idx++, $billingAddress->getLongitude(), \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT ); |
||
| 322 | $stmt->bind( $idx++, $billingAddress->getLatitude(), \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT ); |
||
| 323 | $stmt->bind( $idx++, $billingAddress->getBirthday() ); |
||
| 324 | $stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
||
| 325 | $stmt->bind( $idx++, $item->getDateVerified() ); |
||
| 326 | $stmt->bind( $idx++, $item->getPassword() ); |
||
| 327 | $stmt->bind( $idx++, $context->datetime() ); // Modification time |
||
| 328 | $stmt->bind( $idx++, $context->editor() ); |
||
| 329 | |||
| 330 | if( $id !== null ) { |
||
| 331 | $stmt->bind( $idx++, $context->locale()->getSiteId() . '%' ); |
||
| 332 | $stmt->bind( $idx++, (string) $context->user()?->getSiteId() ); |
||
| 333 | $stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
||
| 334 | $item->setId( $id ); |
||
| 335 | } else { |
||
| 336 | $stmt->bind( $idx++, $this->siteId( $item->getSiteId(), \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) ); |
||
| 337 | $stmt->bind( $idx++, $context->datetime() ); // Creation time |
||
| 338 | } |
||
| 339 | |||
| 340 | $stmt->execute()->finish(); |
||
| 341 | |||
| 342 | if( $id === null && $fetch === true ) |
||
| 343 | { |
||
| 344 | /** mshop/customer/manager/laravel/newid |
||
| 345 | * Retrieves the ID generated by the database when inserting a new record |
||
| 346 | * |
||
| 347 | * As soon as a new record is inserted into the database table, |
||
| 348 | * the database server generates a new and unique identifier for |
||
| 349 | * that record. This ID can be used for retrieving, updating and |
||
| 350 | * deleting that specific record from the table again. |
||
| 351 | * |
||
| 352 | * For MySQL: |
||
| 353 | * SELECT LAST_INSERT_ID() |
||
| 354 | * For PostgreSQL: |
||
| 355 | * SELECT currval('seq_mcus_id') |
||
| 356 | * For SQL Server: |
||
| 357 | * SELECT SCOPE_IDENTITY() |
||
| 358 | * For Oracle: |
||
| 359 | * SELECT "seq_mcus_id".CURRVAL FROM DUAL |
||
| 360 | * |
||
| 361 | * There's no way to retrive the new ID by a SQL statements that |
||
| 362 | * fits for most database servers as they implement their own |
||
| 363 | * specific way. |
||
| 364 | * |
||
| 365 | * @param string SQL statement for retrieving the last inserted record ID |
||
| 366 | * @since 2015.01 |
||
| 367 | * @category Developer |
||
| 368 | * @see mshop/customer/manager/laravel/insert |
||
| 369 | * @see mshop/customer/manager/laravel/update |
||
| 370 | * @see mshop/customer/manager/laravel/delete |
||
| 371 | * @see mshop/customer/manager/laravel/search |
||
| 372 | * @see mshop/customer/manager/laravel/count |
||
| 373 | */ |
||
| 374 | $path = 'mshop/customer/manager/laravel/newid'; |
||
| 375 | $id = $this->newId( $conn, $path ); |
||
| 376 | } |
||
| 377 | |||
| 378 | return $this->object()->saveRefs( $item->setId( $id ), $fetch ); |
||
| 379 | } |
||
| 421 |