Conditions | 20 |
Paths | 320 |
Total Lines | 320 |
Code Lines | 225 |
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 |
||
278 | public function persist( $order ) { |
||
279 | global $wpdb; |
||
280 | |||
281 | $date_created = ''; |
||
282 | if ( null !== $order->get_date_created() ) { |
||
283 | $date_created = $order->get_date_created()->format( 'Y-m-d H:i:s' ); |
||
284 | } |
||
285 | |||
286 | $date_modified = ''; |
||
287 | if ( null !== $order->get_date_modified() ) { |
||
288 | $date_modified = $order->get_date_modified()->format( 'Y-m-d H:i:s' ); |
||
289 | } |
||
290 | |||
291 | $order_id = $order->get_id(); |
||
292 | |||
293 | $customer = $order->get_customer(); |
||
294 | |||
295 | // check if it's a new order or if we need to update an existing one |
||
296 | if ( empty( $order_id ) ) { |
||
297 | /** New order */ |
||
298 | |||
299 | // insert order |
||
300 | $r = $wpdb->insert( |
||
301 | $wpdb->prefix . 'dlm_order', |
||
302 | array( |
||
303 | 'status' => $order->get_status()->get_key(), |
||
304 | 'date_created' => $date_created, |
||
305 | 'date_modified' => $date_modified, |
||
306 | 'currency' => $order->get_currency(), |
||
307 | 'hash' => $order->get_hash() |
||
308 | ), |
||
309 | array( |
||
310 | '%s', |
||
311 | '%s', |
||
312 | '%s', |
||
313 | '%s', |
||
314 | '%s' |
||
315 | ) |
||
316 | ); |
||
317 | |||
318 | if ( false === $r ) { |
||
319 | throw new \Exception( "Failed creating Order" ); |
||
320 | } |
||
321 | |||
322 | // set the new id as order id |
||
323 | $order->set_id( $wpdb->insert_id ); |
||
324 | |||
325 | // insert customer record |
||
326 | $r = $wpdb->insert( |
||
327 | $wpdb->prefix . 'dlm_order_customer', |
||
328 | array( |
||
329 | 'first_name' => $customer->get_first_name(), |
||
330 | 'last_name' => $customer->get_last_name(), |
||
331 | 'company' => $customer->get_company(), |
||
332 | 'address_1' => $customer->get_address_1(), |
||
333 | 'address_2' => $customer->get_address_2(), |
||
334 | 'city' => $customer->get_city(), |
||
335 | 'state' => $customer->get_state(), |
||
336 | 'postcode' => $customer->get_postcode(), |
||
337 | 'country' => $customer->get_country(), |
||
338 | 'email' => $customer->get_email(), |
||
339 | 'phone' => $customer->get_phone(), |
||
340 | 'ip_address' => $customer->get_ip_address(), |
||
341 | 'order_id' => $order->get_id() |
||
342 | ), |
||
343 | array( |
||
344 | '%s', |
||
345 | '%s', |
||
346 | '%s', |
||
347 | '%s', |
||
348 | '%s', |
||
349 | '%s', |
||
350 | '%s', |
||
351 | '%s', |
||
352 | '%s', |
||
353 | '%s', |
||
354 | '%s', |
||
355 | '%s', |
||
356 | '%d' |
||
357 | ) |
||
358 | ); |
||
359 | |||
360 | if ( false === $r ) { |
||
361 | throw new \Exception( "Failed creating Customer" ); |
||
362 | } |
||
363 | |||
364 | } else { |
||
365 | |||
366 | // update an existing order |
||
367 | $r = $wpdb->update( $wpdb->prefix . 'dlm_order', |
||
368 | array( |
||
369 | 'status' => $order->get_status()->get_key(), |
||
370 | 'date_modified' => current_time( 'mysql', 1 ), |
||
371 | 'currency' => $order->get_currency(), |
||
372 | 'hash' => $order->get_hash() |
||
373 | ), |
||
374 | array( 'id' => $order_id ), |
||
375 | array( |
||
376 | '%s', |
||
377 | '%s', |
||
378 | '%s', |
||
379 | '%s' |
||
380 | ), |
||
381 | array( '%d' ) |
||
382 | ); |
||
383 | |||
384 | if ( false === $r ) { |
||
385 | throw new \Exception( "Failed updating Order" ); |
||
386 | } |
||
387 | |||
388 | // update customer record |
||
389 | $r = $wpdb->update( |
||
390 | $wpdb->prefix . 'dlm_order_customer', |
||
391 | array( |
||
392 | 'first_name' => $customer->get_first_name(), |
||
393 | 'last_name' => $customer->get_last_name(), |
||
394 | 'company' => $customer->get_company(), |
||
395 | 'address_1' => $customer->get_address_1(), |
||
396 | 'address_2' => $customer->get_address_2(), |
||
397 | 'city' => $customer->get_city(), |
||
398 | 'state' => $customer->get_state(), |
||
399 | 'postcode' => $customer->get_postcode(), |
||
400 | 'country' => $customer->get_country(), |
||
401 | 'email' => $customer->get_email(), |
||
402 | 'phone' => $customer->get_phone(), |
||
403 | 'ip_address' => $customer->get_ip_address(), |
||
404 | 'order_id' => $order->get_id() |
||
405 | ), |
||
406 | array( 'order_id' => $order_id ), |
||
407 | array( |
||
408 | '%s', |
||
409 | '%s', |
||
410 | '%s', |
||
411 | '%s', |
||
412 | '%s', |
||
413 | '%s', |
||
414 | '%s', |
||
415 | '%s', |
||
416 | '%s', |
||
417 | '%s', |
||
418 | '%s', |
||
419 | '%s', |
||
420 | '%d' |
||
421 | ), |
||
422 | array( '%d' ) |
||
423 | ); |
||
424 | |||
425 | if ( false === $r ) { |
||
426 | throw new \Exception( "Failed updating customer" ); |
||
427 | } |
||
428 | |||
429 | } |
||
430 | |||
431 | // handle order items |
||
432 | $order_items = $order->get_items(); |
||
433 | if ( ! empty( $order_items ) ) { |
||
434 | foreach ( $order_items as $order_item ) { |
||
435 | |||
436 | // check if this order item exists in DB already |
||
437 | $order_item_id = $order_item->get_id(); |
||
438 | if ( empty( $order_item_id ) ) { |
||
439 | |||
440 | // insert new order item |
||
441 | $r = $wpdb->insert( |
||
442 | $wpdb->prefix . 'dlm_order_item', |
||
443 | array( |
||
444 | 'order_id' => $order->get_id(), |
||
445 | 'label' => $order_item->get_label(), |
||
446 | 'qty' => $order_item->get_qty(), |
||
447 | 'download_id' => $order_item->get_download_id(), |
||
448 | 'tax_class' => $order_item->get_tax_class(), |
||
449 | 'tax_total' => $order_item->get_tax_total(), |
||
450 | 'subtotal' => $order_item->get_subtotal(), |
||
451 | 'total' => $order_item->get_total() |
||
452 | ), |
||
453 | array( |
||
454 | '%d', |
||
455 | '%s', |
||
456 | '%d', |
||
457 | '%d', |
||
458 | '%s', |
||
459 | '%d', |
||
460 | '%d', |
||
461 | '%d', |
||
462 | ) |
||
463 | ); |
||
464 | |||
465 | if ( false === $r ) { |
||
466 | throw new \Exception( "Failed creating OrderItem" ); |
||
467 | } |
||
468 | |||
469 | $order_item->set_id( $wpdb->insert_id ); |
||
470 | } else { |
||
471 | |||
472 | // update existing order item record |
||
473 | $r = $wpdb->update( |
||
474 | $wpdb->prefix . 'dlm_order_item', |
||
475 | array( |
||
476 | 'order_id' => $order->get_id(), |
||
477 | 'label' => $order_item->get_label(), |
||
478 | 'qty' => $order_item->get_qty(), |
||
479 | 'download_id' => $order_item->get_download_id(), |
||
480 | 'tax_class' => $order_item->get_tax_class(), |
||
481 | 'tax_total' => $order_item->get_tax_total(), |
||
482 | 'subtotal' => $order_item->get_subtotal(), |
||
483 | 'total' => $order_item->get_total() |
||
484 | ), |
||
485 | array( 'id' => $order_item_id ), |
||
486 | array( |
||
487 | '%d', |
||
488 | '%s', |
||
489 | '%d', |
||
490 | '%d', |
||
491 | '%s', |
||
492 | '%d', |
||
493 | '%d', |
||
494 | '%d', |
||
495 | ), |
||
496 | array( '%d' ) |
||
497 | ); |
||
498 | |||
499 | if ( false === $r ) { |
||
500 | throw new \Exception( "Failed updating OrderItem" ); |
||
501 | } |
||
502 | |||
503 | } |
||
504 | } |
||
505 | } |
||
506 | |||
507 | // handle transactions |
||
508 | $transactions = $order->get_transactions(); |
||
509 | if ( ! empty( $transactions ) ) { |
||
510 | |||
511 | /** @var Transaction\OrderTransaction $transaction */ |
||
512 | foreach ( $transactions as $transaction ) { |
||
513 | |||
514 | $transaction_id = $transaction->get_id(); |
||
515 | |||
516 | $transaction_date_created = null; |
||
517 | if ( null !== $transaction->get_date_created() ) { |
||
518 | $transaction_date_created = $transaction->get_date_created()->format( 'Y-m-d H:i:s' ); |
||
519 | } |
||
520 | |||
521 | $transaction_date_modified = null; |
||
522 | if ( null !== $transaction->get_date_modified() ) { |
||
523 | $transaction_date_modified = $transaction->get_date_modified()->format( 'Y-m-d H:i:s' ); |
||
524 | } |
||
525 | |||
526 | // check if it's a new transaction or an existing one |
||
527 | if ( empty( $transaction_id ) ) { |
||
528 | |||
529 | // it's a new transaction |
||
530 | |||
531 | $r = $wpdb->insert( |
||
532 | $wpdb->prefix . 'dlm_order_transaction', |
||
533 | array( |
||
534 | 'order_id' => $order->get_id(), |
||
535 | 'date_created' => $transaction_date_created, |
||
536 | 'date_modified' => $transaction_date_modified, |
||
537 | 'amount' => $transaction->get_amount(), |
||
538 | 'status' => $transaction->get_status()->get_key(), |
||
539 | 'processor' => $transaction->get_processor(), |
||
540 | 'processor_nice_name' => $transaction->get_processor_nice_name(), |
||
541 | 'processor_transaction_id' => $transaction->get_processor_transaction_id(), |
||
542 | 'processor_status' => $transaction->get_processor_status() |
||
543 | ), |
||
544 | array( |
||
545 | '%d', |
||
546 | '%s', |
||
547 | '%s', |
||
548 | '%d', |
||
549 | '%s', |
||
550 | '%s', |
||
551 | '%s', |
||
552 | '%s', |
||
553 | '%s' |
||
554 | ) |
||
555 | ); |
||
556 | |||
557 | if ( false === $r ) { |
||
558 | throw new \Exception( "Failed creating OrderTransaction" ); |
||
559 | } |
||
560 | |||
561 | |||
562 | $transaction->set_id( $wpdb->insert_id ); |
||
563 | |||
564 | } else { |
||
565 | |||
566 | // it's an existing transaction |
||
567 | |||
568 | $r = $wpdb->update( |
||
569 | $wpdb->prefix . 'dlm_order_transaction', |
||
570 | array( |
||
571 | 'order_id' => $order->get_id(), |
||
572 | 'date_created' => $transaction_date_created, |
||
573 | 'date_modified' => $transaction_date_modified, |
||
574 | 'amount' => $transaction->get_amount(), |
||
575 | 'status' => $transaction->get_status()->get_key(), |
||
576 | 'processor' => $transaction->get_processor(), |
||
577 | 'processor_nice_name' => $transaction->get_processor_nice_name(), |
||
578 | 'processor_transaction_id' => $transaction->get_processor_transaction_id(), |
||
579 | 'processor_status' => $transaction->get_processor_status() |
||
580 | ), |
||
581 | array( 'id' => $transaction_id ), |
||
582 | array( |
||
583 | '%d', |
||
584 | '%s', |
||
585 | '%s', |
||
586 | '%d', |
||
587 | '%s', |
||
588 | '%s', |
||
589 | '%s', |
||
590 | '%s', |
||
591 | '%s' |
||
592 | ), |
||
593 | array( '%d' ) |
||
594 | ); |
||
595 | |||
596 | if ( false === $r ) { |
||
597 | throw new \Exception( "Failed updating OrderTransaction" ); |
||
598 | } |
||
645 | } |