Completed
Push — development ( db1c54...00218f )
by Ashutosh
10:15
created

BaseCartController   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 345
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 50
eloc 149
dl 0
loc 345
rs 8.4
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A addProduct() 0 33 5
A getTaxByPriority() 0 10 2
A findStateByRegionId() 0 8 2
A getValueForUnionTerritory() 0 7 3
A getValueForOtherState() 0 8 3
A getValueForSameState() 0 14 4
A addCurrencyAttributes() 0 15 3
A allowSubscription() 0 14 4
A cartUpdate() 0 10 2
A postContactUs() 0 27 2
A getValueForOthers() 0 11 3
A cartRemove() 0 6 1
A getCartCollection() 0 8 2
B currency() 0 26 9
A findCountryByGeoip() 0 11 3
A updateQty() 0 12 1
A reduseQty() 0 8 1

How to fix   Complexity   

Complex Class

Complex classes like BaseCartController 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 BaseCartController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
use App\Model\Payment\Tax;
8
use App\Model\Payment\TaxProductRelation;
9
use App\Model\Product\Product;
10
use Bugsnag;
11
use Cart;
12
use Exception;
13
use Session;
14
15
class BaseCartController extends Controller
16
{
17
	public function getCartCollection($items)
18
	{
19
    if ($items == null) {
20
    $cartCollection = Cart::getContent();
21
    } else {
22
        $cartCollection = $items;
23
    }
24
    return $cartCollection;
25
	}
26
27
	/**
28
     * @param type $tax_class_id
29
     *
30
     * @throws \Exception
31
     *
32
     * @return type
33
     */
34
    public function getTaxByPriority($taxClassId)
35
    {
36
        try {
37
            $taxe_relation = Tax::where('tax_classes_id', $taxClassId)->get();
38
39
            return $taxe_relation;
40
        } catch (\Exception $ex) {
41
            Bugsnag::notifyException($ex);
42
43
            throw new \Exception('error in get tax priority');
44
        }
45
    }
46
47
     /**
48
     *   Get tax value for Same State.
49
     *
50
     * @param type $productid
51
     * @param type $c_gst
52
     * @param type $s_gst
53
     *                        return type
54
     */
55
    public function getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes)
56
    {
57
        try {
58
            $value = '';
59
            $value = $taxes->toArray()[0]['active'] ?
60
61
                  (TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() ?
62
                        $c_gst + $s_gst.'%' : 0) : 0;
63
64
            return $value;
65
        } catch (Exception $ex) {
66
            Bugsnag::notifyException($ex);
67
68
            return redirect()->back()->with('fails', $ex->getMessage());
69
        }
70
    }
71
72
     /**
73
     *   Get tax value for Other States.
74
     *
75
     * @param type $productid
76
     * @param type $i_gst
77
     *                        return type
78
     */
79
    public function getValueForOtherState($productid, $i_gst, $taxClassId, $taxes)
80
    {
81
        $value = '';
82
        $value = $taxes->toArray()[0]['active'] ? //If the Current Class is active
83
              (TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() ?
84
                        $i_gst.'%' : 0) : 0; //IGST
85
86
        return $value;
87
    }
88
     
89
    /**
90
     *  Get tax value for Union Territory States.
91
     *
92
     * @param type $productid
93
     * @param type $c_gst
94
     * @param type $ut_gst
95
     *                        return type
96
     */
97
    public function getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes)
98
    {
99
        $value = '';
100
        $value = $taxes->toArray()[0]['active'] ?
101
             (TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() ? $ut_gst + $c_gst.'%' : 0) : 0;
102
103
        return $value;
104
    }
105
106
107
    public function getValueForOthers($productid, $taxClassId, $taxes)
108
    {
109
        $otherRate = 0;
110
        $status = $taxes->toArray()[0]['active'];
111
        if ($status && (TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() > 0)) {
112
            $otherRate = Tax::where('tax_classes_id', $taxClassId)->first()->rate;
113
        }
114
115
        $value = $otherRate.'%';
116
117
        return $value;
118
    }
119
    
120
    /**
121
     * @param type $id
122
     *
123
     * @return array
124
     */
125
    public function addCurrencyAttributes($id)
126
    {
127
        try {
128
            $currency = $this->currency();
129
            $product = $this->product->where('id', $id)->first();
130
            if ($product) {
131
                $productCurrency = $this->currency();
132
                $currency = $this->currency->where('code', $productCurrency)->get()->toArray();
133
            } else {
134
                $currency = [];
135
            }
136
137
            return $currency;
138
        } catch (\Exception $ex) {
139
            throw new \Exception($ex->getMessage());
140
        }
141
    }
142
143
        /**
144
     * @param Request $request
145
     *
146
     * @return type
147
     */
148
    public function postContactUs(Request $request)
149
    {
150
        $this->validate($request, [
151
            'name'    => 'required',
152
            'email'   => 'required|email',
153
            'message' => 'required',
154
        ]);
155
156
        $set = new \App\Model\Common\Setting();
157
        $set = $set->findOrFail(1);
158
159
        try {
160
            $from = $set->email;
161
            $fromname = $set->company;
162
            $toname = '';
163
            $to = '[email protected]';
164
            $data = '';
165
            $data .= 'Name: '.$request->input('name').'<br/s>';
166
            $data .= 'Email: '.$request->input('email').'<br/>';
167
            $data .= 'Message: '.$request->input('message').'<br/>';
168
            $data .= 'Mobile: '.$request->input('Mobile').'<br/>';
169
170
            $subject = 'Faveo billing enquiry';
171
            $this->templateController->mailing($from, $to, $data, $subject, [], $fromname, $toname);
172
            return redirect()->back()->with('success', 'Your message was sent successfully. Thanks.');
173
        } catch (\Exception $ex) {
174
            return redirect()->back()->with('fails', $ex->getMessage());
175
        }
176
    }
177
178
    public function cartRemove(Request $request)
179
    {
180
        $id = $request->input('id');
181
        Cart::remove($id);
182
183
        return 'success';
184
    }
185
186
    public function reduseQty(Request $request)
187
    {
188
        $id = $request->input('id');
189
        Cart::update($id, [
190
            'quantity' => -1, // so if the current product has a quantity of 4, it will subtract 1 and will result to 3
191
        ]);
192
193
        return 'success';
194
    }
195
196
    public function updateQty(Request $request)
197
    {
198
        $id = $request->input('productid');
199
        $qty = $request->input('qty');
200
        Cart::update($id, [
201
            'quantity' => [
202
                'relative' => false,
203
                'value'    => $qty,
204
            ],
205
        ]);
206
207
        return 'success';
208
    }
209
210
    public function addProduct($id)
211
    {
212
        try {
213
            $qty = 1;
214
215
            $currency = $this->currency();
216
            $product = Product::where('id', $id)->first();
217
            if ($product) {
218
                $actualPrice = $this->cost($product->id);
219
                $currency = $this->currency();
220
                $productName = $product->name;
221
                $planid = 0;
222
                if ($this->checkPlanSession() === true) {
223
                    $planid = Session::get('plan');
224
                }
225
                $isTaxApply = $product->tax_apply;
226
                $taxConditions = $this->checkTax($id);
227
228
                /*
229
                 * Check if this product allow multiple qty
230
                 */
231
                if ($product->multiple_qty == 1) {
232
                    // Allow
233
                } else {
234
                    $qty = 1;
235
                }
236
                $items = ['id' => $id, 'name' => $productName, 'price' => $actualPrice, 'quantity' => $qty, 'attributes' => ['currency' => [[$currency]]]];
237
                $items = array_merge($items, $taxConditions);
238
239
                return $items;
240
            }
241
        } catch (\Exception $e) {
242
            Bugsnag::notifyException($e);
243
        }
244
    }
245
246
        /**
247
     * @param type $userid
248
     *
249
     * @throws \Exception
250
     *
251
     * @return string
252
     */
253
    public function currency($userid = '')
254
    {
255
        try {
256
            $currency = 'INR';
257
            if ($this->checkCurrencySession() === true) {
258
                $currency = Session::get('currency');
259
            }
260
261
            if (\Auth::user()) {
262
                $currency = \Auth::user()->currency;
263
                if ($currency == 'USD' || $currency == '1') {
264
                    $currency = 'USD';
265
                }
266
            }
267
            if ($userid != '') {
268
                $user = new \App\User();
269
                $currency = $user->find($userid)->currency;
270
                if ($currency == 'USD' || $currency == '1') {
271
                    $currency = 'USD';
272
                } else {
273
                    $currency = 'INR';
274
                }
275
            }
276
            return $currency;
277
        } catch (\Exception $ex) {
278
            throw new \Exception($ex->getMessage());
279
        }
280
    }
281
282
        /**
283
     * @param type $productid
284
     *
285
     * @throws \Exception
286
     *
287
     * @return bool
288
     */
289
    public function allowSubscription($productid)
290
    {
291
        try {
292
            $reponse = false;
293
            $product = $this->product->find($productid);
294
            if ($product) {
295
                if ($product->subscription == 1) {
296
                    $reponse = true;
297
                }
298
            }
299
300
            return $reponse;
301
        } catch (\Exception $ex) {
302
            throw new \Exception($ex->getMessage());
303
        }
304
    }
305
306
    /**
307
     * @param type $id
308
     * @param type $key
309
     * @param type $value
310
     */
311
    public function cartUpdate($id, $key, $value)
312
    {
313
        try {
314
            Cart::update(
315
                    $id, [
316
                $key => $value, // new item name
317
                    ]
318
            );
319
        } catch (\Exception $ex) {
320
            throw new \Exception($ex->getMessage());
321
        }
322
    }
323
324
     /**
325
     * @param type $iso
326
     *
327
     * @throws \Exception
328
     *
329
     * @return string
330
     */
331
    public static function findCountryByGeoip($iso)
332
    {
333
        try {
334
            $country = \App\Model\Common\Country::where('country_code_char2', $iso)->first();
335
            if ($country) {
336
                return $country->country_code_char2;
337
            } else {
338
                return '';
339
            }
340
        } catch (\Exception $ex) {
341
            throw new \Exception($ex->getMessage());
342
        }
343
    }
344
345
     /**
346
     * @param type $iso
347
     *
348
     * @throws \Exception
349
     *
350
     * @return array
351
     */
352
    public static function findStateByRegionId($iso)
353
    {
354
        try {
355
            $states = \App\Model\Common\State::where('country_code_char2', $iso)->pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
356
357
            return $states;
358
        } catch (\Exception $ex) {
359
            throw new \Exception($ex->getMessage());
360
        }
361
    }
362
363
364
}
365