| Total Complexity | 47 |
| Total Lines | 277 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ExtendedBaseCartController 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 ExtendedBaseCartController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class ExtendedBaseCartController extends Controller |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @return type |
||
| 18 | */ |
||
| 19 | public function addCouponUpdate() |
||
| 20 | { |
||
| 21 | try { |
||
| 22 | $code = \Input::get('coupon'); |
||
| 23 | $cart = Cart::getContent(); |
||
| 24 | $id = ''; |
||
| 25 | foreach ($cart as $item) { |
||
| 26 | $id = $item->id; |
||
| 27 | } |
||
| 28 | $promo_controller = new \App\Http\Controllers\Payment\PromotionController(); |
||
| 29 | $result = $promo_controller->checkCode($code, $id); |
||
| 30 | if ($result == 'success') { |
||
| 31 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
|
|
|||
| 32 | } |
||
| 33 | |||
| 34 | return redirect()->back(); |
||
| 35 | } catch (\Exception $ex) { |
||
| 36 | Bugsnag::notifyException($ex); |
||
| 37 | |||
| 38 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return type |
||
| 44 | */ |
||
| 45 | public function clearCart() |
||
| 46 | { |
||
| 47 | foreach (Cart::getContent() as $item) { |
||
| 48 | if (\Session::has('domain'.$item->id)) { |
||
| 49 | \Session::forget('domain'.$item->id); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | $this->removePlanSession(); |
||
| 54 | $renew_control = new \App\Http\Controllers\Order\RenewController(); |
||
| 55 | $renew_control->removeSession(); |
||
| 56 | Cart::clear(); |
||
| 57 | |||
| 58 | return redirect('show/cart'); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @throws \Exception |
||
| 63 | */ |
||
| 64 | public function removePlanSession() |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @throws \Exception |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public function checkPlanSession() |
||
| 81 | { |
||
| 82 | try { |
||
| 83 | if (Session::has('plan')) { |
||
| 84 | return true; |
||
| 85 | } |
||
| 86 | |||
| 87 | return false; |
||
| 88 | } catch (\Exception $ex) { |
||
| 89 | throw new \Exception($ex->getMessage()); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param type $iso |
||
| 95 | * |
||
| 96 | * @throws \Exception |
||
| 97 | * |
||
| 98 | * @return type |
||
| 99 | */ |
||
| 100 | public static function getMobileCodeByIso($iso) |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get Cost For a particular Plan. |
||
| 119 | * |
||
| 120 | * @param int $productid |
||
| 121 | * @param int $userid |
||
| 122 | * @param int $planid |
||
| 123 | * |
||
| 124 | * @throws \Exception |
||
| 125 | * |
||
| 126 | * @return int |
||
| 127 | */ |
||
| 128 | public function planCost($productid, $userid, $planid = '') |
||
| 129 | { |
||
| 130 | try { |
||
| 131 | $cost = 0; |
||
| 132 | $months = 0; |
||
| 133 | if ($this->checkPlanSession() === true) { |
||
| 134 | $planid = Session::get('plan'); |
||
| 135 | } |
||
| 136 | $plan = Plan::where('id', $planid)->where('product', $productid)->first(); |
||
| 137 | if ($plan) { //Get the Total Plan Cost if the Plan Exists For a Product |
||
| 138 | $months = 1; |
||
| 139 | $cont = new CartController(); |
||
| 140 | $currency = $cont->currency($userid); |
||
| 141 | $product = Product::find($productid); |
||
| 142 | $days = $plan->periods->pluck('days')->first(); |
||
| 143 | $price = ($product->planRelation->find($planid)->planPrice->where('currency', $currency['currency'])->first()->add_price); |
||
| 144 | if ($days) { //If Period Is defined for a Particular Plan ie no. of Days Generated |
||
| 145 | $months = $days >= '365' ? $days / 30 / 12 : $days / 30; |
||
| 146 | } |
||
| 147 | $finalPrice = str_replace(',', '', $price); |
||
| 148 | $cost = round($months) * $finalPrice; |
||
| 149 | } |
||
| 150 | |||
| 151 | return $cost; |
||
| 152 | } catch (\Exception $ex) { |
||
| 153 | dd($ex); |
||
| 154 | |||
| 155 | throw new \Exception($ex->getMessage()); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getGeoipCountry($country_iso, $user_country = '') |
||
| 175 | } |
||
| 176 | |||
| 177 | public function getGeoipState($state_code, $user_state = '') |
||
| 178 | { |
||
| 179 | $geoip_state = ''; |
||
| 180 | if (\Auth::user()) { |
||
| 181 | if (\Auth::user()->role == 'admin') { |
||
| 182 | $geoip_state = $user_state; |
||
| 183 | } elseif (\Auth::user()->role == 'user') { |
||
| 184 | $geoip_state = \Auth::user()->state; |
||
| 185 | } |
||
| 186 | if ($geoip_state == '') { |
||
| 187 | $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 188 | if (array_key_exists('id', $geoip_state_array)) { |
||
| 189 | $geoip_state = $geoip_state_array['id']; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return $geoip_state; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * When from same Indian State. |
||
| 199 | */ |
||
| 200 | public function getTaxWhenIndianSameState($user_state, $origin_state, $productid, $c_gst, $s_gst, $state_code, $status) |
||
| 201 | { |
||
| 202 | $taxes = [0]; |
||
| 203 | $value = ''; |
||
| 204 | $taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 205 | if ($taxClassId) { |
||
| 206 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 207 | $value = $this->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes); |
||
| 208 | if ($value == 0) { |
||
| 209 | $status = 0; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | return ['taxes'=>$taxes, 'status'=>$status, 'value'=>$value]; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * When from other Indian State. |
||
| 218 | */ |
||
| 219 | public function getTaxWhenIndianOtherState($user_state, $origin_state, $productid, $i_gst, $state_code, $status) |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * When from Union Territory. |
||
| 237 | */ |
||
| 238 | public function getTaxWhenUnionTerritory( |
||
| 239 | $user_state, |
||
| 240 | $origin_state, |
||
| 241 | $productid, |
||
| 242 | $c_gst, |
||
| 243 | $ut_gst, |
||
| 244 | $state_code, |
||
| 245 | $status |
||
| 246 | ) { |
||
| 247 | $taxClassId = TaxClass::where('name', 'Union Territory GST') |
||
| 248 | ->pluck('id')->toArray(); //Get the class Id of state |
||
| 249 | if ($taxClassId) { |
||
| 250 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 251 | $value = $this->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes); |
||
| 252 | if ($value == '') { |
||
| 253 | $status = 0; |
||
| 254 | } |
||
| 255 | } else { |
||
| 256 | $taxes = [0]; |
||
| 257 | $value = ''; |
||
| 258 | } |
||
| 259 | |||
| 260 | return ['taxes'=>$taxes, 'status'=>$status, 'value'=>$value]; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * When from Other Country and tax is applied for that country or state. |
||
| 265 | */ |
||
| 266 | public function getTaxForSpecificCountry($taxClassId, $productid, $status) |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * When from Other Country and tax is applied for Any country and state. |
||
| 280 | */ |
||
| 281 | public function getTaxForAnyCountry($taxClassId, $productid, $status) |
||
| 291 | } |
||
| 292 | } |
||
| 293 |