| Total Complexity | 52 |
| Total Lines | 223 |
| 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 |
||
| 8 | class BaseTemplateController extends ExtendedBaseTemplateController |
||
| 9 | { |
||
| 10 | public function ifStatement($rate, $price, $cart1, $shop1, $country = '', $state = '') |
||
| 11 | { |
||
| 12 | try { |
||
| 13 | $tax_rule = $this->tax_rule->find(1); |
||
| 14 | $product = $tax_rule->inclusive; |
||
| 15 | $shop = $tax_rule->shop_inclusive; |
||
| 16 | $cart = $tax_rule->cart_inclusive; |
||
| 17 | $result = $price; |
||
| 18 | $controller = new \App\Http\Controllers\Front\GetPageTemplateController(); |
||
| 19 | $location = $controller->getLocation(); |
||
| 20 | |||
| 21 | $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']); |
||
| 22 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']); |
||
| 23 | $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray(); |
||
| 24 | $state_code = $location['countryCode'].'-'.$location['region']; |
||
| 25 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 26 | $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']); |
||
| 27 | $country_iso = $location['countryCode']; |
||
| 28 | |||
| 29 | $geoip_country = ''; |
||
| 30 | $geoip_state = ''; |
||
| 31 | if (\Auth::user()) { |
||
| 32 | $geoip_country = \Auth::user()->country; |
||
| 33 | $geoip_state = \Auth::user()->state; |
||
| 34 | } |
||
| 35 | if ($geoip_country == '') { |
||
| 36 | $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($country_iso); |
||
| 37 | } |
||
| 38 | $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 39 | if ($geoip_state == '') { |
||
| 40 | if (array_key_exists('id', $geoip_state_array)) { |
||
| 41 | $geoip_state = $geoip_state_array['id']; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | $result = $this->getResult($country, $geoip_country, $state, $geoip_state, |
||
| 45 | $shop, $cart, $cart1, $shop1, $rate, $product, $price); |
||
| 46 | |||
| 47 | return $result; |
||
| 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, |
||
| 56 | $shop, $cart, $cart1, $shop1, $rate, $product, $price) |
||
| 57 | { |
||
| 58 | if ($country == $geoip_country || $state == $geoip_state || ($country == '' && $state == '')) { |
||
| 59 | $result = $this->getCartResult($product, $shop, $cart, $rate, $price, $cart1, $shop1); |
||
| 60 | } |
||
| 61 | |||
| 62 | return $result; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function getCartResult($product, $shop, $cart, $rate, $price, $cart1, $shop1) |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getTotalCart($shop, $cart, $rate, $price, $cart1, $shop1) |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getTotalSub($shop, $cart, $rate, $price, $cart1, $shop1) |
||
| 112 | } |
||
| 113 | |||
| 114 | public function getPrice($months,$price, $currency, $value, $cost) |
||
| 123 | } |
||
| 124 | |||
| 125 | public function prices($id) |
||
| 126 | { |
||
| 127 | $plan = new Plan(); |
||
| 128 | $plans = $plan->where('product', $id)->orderBy('id','desc')->get(); |
||
| 129 | $price = []; |
||
| 130 | $cart_controller = new \App\Http\Controllers\Front\CartController(); |
||
| 131 | $currency = $cart_controller->currency(); |
||
| 132 | |||
| 133 | foreach ($plans as $value) { |
||
| 134 | $cost = $value->planPrice()->where('currency', $currency)->first()->add_price; |
||
| 135 | $cost = \App\Http\Controllers\Front\CartController::rounding($cost); |
||
| 136 | if ($value->days >= 366){ |
||
| 137 | $months = intval($value->days / 30 / 12).' '.'year'; |
||
| 138 | }elseif ($value->days < 365){ |
||
| 139 | $months = intval($value->days / 30 ).' '.'months'; |
||
| 140 | } |
||
| 141 | else { |
||
| 142 | $months = ''; |
||
| 143 | } |
||
| 144 | $price = $this->getPrice($months,$price, $currency, $value, $cost); |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | // $this->leastAmount($id); |
||
| 149 | |||
| 150 | return $price ; |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | public function withoutTaxRelation($productid, $currency) |
||
| 155 | { |
||
| 156 | try { |
||
| 157 | $product = $this->product->findOrFail($productid); |
||
| 158 | $controller = new \App\Http\Controllers\Front\CartController(); |
||
| 159 | $price = $controller->cost($productid); |
||
| 160 | |||
| 161 | return $price; |
||
| 162 | } catch (\Exception $ex) { |
||
| 163 | Bugsnag::notifyException($ex); |
||
| 164 | |||
| 165 | throw new \Exception($ex->getMessage()); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | public function taxProcess($taxes, $price, $cart, $shop) |
||
| 170 | { |
||
| 171 | try { |
||
| 172 | $rate = ''; |
||
| 173 | $tax_amount = ''; |
||
| 174 | foreach ($taxes as $tax) { |
||
| 175 | if ($tax->compound != 1) { |
||
| 176 | $rate += $tax->rate; |
||
| 177 | } else { |
||
| 178 | $rate = $tax->rate; |
||
| 179 | } |
||
| 180 | $tax_amount = $this->ifStatement($rate, $price, $cart, $shop, $tax->country, $tax->state); |
||
| 181 | } |
||
| 182 | // dd($tax_amount); |
||
| 183 | return $tax_amount; |
||
| 184 | } catch (\Exception $ex) { |
||
| 185 | Bugsnag::notifyException($ex); |
||
| 186 | |||
| 187 | throw new \Exception($ex->getMessage()); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | public function getTaxAmount($cart, $taxes, $price, $cart1, $shop) |
||
| 192 | { |
||
| 193 | if ($cart == 1) { |
||
| 194 | $tax_amount = $this->taxProcess($taxes, $price, $cart1, $shop); |
||
| 195 | } else { |
||
| 196 | $rate = ''; |
||
| 197 | foreach ($taxes as $tax) { |
||
| 198 | if ($tax->compound != 1) { |
||
| 199 | $rate += $tax->rate; |
||
| 200 | } else { |
||
| 201 | $rate = $tax->rate; |
||
| 202 | $price = $this->calculateTotal($rate, $price); |
||
| 203 | } |
||
| 204 | $tax_amount = $this->calculateTotal($rate, $price); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | return $tax_amount; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function calculateTotal($rate, $price) |
||
| 212 | { |
||
| 213 | $tax_amount = $price * ($rate / 100); |
||
| 214 | $total = $price + $tax_amount; |
||
| 215 | |||
| 216 | return $total; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function getDuration($value) |
||
| 231 | } |
||
| 232 | |||
| 233 | } |
||
| 234 |