| Total Complexity | 61 |
| Total Lines | 217 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BaseTemplateController 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 BaseTemplateController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class BaseTemplateController extends Controller |
||
| 10 | { |
||
| 11 | |||
| 12 | public function ifStatement($rate, $price, $cart1, $shop1, $country = '', $state = '') |
||
| 13 | { |
||
| 14 | try { |
||
| 15 | $tax_rule = $this->tax_rule->find(1); |
||
| 16 | $product = $tax_rule->inclusive; |
||
| 17 | $shop = $tax_rule->shop_inclusive; |
||
| 18 | $cart = $tax_rule->cart_inclusive; |
||
| 19 | $result = $price; |
||
| 20 | $controller = new \App\Http\Controllers\Front\GetPageTemplateController(); |
||
| 21 | $location = $controller->getLocation(); |
||
| 22 | |||
| 23 | $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']); |
||
| 24 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']); |
||
| 25 | $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray(); |
||
| 26 | $state_code = $location['countryCode'].'-'.$location['region']; |
||
| 27 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 28 | $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']); |
||
| 29 | $country_iso = $location['countryCode']; |
||
| 30 | |||
| 31 | $geoip_country = ''; |
||
| 32 | $geoip_state = ''; |
||
| 33 | if (\Auth::user()) { |
||
| 34 | $geoip_country = \Auth::user()->country; |
||
| 35 | $geoip_state = \Auth::user()->state; |
||
| 36 | } |
||
| 37 | if ($geoip_country == '') { |
||
| 38 | $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($country_iso); |
||
| 39 | } |
||
| 40 | $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 41 | if ($geoip_state == '') { |
||
| 42 | if (array_key_exists('id', $geoip_state_array)) { |
||
| 43 | $geoip_state = $geoip_state_array['id']; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | $result = $this->getResult($country,$geoip_country,$state,$geoip_state,$shop,$cart,$cart1, $shop1,$rate,$product,$price); |
||
| 47 | |||
| 48 | } catch (\Exception $ex) { |
||
| 49 | Bugsnag::notifyException($ex); |
||
|
|
|||
| 50 | |||
| 51 | throw new \Exception($ex->getMessage()); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | public function getResult($country,$geoip_country,$state,$geoip_state,$shop,$cart,$cart1, $shop1,$rate,$product,$price) |
||
| 56 | { |
||
| 57 | if ($country == $geoip_country || $state == $geoip_state || ($country == '' && $state == '')) { |
||
| 58 | $result = $this->getCartResult($product,$shop,$cart,$rate,$price,$cart1,$shop1); |
||
| 59 | |||
| 60 | } |
||
| 61 | return $result; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function getCartResult($product,$shop,$cart,$rate,$price,$cart1,$shop1) |
||
| 65 | { |
||
| 66 | if ($product==1) |
||
| 67 | { |
||
| 68 | $result = $this->getTotalSub($shop,$cart,$rate,$price,$cart1,$shop1); |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($product == 0){ |
||
| 72 | $result= $this->getTotalCart($shop,$cart,$price,$cart1,$shop1,$shop); |
||
| 73 | } |
||
| 74 | |||
| 75 | return $result; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getTotalCart($shop,$cart,$rate,$price,$cart1,$shop1) |
||
| 79 | { |
||
| 80 | if ($shop == 0 && $cart == 0) { |
||
| 81 | $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1 = 0); |
||
| 82 | } |
||
| 83 | if ($shop == 1 && $cart == 1) { |
||
| 84 | $result = $this->calculateTotalcart($rate, $price, $cart1, $shop1); |
||
| 85 | } |
||
| 86 | if ($shop == 1 && $cart == 0) { |
||
| 87 | $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1); |
||
| 88 | } |
||
| 89 | if ($shop == 0 && $cart == 1) { |
||
| 90 | $result = $this->calculateTotalcart($rate, $price, $cart = 1, $shop = 1); |
||
| 91 | } |
||
| 92 | return $result; |
||
| 93 | } |
||
| 94 | |||
| 95 | |||
| 96 | public function getTotalSub($shop,$cart,$rate,$price,$cart1,$shop1) |
||
| 97 | { |
||
| 98 | if ($shop == 1 && $cart == 1) { |
||
| 99 | $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1 = 0); |
||
| 100 | } |
||
| 101 | if ($shop == 0 && $cart == 0) { |
||
| 102 | $result = $this->calculateSub($rate, $price, $cart1 = 1, $shop1 = 1); |
||
| 103 | } |
||
| 104 | if ($shop == 1 && $cart == 0) { |
||
| 105 | $result = $this->calculateSub($rate, $price, $cart1, $shop1 = 0); |
||
| 106 | } |
||
| 107 | if ($shop == 0 && $cart == 1) { |
||
| 108 | $result = $this->calculateSub($rate, $price, $cart1 = 0, $shop1); |
||
| 109 | } |
||
| 110 | return $result; |
||
| 111 | |||
| 112 | } |
||
| 113 | |||
| 114 | public function calculateTotalcart($rate, $price, $cart, $shop) |
||
| 115 | { |
||
| 116 | if (($cart == 1 && $shop == 1) || ($cart == 1 && $shop == 0) || ($cart == 0 && $shop == 1)) { |
||
| 117 | $tax_amount = $price * ($rate / 100); |
||
| 118 | $total = $price + $tax_amount; |
||
| 119 | return $total; |
||
| 120 | } |
||
| 121 | return $price; |
||
| 122 | } |
||
| 123 | |||
| 124 | |||
| 125 | public function calculateSub($rate, $price, $cart, $shop) |
||
| 126 | { |
||
| 127 | if (($cart == 1 && $shop == 1) || ($cart == 1 && $shop == 0) || ($cart == 0 && $shop == 1)) { |
||
| 128 | $total = $price / (($rate / 100) + 1); |
||
| 129 | return $total; |
||
| 130 | } |
||
| 131 | return $price; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getPrice($price,$currency,$value,$cost) |
||
| 142 | } |
||
| 143 | |||
| 144 | public function prices($id) |
||
| 145 | { |
||
| 146 | $plan = new Plan(); |
||
| 147 | $plans = $plan->where('product', $id)->get(); |
||
| 148 | $price = array(); |
||
| 149 | $cart_controller = new \App\Http\Controllers\Front\CartController(); |
||
| 150 | $currency = $cart_controller->currency(); |
||
| 151 | |||
| 152 | foreach ($plans as $value) { |
||
| 153 | $cost = $value->planPrice()->where('currency', $currency)->first()->add_price; |
||
| 154 | |||
| 155 | $cost = \App\Http\Controllers\Front\CartController::rounding($cost); |
||
| 156 | $months = round($value->days / 30 / 12); |
||
| 157 | $price = $this->getPrice($price,$currency,$value,$cost); |
||
| 158 | |||
| 159 | } |
||
| 160 | $this->leastAmount($id); |
||
| 161 | |||
| 162 | return $price; |
||
| 163 | } |
||
| 164 | |||
| 165 | public function withoutTaxRelation($productid, $currency) |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | |||
| 179 | public function taxProcess($taxes, $price, $cart, $shop) |
||
| 180 | { |
||
| 181 | try { |
||
| 182 | $rate = ''; |
||
| 183 | $tax_amount = ''; |
||
| 184 | foreach ($taxes as $tax) { |
||
| 185 | if ($tax->compound != 1) { |
||
| 186 | $rate += $tax->rate; |
||
| 187 | } else { |
||
| 188 | $rate = $tax->rate; |
||
| 189 | } |
||
| 190 | $tax_amount = $this->ifStatement($rate, $price, $cart, $shop, $tax->country, $tax->state); |
||
| 191 | } |
||
| 192 | return $tax_amount; |
||
| 193 | } catch (\Exception $ex) { |
||
| 194 | Bugsnag::notifyException($ex); |
||
| 195 | |||
| 196 | throw new \Exception($ex->getMessage()); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | public function getTaxAmount($cart,$taxes, $price, $cart1, $shop) |
||
| 201 | { |
||
| 202 | if ($cart == 1) { |
||
| 203 | $tax_amount = $this->taxProcess($taxes, $price, $cart1, $shop); |
||
| 204 | } else { |
||
| 205 | $rate = ''; |
||
| 206 | foreach ($taxes as $tax) { |
||
| 207 | if ($tax->compound != 1) { |
||
| 208 | $rate += $tax->rate; |
||
| 209 | } else { |
||
| 210 | $rate = $tax->rate; |
||
| 211 | $price = $this->calculateTotal($rate, $price); |
||
| 212 | } |
||
| 213 | $tax_amount = $this->calculateTotal($rate, $price); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | return $tax_amount; |
||
| 217 | } |
||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | public function calculateTotal($rate, $price) |
||
| 226 | } |
||
| 227 | } |
||
| 228 |