Completed
Push — development ( 50bb76...10afda )
by Ashutosh
08:23
created

ExtendedBaseCartController   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 46
eloc 135
dl 0
loc 293
rs 8.72
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A removePlanSession() 0 8 3
A getGeoipCountry() 0 10 3
A getTaxWhenIndianOtherState() 0 14 3
A getTaxWhenIndianSameState() 0 16 3
A clearCart() 0 13 3
A getTaxForSpecificCountry() 0 9 2
B planCost() 0 33 6
A productCost() 0 21 3
A getMobileCodeByIso() 0 14 4
A checkPlanSession() 0 10 3
A getGeoipState() 0 13 4
A getTaxForAnyCountry() 0 10 2
A addCouponUpdate() 0 20 4
A getTaxWhenUnionTerritory() 0 14 3

How to fix   Complexity   

Complex Class

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
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Payment\Plan;
7
use App\Model\Payment\PlanPrice;
8
use App\Model\Product\Product;
9
use App\Model\Payment\Tax;
10
use App\Model\Payment\TaxByState;
11
use App\Model\Payment\TaxClass;
12
use App\Model\Payment\TaxOption;
13
use App\Model\Payment\TaxProductRelation;
14
use Bugsnag;
15
use Cart;
16
use Session;
17
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()
68
    {
69
        try {
70
            if (Session::has('plan')) {
71
                Session::forget('plan');
72
            }
73
        } catch (\Exception $ex) {
74
            throw new \Exception($ex->getMessage());
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)
198
    {
199
    $geoip_country = '';
200
    if (\Auth::user()) {
201
    $geoip_country = \Auth::user()->country;
202
      }
203
   if ($geoip_country == '') {
204
        $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($country_iso);
205
    }
206
    return $geoip_country;
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)
250
    {
251
       $taxClassId = TaxClass::where('name', 'Inter State GST')->pluck('id')->toArray(); //Get the class Id  of state
252
       if ($taxClassId) {
253
           $taxes = $this->getTaxByPriority($taxClassId);
254
           $value = $this->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes);
255
           if ($value == '') {
256
               $status = 0;
257
           }
258
       } else {
259
           $taxes = [0];
260
           $value ='';  
261
       }
262
       return ['taxes'=>$taxes,'status'=>$status,'value'=>$value];
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)
288
     {
289
       $taxes = $this->getTaxByPriority($taxClassId);
290
       $value = $this->getValueForOthers($productid, $taxClassId, $taxes);
291
       if ($value == '') {
292
           $status = 0;
293
       }
294
       $rate = $value;
295
       return ['taxes'=>$taxes , 'status'=>$status,'value'=>$value,'rate'=>$value];
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)
302
     {
303
       $taxForAnyCountry = $this->getTaxForAnyCountry($taxClassId,$productid,$status);
304
       $taxes = $this->getTaxByPriority($taxClassId);
305
       $value = $this->getValueForOthers($productid, $taxClassId, $taxes);
306
       if ($value == '') {
307
           $status = 0;
308
       }
309
       $rate = $value;
310
       return ['taxes'=>$taxes , 'status'=>$status,'value'=>$value,'rate'=>$value];
311
     }
312
}
313