| Total Complexity | 51 |
| Total Lines | 456 |
| 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 | use TaxCalculation; |
||
|
|
|||
| 24 | |||
| 25 | public $templateController; |
||
| 26 | public $product; |
||
| 27 | public $currency; |
||
| 28 | public $addons; |
||
| 29 | public $addonRelation; |
||
| 30 | public $licence; |
||
| 31 | public $tax_option; |
||
| 32 | public $tax_by_state; |
||
| 33 | public $setting; |
||
| 34 | |||
| 35 | public function __construct() |
||
| 60 | } |
||
| 61 | |||
| 62 | public function productList(Request $request) |
||
| 63 | { |
||
| 64 | $cont = new \App\Http\Controllers\Front\PageController(); |
||
| 65 | $location = $cont->getLocation(); |
||
| 66 | $country = $this->findCountryByGeoip($location['iso_code']); |
||
| 67 | $states = $this->findStateByRegionId($location['iso_code']); |
||
| 68 | $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray(); |
||
| 69 | $state_code = $location['iso_code'].'-'.$location['state']; |
||
| 70 | $state = $this->getStateByCode($state_code); |
||
| 71 | $mobile_code = $this->getMobileCodeByIso($location['iso_code']); |
||
| 72 | $currency = $this->currency(); |
||
| 73 | |||
| 74 | \Session::put('currency', $currency); |
||
| 75 | if (!\Session::has('currency')) { |
||
| 76 | \Session::put('currency', 'INR'); |
||
| 77 | } |
||
| 78 | |||
| 79 | try { |
||
| 80 | $page_controller = new PageController(); |
||
| 81 | |||
| 82 | return $page_controller->cart(); |
||
| 83 | } catch (\Exception $ex) { |
||
| 84 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /* |
||
| 89 | * The first request to the cart Page comes here |
||
| 90 | * Get Plan id and Product id as Request |
||
| 91 | * |
||
| 92 | * @param int $plan Planid; |
||
| 93 | * @param int $id Productid; |
||
| 94 | */ |
||
| 95 | public function cart(Request $request) |
||
| 96 | { |
||
| 97 | try { |
||
| 98 | $plan = ''; |
||
| 99 | if ($request->has('subscription')) {//put he Plan id sent into session variable |
||
| 100 | $plan = $request->get('subscription'); |
||
| 101 | Session::put('plan', $plan); |
||
| 102 | } |
||
| 103 | $id = $request->input('id'); |
||
| 104 | if (!array_key_exists($id, Cart::getContent())) { |
||
| 105 | $items = $this->addProduct($id); |
||
| 106 | \Cart::add($items); //Add Items To the Cart Collection |
||
| 107 | } |
||
| 108 | |||
| 109 | return redirect('show/cart'); |
||
| 110 | } catch (\Exception $ex) { |
||
| 111 | app('log')->error($ex->getMessage()); |
||
| 112 | Bugsnag::notifyException($ex->getMessage()); |
||
| 113 | |||
| 114 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /* |
||
| 119 | * Show the cart with all the Cart Attributes and Cart Collections |
||
| 120 | * Link: https://github.com/darryldecode/laravelshoppingcart |
||
| 121 | */ |
||
| 122 | public function showCart() |
||
| 123 | { |
||
| 124 | try { |
||
| 125 | $cartCollection = Cart::getContent(); |
||
| 126 | $attributes = []; |
||
| 127 | foreach ($cartCollection as $item) { |
||
| 128 | $attributes[] = $item->attributes; |
||
| 129 | $cart_currency = $attributes[0]['currency']['currency']; |
||
| 130 | if (\Auth::user()) {//If User is Loggen in and his currency changes after logginng in then remove his previous order from cart |
||
| 131 | $currency = \Auth::user()->currency; |
||
| 132 | if ($cart_currency != $currency) { |
||
| 133 | $id = $item->id; |
||
| 134 | Cart::session(\Auth::user()->id)->remove($id); |
||
| 135 | $items = $this->addProduct($id); |
||
| 136 | Cart::add($items); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | return view('themes.default1.front.cart', compact('cartCollection', 'attributes')); |
||
| 142 | } catch (\Exception $ex) { |
||
| 143 | app('log')->error($ex->getMessage()); |
||
| 144 | Bugsnag::notifyException($ex->getMessage()); |
||
| 145 | |||
| 146 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | public function checkTax($productid, $user_state = '', $user_country = '') |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return type |
||
| 254 | */ |
||
| 255 | public function addCouponUpdate() |
||
| 256 | { |
||
| 257 | try { |
||
| 258 | $code = \Input::get('coupon'); |
||
| 259 | $cart = Cart::getContent(); |
||
| 260 | foreach ($cart as $item) { |
||
| 261 | $id = $item->id; |
||
| 262 | } |
||
| 263 | $promo_controller = new \App\Http\Controllers\Payment\PromotionController(); |
||
| 264 | $result = $promo_controller->checkCode($code, $id); |
||
| 265 | if ($result == 'success') { |
||
| 266 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 267 | } |
||
| 268 | |||
| 269 | return redirect()->back(); |
||
| 270 | } catch (\Exception $ex) { |
||
| 271 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return type |
||
| 277 | */ |
||
| 278 | public function contactUs() |
||
| 279 | { |
||
| 280 | try { |
||
| 281 | return view('themes.default1.front.contact'); |
||
| 282 | } catch (\Exception $ex) { |
||
| 283 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param Request $request |
||
| 289 | * |
||
| 290 | * @return type |
||
| 291 | */ |
||
| 292 | public function postContactUs(Request $request) |
||
| 293 | { |
||
| 294 | $this->validate($request, [ |
||
| 295 | 'name' => 'required', |
||
| 296 | 'email' => 'required|email', |
||
| 297 | 'message' => 'required', |
||
| 298 | ]); |
||
| 299 | |||
| 300 | $set = new \App\Model\Common\Setting(); |
||
| 301 | $set = $set->findOrFail(1); |
||
| 302 | |||
| 303 | try { |
||
| 304 | $from = $set->email; |
||
| 305 | $fromname = $set->company; |
||
| 306 | $toname = ''; |
||
| 307 | $to = $set->company_email; |
||
| 308 | $data = ''; |
||
| 309 | $data .= 'Name: '.strip_tags($request->input('name')).'<br/>'; |
||
| 310 | $data .= 'Email: '.strip_tags($request->input('email')).'<br/>'; |
||
| 311 | $data .= 'Message: '.strip_tags($request->input('message')).'<br/>'; |
||
| 312 | $data .= 'Mobile: '.strip_tags($request->input('country_code').$request->input('Mobile')).'<br/>'; |
||
| 313 | $subject = 'Faveo billing enquiry'; |
||
| 314 | $this->templateController->Mailing($from, $to, $data, $subject, [], $fromname, $toname); |
||
| 315 | //$this->templateController->Mailing($from, $to, $data, $subject); |
||
| 316 | return redirect()->back()->with('success', 'Your message was sent successfully. Thanks.'); |
||
| 317 | } catch (\Exception $ex) { |
||
| 318 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param type $code |
||
| 324 | * |
||
| 325 | * @throws \Exception |
||
| 326 | * |
||
| 327 | * @return type |
||
| 328 | */ |
||
| 329 | public static function getCountryByCode($code) |
||
| 330 | { |
||
| 331 | try { |
||
| 332 | $country = \App\Model\Common\Country::where('country_code_char2', $code)->first(); |
||
| 333 | if ($country) { |
||
| 334 | return $country->nicename; |
||
| 335 | } |
||
| 336 | } catch (\Exception $ex) { |
||
| 337 | throw new \Exception($ex->getMessage()); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param type $name |
||
| 343 | * |
||
| 344 | * @throws \Exception |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public static function getTimezoneByName($name) |
||
| 349 | { |
||
| 350 | try { |
||
| 351 | $timezone = \App\Model\Common\Timezone::where('name', $name)->first(); |
||
| 352 | if ($timezone) { |
||
| 353 | $timezone = $timezone->id; |
||
| 354 | } else { |
||
| 355 | $timezone = '114'; |
||
| 356 | } |
||
| 357 | |||
| 358 | return $timezone; |
||
| 359 | } catch (\Exception $ex) { |
||
| 360 | throw new \Exception($ex->getMessage()); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param type $code |
||
| 366 | * |
||
| 367 | * @throws \Exception |
||
| 368 | * |
||
| 369 | * @return type |
||
| 370 | */ |
||
| 371 | public static function getStateByCode($code) |
||
| 372 | { |
||
| 373 | try { |
||
| 374 | $result = ['id' => '', 'name' => '']; |
||
| 375 | |||
| 376 | $subregion = \App\Model\Common\State::where('state_subdivision_code', $code)->first(); |
||
| 377 | if ($subregion) { |
||
| 378 | $result = ['id' => $subregion->state_subdivision_code, |
||
| 379 | 'name' => $subregion->state_subdivision_name, ]; |
||
| 380 | } |
||
| 381 | |||
| 382 | return $result; |
||
| 383 | } catch (\Exception $ex) { |
||
| 384 | throw new \Exception($ex->getMessage()); |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param type $id |
||
| 390 | * |
||
| 391 | * @throws \Exception |
||
| 392 | * |
||
| 393 | * @return type |
||
| 394 | */ |
||
| 395 | public static function getStateNameById($id) |
||
| 396 | { |
||
| 397 | try { |
||
| 398 | $name = ''; |
||
| 399 | $subregion = \App\Model\Common\State::where('state_subdivision_id', $id)->first(); |
||
| 400 | if ($subregion) { |
||
| 401 | $name = $subregion->state_subdivision_name; |
||
| 402 | } |
||
| 403 | |||
| 404 | return $name; |
||
| 405 | } catch (\Exception $ex) { |
||
| 406 | throw new \Exception($ex->getMessage()); |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param type $userid |
||
| 412 | * |
||
| 413 | * @throws \Exception |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public static function currency($userid = '') |
||
| 418 | { |
||
| 419 | try { |
||
| 420 | $currency = Setting::find(1)->default_currency; |
||
| 421 | $currency_symbol = Setting::find(1)->default_symbol; |
||
| 422 | if (!\Auth::user()) {//When user is not logged in |
||
| 423 | $cont = new \App\Http\Controllers\Front\PageController(); |
||
| 424 | $location = $cont->getLocation(); |
||
| 425 | $country = self::findCountryByGeoip($location['iso_code']); |
||
| 426 | $userCountry = Country::where('country_code_char2', $country)->first(); |
||
| 427 | $currencyStatus = $userCountry->currency->status; |
||
| 428 | if ($currencyStatus == 1) { |
||
| 429 | $currency = $userCountry->currency->code; |
||
| 430 | $currency_symbol = $userCountry->currency->symbol; |
||
| 431 | } |
||
| 432 | } |
||
| 433 | if (\Auth::user()) { |
||
| 434 | $currency = \Auth::user()->currency; |
||
| 435 | $currency_symbol = \Auth::user()->currency_symbol; |
||
| 436 | } |
||
| 437 | if ($userid != '') {//For Admin Panel Clients |
||
| 438 | $currencyAndSymbol = self::getCurrency($userid); |
||
| 439 | $currency = $currencyAndSymbol['currency']; |
||
| 440 | $currency_symbol = $currencyAndSymbol['symbol']; |
||
| 441 | } |
||
| 442 | |||
| 443 | return ['currency'=>$currency, 'symbol'=>$currency_symbol]; |
||
| 444 | } catch (\Exception $ex) { |
||
| 445 | throw new \Exception($ex->getMessage()); |
||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | /* |
||
| 450 | * Get Currency And Symbol For Admin Panel Clients |
||
| 451 | */ |
||
| 452 | public static function getCurrency($userid) |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param int $productid |
||
| 463 | * @param int $userid |
||
| 464 | * @param int $planid |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | public function cost($productid, $userid = '', $planid = '') |
||
| 480 |