| Total Complexity | 88 |
| Total Lines | 642 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CartController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CartController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class CartController extends BaseCartController |
||
| 22 | { |
||
| 23 | public $templateController; |
||
| 24 | public $product; |
||
| 25 | public $currency; |
||
| 26 | public $addons; |
||
| 27 | public $addonRelation; |
||
| 28 | public $licence; |
||
| 29 | public $tax_option; |
||
| 30 | public $tax_by_state; |
||
| 31 | public $setting; |
||
| 32 | |||
| 33 | public function __construct() |
||
| 34 | { |
||
| 35 | $templateController = new TemplateController(); |
||
| 36 | $this->templateController = $templateController; |
||
| 37 | |||
| 38 | $product = new Product(); |
||
| 39 | $this->product = $product; |
||
| 40 | |||
| 41 | $plan_price = new PlanPrice(); |
||
| 42 | $this->$plan_price = $plan_price; |
||
| 43 | |||
| 44 | $currency = new Currency(); |
||
| 45 | $this->currency = $currency; |
||
| 46 | |||
| 47 | $tax = new Tax(); |
||
| 48 | $this->tax = $tax; |
||
| 49 | |||
| 50 | $setting = new Setting(); |
||
| 51 | $this->setting = $setting; |
||
| 52 | |||
| 53 | $tax_option = new TaxOption(); |
||
| 54 | $this->tax_option = $tax_option; |
||
| 55 | |||
| 56 | $tax_by_state = new TaxByState(); |
||
| 57 | $this->tax_by_state = new $tax_by_state(); |
||
| 58 | |||
| 59 | // $this->middleware('Inatall'); |
||
| 60 | // $this->middleware('admin'); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function productList(Request $request) |
||
| 64 | { |
||
| 65 | try { |
||
| 66 | $cont = new \App\Http\Controllers\Front\GetPageTemplateController(); |
||
| 67 | $location = $cont->getLocation(); |
||
| 68 | } catch (\Exception $ex) { |
||
| 69 | $location = false; |
||
| 70 | $error = $ex->getMessage(); |
||
| 71 | } |
||
| 72 | |||
| 73 | $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']); |
||
| 74 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']); |
||
| 75 | $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray(); |
||
| 76 | $state_code = $location['countryCode'].'-'.$location['region']; |
||
| 77 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 78 | $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']); |
||
| 79 | $currency = $cont->getCurrency($location); |
||
| 80 | |||
| 81 | \Session::put('currency', $currency); |
||
| 82 | if (!\Session::has('currency')) { |
||
| 83 | \Session::put('currency', 'INR'); |
||
| 84 | } |
||
| 85 | |||
| 86 | try { |
||
| 87 | $page_controller = new PageController(); |
||
| 88 | |||
| 89 | return $page_controller->cart(); |
||
| 90 | } catch (\Exception $ex) { |
||
| 91 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | public function cart(Request $request) |
||
| 96 | { |
||
| 97 | try { |
||
| 98 | $plan = ''; |
||
| 99 | if ($request->has('subscription')) { |
||
| 100 | $plan = $request->get('subscription'); |
||
| 101 | Session::put('plan', $plan); |
||
| 102 | } |
||
| 103 | $id = $request->input('id'); |
||
| 104 | |||
| 105 | if (!array_key_exists($id, Cart::getContent())) { |
||
| 106 | $items = $this->addProduct($id); |
||
| 107 | Cart::add($items); |
||
| 108 | } |
||
| 109 | |||
| 110 | return redirect('show/cart'); |
||
| 111 | } catch (\Exception $ex) { |
||
| 112 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | public function showCart() |
||
| 117 | { |
||
| 118 | try { |
||
| 119 | $cartCollection = []; |
||
| 120 | $items = (\Session::get('items')); |
||
| 121 | |||
| 122 | $currency = 'INR'; |
||
| 123 | $cart_currency = 'INR'; |
||
| 124 | $attributes = []; |
||
| 125 | $cartCollection = $this->getCartCollection($items); |
||
| 126 | foreach ($cartCollection as $item) { |
||
| 127 | $attributes[] = $item->attributes; |
||
| 128 | $cart_currency = $attributes[0]['currency']; |
||
| 129 | $currency = $attributes[0]['currency']; |
||
| 130 | if (\Auth::user()) { |
||
| 131 | $cart_currency = $attributes[0]['currency']; |
||
| 132 | $user_currency = \Auth::user()->currency; |
||
| 133 | $user_country = \Auth::user()->country; |
||
| 134 | $user_state = \Auth::user()->state; |
||
| 135 | $currency = 'INR'; |
||
| 136 | if ($user_currency == 1 || $user_currency == 'USD') { |
||
| 137 | $currency = 'USD'; |
||
| 138 | } |
||
| 139 | if ($cart_currency != $currency) { |
||
| 140 | $id = $item->id; |
||
| 141 | Cart::remove($id); |
||
| 142 | $items = $this->addProduct($id); |
||
| 143 | |||
| 144 | Cart::add($items); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | return view('themes.default1.front.cart', compact('cartCollection', 'attributes')); |
||
| 150 | } catch (\Exception $ex) { |
||
| 151 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | public function checkTax($productid) |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * @param type $price |
||
| 378 | * |
||
| 379 | * @throws \Exception |
||
| 380 | * |
||
| 381 | * @return type |
||
| 382 | */ |
||
| 383 | public static function rounding($price) |
||
| 384 | { |
||
| 385 | try { |
||
| 386 | $tax_rule = new \App\Model\Payment\TaxOption(); |
||
| 387 | $rule = $tax_rule->findOrFail(1); |
||
| 388 | $rounding = $rule->rounding; |
||
| 389 | if ($rounding == 1) { |
||
| 390 | // $price = str_replace(',', '', $price); |
||
| 391 | |||
| 392 | return round($price); |
||
| 393 | } else { |
||
| 394 | return $price; |
||
| 395 | } |
||
| 396 | } catch (\Exception $ex) { |
||
| 397 | Bugsnag::notifyException($ex); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return type |
||
| 403 | */ |
||
| 404 | public function contactUs() |
||
| 405 | { |
||
| 406 | try { |
||
| 407 | return view('themes.default1.front.contact'); |
||
| 408 | } catch (\Exception $ex) { |
||
| 409 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param type $code |
||
| 415 | * |
||
| 416 | * @throws \Exception |
||
| 417 | * |
||
| 418 | * @return type |
||
| 419 | */ |
||
| 420 | public static function getCountryByCode($code) |
||
| 421 | { |
||
| 422 | try { |
||
| 423 | $country = \App\Model\Common\Country::where('country_code_char2', $code)->first(); |
||
| 424 | if ($country) { |
||
| 425 | return $country->country_name; |
||
| 426 | } |
||
| 427 | } catch (\Exception $ex) { |
||
| 428 | throw new \Exception($ex->getMessage()); |
||
| 429 | } |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param type $name |
||
| 434 | * |
||
| 435 | * @throws \Exception |
||
| 436 | * |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | public static function getTimezoneByName($name) |
||
| 440 | { |
||
| 441 | try { |
||
| 442 | if ($name) { |
||
| 443 | $timezone = \App\Model\Common\Timezone::where('name', $name)->first(); |
||
| 444 | if ($timezone) { |
||
| 445 | $timezone = $timezone->id; |
||
| 446 | } else { |
||
| 447 | $timezone = '114'; |
||
| 448 | } |
||
| 449 | } else { |
||
| 450 | $timezone = '114'; |
||
| 451 | } |
||
| 452 | |||
| 453 | return $timezone; |
||
| 454 | } catch (\Exception $ex) { |
||
| 455 | throw new \Exception($ex->getMessage()); |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param type $code |
||
| 461 | * |
||
| 462 | * @throws \Exception |
||
| 463 | * |
||
| 464 | * @return type |
||
| 465 | */ |
||
| 466 | public static function getStateByCode($code) |
||
| 467 | { |
||
| 468 | try { |
||
| 469 | $result = ['id' => '', 'name' => '']; |
||
| 470 | $subregion = \App\Model\Common\State::where('state_subdivision_code', $code)->first(); |
||
| 471 | if ($subregion) { |
||
| 472 | $result = ['id' => $subregion->state_subdivision_code, 'name' => $subregion->state_subdivision_name]; |
||
| 473 | //return ['id' => $subregion->state_subdivision_code, 'name' => $subregion->state_subdivision_name]; |
||
| 474 | } |
||
| 475 | |||
| 476 | return $result; |
||
| 477 | } catch (\Exception $ex) { |
||
| 478 | throw new \Exception($ex->getMessage()); |
||
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param type $id |
||
| 484 | * |
||
| 485 | * @throws \Exception |
||
| 486 | * |
||
| 487 | * @return type |
||
| 488 | */ |
||
| 489 | public static function getStateNameById($id) |
||
| 490 | { |
||
| 491 | try { |
||
| 492 | $name = ''; |
||
| 493 | $subregion = \App\Model\Common\State::where('state_subdivision_id', $id)->first(); |
||
| 494 | if ($subregion) { |
||
| 495 | $name = $subregion->state_subdivision_name; |
||
| 496 | } |
||
| 497 | |||
| 498 | return $name; |
||
| 499 | } catch (\Exception $ex) { |
||
| 500 | throw new \Exception($ex->getMessage()); |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param type $productid |
||
| 506 | * @param type $price |
||
| 507 | * @param type $cart |
||
| 508 | * @param type $cart1 |
||
| 509 | * @param type $shop |
||
| 510 | * |
||
| 511 | * @return type |
||
| 512 | */ |
||
| 513 | public static function calculateTax($productid, $price, $cart = 1, $cart1 = 0, $shop = 0) |
||
| 514 | { |
||
| 515 | try { |
||
| 516 | $template_controller = new TemplateController(); |
||
| 517 | $result = $template_controller->checkTax($productid, $price, $cart, $cart1, $shop); |
||
| 518 | |||
| 519 | $result = self::rounding($result); |
||
| 520 | |||
| 521 | return $result; |
||
| 522 | } catch (\Exception $ex) { |
||
| 523 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 524 | } |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param type $rate |
||
| 529 | * @param type $price |
||
| 530 | * |
||
| 531 | * @return type |
||
| 532 | */ |
||
| 533 | public static function taxValue($rate, $price) |
||
| 545 | } |
||
| 546 | } |
||
| 547 | |||
| 548 | |||
| 549 | /** |
||
| 550 | * @param type $productid |
||
| 551 | * @param type $userid |
||
| 552 | * @param type $planid |
||
| 553 | * |
||
| 554 | * @return type |
||
| 555 | */ |
||
| 556 | public function cost($productid, $userid = '', $planid = '') |
||
| 557 | { |
||
| 558 | try { |
||
| 559 | $cost = $this->planCost($productid, $userid, $planid); |
||
| 560 | if ($cost == 0) { |
||
| 561 | $cost = $this->productCost($productid, $userid); |
||
| 562 | } |
||
| 563 | |||
| 564 | return self::rounding($cost); |
||
| 565 | } catch (\Exception $ex) { |
||
| 566 | // throw new \Exception($ex->getMessage()); |
||
| 567 | } |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @param type $productid |
||
| 572 | * @param type $userid |
||
| 573 | * |
||
| 574 | * @throws \Exception |
||
| 575 | * |
||
| 576 | * @return type |
||
| 577 | */ |
||
| 578 | public function productCost($productid, $userid = '') |
||
| 579 | { |
||
| 580 | try { |
||
| 581 | $sales = 0; |
||
| 582 | $currency = $this->currency($userid); |
||
| 583 | $product = $this->product->find($productid); |
||
| 584 | |||
| 585 | // $price = $product->price()->where('currency', $currency)->first(); |
||
| 586 | $plan_id = Plan::where('product', $productid)->pluck('id')->first(); |
||
| 587 | $price = PlanPrice::where('plan_id', $plan_id)->where('currency', $currency)->pluck('add_price')->first(); |
||
| 588 | if ($price) { |
||
| 589 | $sales = $price; |
||
| 590 | // if ($sales == 0) { |
||
| 591 | // $sales = $price->price; |
||
| 592 | // } |
||
| 593 | } |
||
| 594 | //} |
||
| 595 | |||
| 596 | return $sales; |
||
| 597 | } catch (\Exception $ex) { |
||
| 598 | throw new \Exception($ex->getMessage()); |
||
| 599 | } |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @param type $productid |
||
| 604 | * @param type $userid |
||
| 605 | * @param type $planid |
||
| 606 | * |
||
| 607 | * @throws \Exception |
||
| 608 | * |
||
| 609 | * @return type |
||
| 610 | */ |
||
| 611 | public function planCost($productid, $userid, $planid = '') |
||
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | |||
| 648 | /** |
||
| 649 | * @throws \Exception |
||
| 650 | * |
||
| 651 | * @return bool |
||
| 652 | */ |
||
| 653 | public function checkCurrencySession() |
||
| 663 | } |
||
| 664 | } |
||
| 666 |