| Total Complexity | 46 |
| Total Lines | 293 |
| 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 |
||
| 18 | class ExtendedBaseCartController extends Controller |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @return type |
||
| 22 | */ |
||
| 23 | public function addCouponUpdate() |
||
| 24 | { |
||
| 25 | try { |
||
| 26 | $code = \Input::get('coupon'); |
||
| 27 | $cart = Cart::getContent(); |
||
| 28 | $id = ''; |
||
| 29 | foreach ($cart as $item) { |
||
| 30 | $id = $item->id; |
||
| 31 | } |
||
| 32 | $promo_controller = new \App\Http\Controllers\Payment\PromotionController(); |
||
| 33 | $result = $promo_controller->checkCode($code, $id); |
||
| 34 | if ($result == 'success') { |
||
| 35 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 36 | } |
||
| 37 | |||
| 38 | return redirect()->back(); |
||
| 39 | } catch (\Exception $ex) { |
||
| 40 | Bugsnag::notifyException($ex); |
||
| 41 | |||
| 42 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return type |
||
| 48 | */ |
||
| 49 | public function clearCart() |
||
| 50 | { |
||
| 51 | foreach (Cart::getContent() as $item) { |
||
| 52 | if (\Session::has('domain'.$item->id)) { |
||
| 53 | \Session::forget('domain'.$item->id); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | $this->removePlanSession(); |
||
| 57 | $renew_control = new \App\Http\Controllers\Order\RenewController(); |
||
| 58 | $renew_control->removeSession(); |
||
| 59 | Cart::clear(); |
||
| 60 | |||
| 61 | return redirect('show/cart'); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @throws \Exception |
||
| 66 | */ |
||
| 67 | public function removePlanSession() |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @throws \Exception |
||
| 80 | * |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public function checkPlanSession() |
||
| 84 | { |
||
| 85 | try { |
||
| 86 | if (Session::has('plan')) { |
||
| 87 | return true; |
||
| 88 | } |
||
| 89 | |||
| 90 | return false; |
||
| 91 | } catch (\Exception $ex) { |
||
| 92 | throw new \Exception($ex->getMessage()); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param type $iso |
||
| 98 | * |
||
| 99 | * @throws \Exception |
||
| 100 | * |
||
| 101 | * @return type |
||
| 102 | */ |
||
| 103 | public static function getMobileCodeByIso($iso) |
||
| 104 | { |
||
| 105 | try { |
||
| 106 | $code = ''; |
||
| 107 | if ($iso != '') { |
||
| 108 | $mobile = \DB::table('mobile')->where('iso', $iso)->first(); |
||
| 109 | if ($mobile) { |
||
| 110 | $code = $mobile->phonecode; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | return $code; |
||
| 115 | } catch (\Exception $ex) { |
||
| 116 | throw new \Exception($ex->getMessage()); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param type $productid |
||
| 122 | * @param type $userid |
||
| 123 | * |
||
| 124 | * @throws \Exception |
||
| 125 | * |
||
| 126 | * @return type |
||
| 127 | */ |
||
| 128 | public function productCost($productid, $userid = '') |
||
| 129 | { |
||
| 130 | try { |
||
| 131 | $sales = 0; |
||
| 132 | $currency = $this->currency($userid); |
||
| 133 | $product = Product::find($productid); |
||
| 134 | |||
| 135 | // $price = $product->price()->where('currency', $currency)->first(); |
||
| 136 | $plan_id = Plan::where('product', $productid)->pluck('id')->first(); |
||
| 137 | $price = PlanPrice::where('plan_id', $plan_id)->where('currency', $currency)->pluck('add_price')->first(); |
||
| 138 | if ($price) { |
||
| 139 | $sales = $price; |
||
| 140 | // if ($sales == 0) { |
||
| 141 | // $sales = $price->price; |
||
| 142 | // } |
||
| 143 | } |
||
| 144 | //} |
||
| 145 | |||
| 146 | return $sales; |
||
| 147 | } catch (\Exception $ex) { |
||
| 148 | throw new \Exception($ex->getMessage()); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param type $productid |
||
| 154 | * @param type $userid |
||
| 155 | * @param type $planid |
||
| 156 | * |
||
| 157 | * @throws \Exception |
||
| 158 | * |
||
| 159 | * @return type |
||
| 160 | */ |
||
| 161 | public function planCost($productid, $userid, $planid = '') |
||
| 162 | { |
||
| 163 | try { |
||
| 164 | $cost = 0; |
||
| 165 | $subscription = $this->allowSubscription($productid); |
||
| 166 | if ($this->checkPlanSession() === true) { |
||
| 167 | $planid = Session::get('plan'); |
||
| 168 | } |
||
| 169 | |||
| 170 | if ($subscription === true) { |
||
| 171 | $plan = new \App\Model\Payment\Plan(); |
||
| 172 | $plan = $plan->where('id', $planid)->where('product', $productid)->first(); |
||
| 173 | $items = (\Session::get('items')); |
||
| 174 | |||
| 175 | if ($plan) { |
||
| 176 | $currency = $this->currency($userid); |
||
| 177 | $price = $plan->planPrice() |
||
| 178 | ->where('currency', $currency) |
||
| 179 | ->first() |
||
| 180 | ->add_price; |
||
| 181 | $days = $plan->days; |
||
| 182 | $months = $days / 30 / 12; |
||
| 183 | if ($items != null) { |
||
| 184 | $cost = $items[$productid]['price']; |
||
| 185 | } else { |
||
| 186 | $cost = round($months) * $price; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | return $cost; |
||
| 192 | } catch (\Exception $ex) { |
||
| 193 | throw new \Exception($ex->getMessage()); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | public function getGeoipCountry($country_iso) |
||
| 207 | } |
||
| 208 | |||
| 209 | public function getGeoipState($state_code) |
||
| 210 | { |
||
| 211 | $geoip_state = ''; |
||
| 212 | $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 213 | if (\Auth::user()) { |
||
| 214 | $geoip_state = \Auth::user()->state; |
||
| 215 | } |
||
| 216 | if ($geoip_state == '') { |
||
| 217 | if (array_key_exists('id', $geoip_state_array)) { |
||
| 218 | $geoip_state = $geoip_state_array['id']; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | return $geoip_state; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * When from same Indian State |
||
| 226 | */ |
||
| 227 | public function getTaxWhenIndianSameState($user_state,$origin_state,$productid,$c_gst,$s_gst,$state_code,$status) |
||
| 228 | { |
||
| 229 | $taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 230 | if ($taxClassId) { |
||
| 231 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 232 | $value = $this->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes); |
||
| 233 | |||
| 234 | if ($value == '') { |
||
| 235 | $status = 0; |
||
| 236 | } |
||
| 237 | } else { |
||
| 238 | $taxes = [0]; |
||
| 239 | $value =''; |
||
| 240 | } |
||
| 241 | |||
| 242 | return ['taxes'=>$taxes,'status'=>$status,'value'=>$value]; |
||
| 243 | } |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * When from other Indian State |
||
| 248 | */ |
||
| 249 | public function getTaxWhenIndianOtherState($user_state,$origin_state,$productid,$i_gst,$state_code,$status) |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * When from Union Territory |
||
| 267 | */ |
||
| 268 | public function getTaxWhenUnionTerritory($user_state,$origin_state,$productid,$c_gst, $ut_gst,$state_code,$status) |
||
| 269 | { |
||
| 270 | $taxClassId = TaxClass::where('name', 'Union Territory GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 271 | if ($taxClassId) { |
||
| 272 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 273 | $value = $this->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes); |
||
| 274 | if ($value == '') { |
||
| 275 | $status = 0; |
||
| 276 | } |
||
| 277 | } else { |
||
| 278 | $taxes = [0]; |
||
| 279 | $value =''; |
||
| 280 | } |
||
| 281 | return ['taxes'=>$taxes,'status'=>$status,'value'=>$value]; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * When from Other Country and tax is applied for that country or state |
||
| 286 | */ |
||
| 287 | public function getTaxForSpecificCountry($taxClassId,$productid,$status) |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * When from Other Country and tax is applied for Any country and state |
||
| 300 | */ |
||
| 301 | public function getTaxForAnyCountry($taxClassId,$productid,$status) |
||
| 311 | } |
||
| 312 | } |
||
| 313 |