| Conditions | 22 |
| Paths | > 20000 |
| Total Lines | 115 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 270 | public function placeOrder() |
||
| 271 | { |
||
| 272 | if (!$this->order) { |
||
| 273 | $this->error(_t(__CLASS__ . ".NoOrderStarted", "A new order has not yet been started.")); |
||
| 274 | return false; |
||
| 275 | } |
||
| 276 | if (!$this->canPlace($this->order)) { //final cart validation |
||
| 277 | return false; |
||
| 278 | } |
||
| 279 | |||
| 280 | if ($this->order->Locale) { |
||
| 281 | ShopTools::install_locale($this->order->Locale); |
||
| 282 | } |
||
| 283 | |||
| 284 | if (DB::get_conn()->supportsTransactions()) { |
||
| 285 | DB::get_conn()->transactionStart(); |
||
| 286 | } |
||
| 287 | |||
| 288 | //update status |
||
| 289 | if ($this->order->TotalOutstanding(false)) { |
||
| 290 | $this->order->setField('Status', 'Unpaid'); |
||
| 291 | } else { |
||
| 292 | $this->order->setField('Status', 'Paid'); |
||
| 293 | } |
||
| 294 | |||
| 295 | if (!$this->order->Placed) { |
||
| 296 | $this->order->setField('Placed', DBDatetime::now()->Rfc2822()); //record placed order datetime |
||
| 297 | if ($request = Controller::curr()->getRequest()) { |
||
| 298 | $this->order->IPAddress = $request->getIP(); //record client IP |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | // Add an error handler that throws an exception upon error, so that we can catch errors as exceptions |
||
| 303 | // in the following block. |
||
| 304 | set_error_handler( |
||
| 305 | function ($severity, $message, $file, $line) { |
||
| 306 | if (!(error_reporting() & $severity)) { |
||
| 307 | // suppressed error, for example from exif_read_data in image manipulation |
||
| 308 | return false; |
||
| 309 | } |
||
| 310 | throw new ErrorException($message, 0, $severity, $file, $line); |
||
| 311 | }, |
||
| 312 | E_ALL & ~(E_STRICT | E_NOTICE | E_DEPRECATED | E_USER_DEPRECATED) |
||
| 313 | ); |
||
| 314 | |||
| 315 | try { |
||
| 316 | //re-write all attributes and modifiers to make sure they are up-to-date before they can't be changed again |
||
| 317 | $items = $this->order->Items(); |
||
| 318 | if ($items->exists()) { |
||
| 319 | foreach ($items as $item) { |
||
| 320 | $item->onPlacement(); |
||
| 321 | $item->write(); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | $modifiers = $this->order->Modifiers(); |
||
| 325 | if ($modifiers->exists()) { |
||
| 326 | foreach ($modifiers as $modifier) { |
||
| 327 | $modifier->write(); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | //add member to order & customers group |
||
| 331 | if ($member = Security::getCurrentUser()) { |
||
| 332 | if (!$this->order->MemberID) { |
||
| 333 | $this->order->MemberID = $member->ID; |
||
| 334 | } |
||
| 335 | $cgroup = ShopConfigExtension::current()->CustomerGroup(); |
||
| 336 | if ($cgroup->exists()) { |
||
| 337 | $member->Groups()->add($cgroup); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | //allow decorators to do stuff when order is saved. |
||
| 341 | $this->order->extend('onPlaceOrder'); |
||
| 342 | $this->order->write(); |
||
| 343 | } catch (Exception $ex) { |
||
| 344 | // Rollback the transaction if an error occurred |
||
| 345 | if (DB::get_conn()->supportsTransactions()) { |
||
| 346 | DB::get_conn()->transactionRollback(); |
||
| 347 | } |
||
| 348 | $this->error($ex->getMessage()); |
||
| 349 | return false; |
||
| 350 | } finally { |
||
| 351 | // restore the error handler, no matter what |
||
| 352 | restore_error_handler(); |
||
| 353 | } |
||
| 354 | |||
| 355 | // Everything went through fine, complete the transaction |
||
| 356 | if (DB::get_conn()->supportsTransactions()) { |
||
| 357 | DB::get_conn()->transactionEnd(); |
||
| 358 | } |
||
| 359 | |||
| 360 | //remove from session |
||
| 361 | ShoppingCart::singleton()->clear(false); |
||
| 362 | /* |
||
| 363 | $cart = ShoppingCart::curr(); |
||
| 364 | if ($cart && $cart->ID == $this->order->ID) { |
||
| 365 | // clear the cart, but don't write the order in the process (order is finalized and should NOT be overwritten) |
||
| 366 | } |
||
| 367 | */ |
||
| 368 | |||
| 369 | //send confirmation if configured and receipt hasn't been sent |
||
| 370 | if (self::config()->send_confirmation |
||
| 371 | && !$this->order->ReceiptSent |
||
| 372 | ) { |
||
| 373 | $this->notifier->sendConfirmation(); |
||
| 374 | } |
||
| 375 | |||
| 376 | //notify admin, if configured |
||
| 377 | if (self::config()->send_admin_notification) { |
||
| 378 | $this->notifier->sendAdminNotification(); |
||
| 379 | } |
||
| 380 | |||
| 381 | // Save order reference to session |
||
| 382 | OrderManipulationExtension::add_session_order($this->order); |
||
| 383 | |||
| 384 | return true; //report success |
||
| 385 | } |
||
| 405 |