| Total Complexity | 60 |
| Total Lines | 527 |
| 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 = '') |
||
| 151 | { |
||
| 152 | try { |
||
| 153 | $taxCondition = []; |
||
| 154 | $tax_attribute = []; |
||
| 155 | $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0]; |
||
| 156 | $taxCondition[0] = new \Darryldecode\Cart\CartCondition([ |
||
| 157 | 'name' => 'null', |
||
| 158 | 'type' => 'tax', |
||
| 159 | 'target' => 'item', |
||
| 160 | 'value' => '0%', |
||
| 161 | ]); |
||
| 162 | $cont = new \App\Http\Controllers\Front\PageController(); |
||
| 163 | $location = $cont->getLocation(); |
||
| 164 | $country = $this->findCountryByGeoip($location['iso_code']); //Get country by geopip |
||
| 165 | $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray(); |
||
| 166 | $state_code = $location['iso_code'].'-'.$location['state']; |
||
| 167 | $state = $this->getStateByCode($state_code); //match the geoip state with billing table state. |
||
| 168 | $geoip_state = $this->getGeoipState($state_code, $user_state); |
||
| 169 | $geoip_country = $this->getGeoipCountry($location['iso_code'], $user_country); |
||
| 170 | |||
| 171 | if ($this->tax_option->findOrFail(1)->inclusive == 0) { |
||
| 172 | $tax_enable = $this->tax_option->findOrFail(1)->tax_enable; |
||
| 173 | //Check the state of user for calculating GST(cgst,igst,utgst,sgst) |
||
| 174 | $user_state = TaxByState::where('state_code', $geoip_state)->first(); |
||
| 175 | $origin_state = $this->setting->first()->state; //Get the State of origin |
||
| 176 | $tax_class_id = TaxProductRelation::where('product_id', $productid)->pluck('tax_class_id')->toArray(); |
||
| 177 | |||
| 178 | if (count($tax_class_id) > 0) {//If the product is allowed for tax (Check in tax_product relation table) |
||
| 179 | if ($tax_enable == 1) {//If GST is Enabled |
||
| 180 | |||
| 181 | $details = $this->getDetailsWhenUserStateIsIndian( |
||
| 182 | $user_state, |
||
| 183 | $origin_state, |
||
| 184 | $productid, |
||
| 185 | $geoip_state, |
||
| 186 | $geoip_country |
||
| 187 | ); |
||
| 188 | |||
| 189 | $c_gst = $details['cgst']; |
||
| 190 | $s_gst = $details['sgst']; |
||
| 191 | $i_gst = $details['igst']; |
||
| 192 | $ut_gst = $details['utgst']; |
||
| 193 | |||
| 194 | $state_code = $details['statecode']; |
||
| 195 | |||
| 196 | $status = $details['status']; |
||
| 197 | $taxes = $details['taxes']; |
||
| 198 | $status = $details['status']; |
||
| 199 | $value = $details['value']; |
||
| 200 | $rate = $details['rate']; |
||
| 201 | foreach ($taxes as $key => $tax) { |
||
| 202 | //All the da a attribute that is sent to the checkout Page if tax_compound=0 |
||
| 203 | if ($taxes[0]) { |
||
| 204 | $tax_attribute[$key] = ['name' => $tax->name, 'c_gst'=>$c_gst, |
||
| 205 | |||
| 206 | 's_gst' => $s_gst, 'i_gst'=>$i_gst, 'ut_gst'=>$ut_gst, |
||
| 207 | 'state' => $state_code, 'origin_state'=>$origin_state, |
||
| 208 | 'tax_enable' => $tax_enable, 'rate'=>$value, 'status'=>$status, ]; |
||
| 209 | |||
| 210 | $taxCondition[0] = new \Darryldecode\Cart\CartCondition([ |
||
| 211 | 'name' => 'no compound', 'type' => 'tax', |
||
| 212 | 'target' => 'item', 'value' => $value, |
||
| 213 | ]); |
||
| 214 | } else { |
||
| 215 | $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0]; |
||
| 216 | $taxCondition[0] = new \Darryldecode\Cart\CartCondition([ |
||
| 217 | 'name' => 'null', 'type' => 'tax', |
||
| 218 | 'target' => 'item', 'value' => '0%', |
||
| 219 | 'rate' => 0, 'tax_enable' =>0, |
||
| 220 | ]); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } elseif ($tax_enable == 0) {//If Tax enable is 0 and other tax is available |
||
| 224 | |||
| 225 | $details = $this->whenOtherTaxAvailableAndTaxNotEnable($tax_class_id, $productid, $geoip_state, $geoip_country); |
||
| 226 | |||
| 227 | $taxes = $details['taxes']; |
||
| 228 | $value = $details['value']; |
||
| 229 | $status = $details['status']; |
||
| 230 | foreach ($taxes as $key => $tax) { |
||
| 231 | $tax_attribute[$key] = ['name' => $tax->name, |
||
| 232 | 'rate' => $value, 'tax_enable'=>0, 'status' => $status, ]; |
||
| 233 | $taxCondition[$key] = new \Darryldecode\Cart\CartCondition([ |
||
| 234 | 'name' => $tax->name, |
||
| 235 | 'type' => 'tax', |
||
| 236 | 'target' => 'item', |
||
| 237 | 'value' => $value, |
||
| 238 | ]); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | return ['conditions' => $taxCondition, 'tax_attributes'=> $tax_attribute]; |
||
| 245 | } catch (\Exception $ex) { |
||
| 246 | Bugsnag::notifyException($ex); |
||
| 247 | |||
| 248 | throw new \Exception('Can not check the tax'); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | public function cartRemove(Request $request) |
||
| 253 | { |
||
| 254 | $id = $request->input('id'); |
||
| 255 | Cart::remove($id); |
||
| 256 | |||
| 257 | return 'success'; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return type |
||
| 262 | */ |
||
| 263 | public function addCouponUpdate() |
||
| 264 | { |
||
| 265 | try { |
||
| 266 | $code = \Input::get('coupon'); |
||
| 267 | $cart = Cart::getContent(); |
||
| 268 | foreach ($cart as $item) { |
||
| 269 | $id = $item->id; |
||
| 270 | } |
||
| 271 | $promo_controller = new \App\Http\Controllers\Payment\PromotionController(); |
||
| 272 | $result = $promo_controller->checkCode($code, $id); |
||
| 273 | if ($result == 'success') { |
||
| 274 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 275 | } |
||
| 276 | |||
| 277 | return redirect()->back(); |
||
| 278 | } catch (\Exception $ex) { |
||
| 279 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param type $price |
||
| 285 | * |
||
| 286 | * @throws \Exception |
||
| 287 | * |
||
| 288 | * @return type |
||
| 289 | */ |
||
| 290 | public static function rounding($price) |
||
| 291 | { |
||
| 292 | try { |
||
| 293 | $tax_rule = new \App\Model\Payment\TaxOption(); |
||
| 294 | $rule = $tax_rule->findOrFail(1); |
||
| 295 | $rounding = $rule->rounding; |
||
| 296 | |||
| 297 | return $price; |
||
| 298 | } catch (\Exception $ex) { |
||
| 299 | Bugsnag::notifyException($ex); |
||
| 300 | // throw new \Exception('error in get tax priority'); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return type |
||
| 306 | */ |
||
| 307 | public function contactUs() |
||
| 308 | { |
||
| 309 | try { |
||
| 310 | return view('themes.default1.front.contact'); |
||
| 311 | } catch (\Exception $ex) { |
||
| 312 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param Request $request |
||
| 318 | * |
||
| 319 | * @return type |
||
| 320 | */ |
||
| 321 | public function postContactUs(Request $request) |
||
| 322 | { |
||
| 323 | $this->validate($request, [ |
||
| 324 | 'name' => 'required', |
||
| 325 | 'email' => 'required|email', |
||
| 326 | 'message' => 'required', |
||
| 327 | ]); |
||
| 328 | |||
| 329 | $set = new \App\Model\Common\Setting(); |
||
| 330 | $set = $set->findOrFail(1); |
||
| 331 | |||
| 332 | try { |
||
| 333 | $from = $set->email; |
||
| 334 | $fromname = $set->company; |
||
| 335 | $toname = ''; |
||
| 336 | $to = $set->company_email; |
||
| 337 | $data = ''; |
||
| 338 | $data .= 'Name: '.strip_tags($request->input('name')).'<br/>'; |
||
| 339 | $data .= 'Email: '.strip_tags($request->input('email')).'<br/>'; |
||
| 340 | $data .= 'Message: '.strip_tags($request->input('message')).'<br/>'; |
||
| 341 | $data .= 'Mobile: '.strip_tags($request->input('country_code').$request->input('Mobile')).'<br/>'; |
||
| 342 | $subject = 'Faveo billing enquiry'; |
||
| 343 | $this->templateController->Mailing($from, $to, $data, $subject, [], $fromname, $toname); |
||
| 344 | //$this->templateController->Mailing($from, $to, $data, $subject); |
||
| 345 | return redirect()->back()->with('success', 'Your message was sent successfully. Thanks.'); |
||
| 346 | } catch (\Exception $ex) { |
||
| 347 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param type $code |
||
| 353 | * |
||
| 354 | * @throws \Exception |
||
| 355 | * |
||
| 356 | * @return type |
||
| 357 | */ |
||
| 358 | public static function getCountryByCode($code) |
||
| 359 | { |
||
| 360 | try { |
||
| 361 | $country = \App\Model\Common\Country::where('country_code_char2', $code)->first(); |
||
| 362 | if ($country) { |
||
| 363 | return $country->nicename; |
||
| 364 | } |
||
| 365 | } catch (\Exception $ex) { |
||
| 366 | throw new \Exception($ex->getMessage()); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param type $name |
||
| 372 | * |
||
| 373 | * @throws \Exception |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public static function getTimezoneByName($name) |
||
| 378 | { |
||
| 379 | try { |
||
| 380 | $timezone = \App\Model\Common\Timezone::where('name', $name)->first(); |
||
| 381 | if ($timezone) { |
||
| 382 | $timezone = $timezone->id; |
||
| 383 | } else { |
||
| 384 | $timezone = '114'; |
||
| 385 | } |
||
| 386 | |||
| 387 | return $timezone; |
||
| 388 | } catch (\Exception $ex) { |
||
| 389 | throw new \Exception($ex->getMessage()); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param type $code |
||
| 395 | * |
||
| 396 | * @throws \Exception |
||
| 397 | * |
||
| 398 | * @return type |
||
| 399 | */ |
||
| 400 | public static function getStateByCode($code) |
||
| 401 | { |
||
| 402 | try { |
||
| 403 | $result = ['id' => '', 'name' => '']; |
||
| 404 | |||
| 405 | $subregion = \App\Model\Common\State::where('state_subdivision_code', $code)->first(); |
||
| 406 | if ($subregion) { |
||
| 407 | $result = ['id' => $subregion->state_subdivision_code, |
||
| 408 | 'name' => $subregion->state_subdivision_name, ]; |
||
| 409 | } |
||
| 410 | |||
| 411 | return $result; |
||
| 412 | } catch (\Exception $ex) { |
||
| 413 | throw new \Exception($ex->getMessage()); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param type $id |
||
| 419 | * |
||
| 420 | * @throws \Exception |
||
| 421 | * |
||
| 422 | * @return type |
||
| 423 | */ |
||
| 424 | public static function getStateNameById($id) |
||
| 425 | { |
||
| 426 | try { |
||
| 427 | $name = ''; |
||
| 428 | $subregion = \App\Model\Common\State::where('state_subdivision_id', $id)->first(); |
||
| 429 | if ($subregion) { |
||
| 430 | $name = $subregion->state_subdivision_name; |
||
| 431 | } |
||
| 432 | |||
| 433 | return $name; |
||
| 434 | } catch (\Exception $ex) { |
||
| 435 | throw new \Exception($ex->getMessage()); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param type $rate |
||
| 441 | * @param type $price |
||
| 442 | * |
||
| 443 | * @return type |
||
| 444 | */ |
||
| 445 | public static function taxValue($rate, $price) |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param type $userid |
||
| 465 | * |
||
| 466 | * @throws \Exception |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | public static function currency($userid = '') |
||
| 471 | { |
||
| 472 | try { |
||
| 473 | $currency = Setting::find(1)->default_currency; |
||
| 474 | $currency_symbol = Setting::find(1)->default_symbol; |
||
| 475 | if (!\Auth::user()) {//When user is not logged in |
||
| 476 | $cont = new \App\Http\Controllers\Front\PageController(); |
||
| 477 | $location = $cont->getLocation(); |
||
| 478 | $country = self::findCountryByGeoip($location['iso_code']); |
||
| 479 | $userCountry = Country::where('country_code_char2', $country)->first(); |
||
| 480 | $currencyStatus = $userCountry->currency->status; |
||
| 481 | if ($currencyStatus == 1) { |
||
| 482 | $currency = $userCountry->currency->code; |
||
| 483 | $currency_symbol = $userCountry->currency->symbol; |
||
| 484 | } |
||
| 485 | } |
||
| 486 | if (\Auth::user()) { |
||
| 487 | $currency = \Auth::user()->currency; |
||
| 488 | $currency_symbol = \Auth::user()->currency_symbol; |
||
| 489 | } |
||
| 490 | if ($userid != '') {//For Admin Panel Clients |
||
| 491 | $currencyAndSymbol = self::getCurrency($userid); |
||
| 492 | $currency = $currencyAndSymbol['currency']; |
||
| 493 | $currency_symbol = $currencyAndSymbol['symbol']; |
||
| 494 | } |
||
| 495 | |||
| 496 | return ['currency'=>$currency, 'symbol'=>$currency_symbol]; |
||
| 497 | } catch (\Exception $ex) { |
||
| 498 | throw new \Exception($ex->getMessage()); |
||
| 499 | } |
||
| 500 | } |
||
| 501 | |||
| 502 | /* |
||
| 503 | * Get Currency And Symbol For Admin Panel Clients |
||
| 504 | */ |
||
| 505 | public static function getCurrency($userid) |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param int $productid |
||
| 516 | * @param int $userid |
||
| 517 | * @param int $planid |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | public function cost($productid, $userid = '', $planid = '') |
||
| 522 | { |
||
| 523 | try { |
||
| 524 | $cost = $this->planCost($productid, $userid, $planid); |
||
| 525 | |||
| 526 | return $cost; |
||
| 527 | } catch (\Exception $ex) { |
||
| 528 | Bugsnag::notifyException($ex->getMessage()); |
||
| 529 | app('log')->error($ex->getMessage()); |
||
| 530 | } |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @throws \Exception |
||
| 535 | * |
||
| 536 | * @return bool |
||
| 537 | */ |
||
| 538 | public function checkCurrencySession() |
||
| 548 | } |
||
| 549 | } |
||
| 551 |