Completed
Pull Request — master (#122)
by vijay
105:11 queued 50:23
created

CartController::ProductList()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 3 Features 0
Metric Value
cc 4
eloc 13
c 7
b 3
f 0
nc 8
nop 1
dl 0
loc 22
rs 8.9197
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Common\TemplateController;
6
use App\Http\Controllers\Controller;
7
use App\Model\Payment\Currency;
8
use App\Model\Payment\Tax;
9
use App\Model\Payment\TaxOption;
10
use App\Model\Product\Product;
11
use Cart;
12
use Illuminate\Http\Request;
13
14
class CartController extends Controller
15
{
16
    public $templateController;
17
    public $product;
18
    public $currency;
19
    public $addons;
20
    public $addonRelation;
21
    public $licence;
22
    public $tax_option;
23
24
    public function __construct()
25
    {
26
        $templateController = new TemplateController();
27
        $this->templateController = $templateController;
28
29
        $product = new Product();
30
        $this->product = $product;
31
32
        $currency = new Currency();
33
        $this->currency = $currency;
34
35
        $tax = new Tax();
36
        $this->tax = $tax;
0 ignored issues
show
Bug introduced by
The property tax does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
38
        $tax_option = new TaxOption();
39
        $this->tax_option = $tax_option;
40
    }
41
42
    public function ProductList(Request $request)
43
    {
44
        $location = \GeoIP::getLocation();
45
        //dd($location);
46
47
        if ($location['country'] == 'India') {
48
            $currency = 'INR';
49
        } else {
50
            $currency = 'USD';
51
        }
52
        \Session::put('currency', $currency);
53
        if (!\Session::has('currency')) {
54
            \Session::put('currency', 'INR');
55
//dd(\Session::get('currency'));
56
        }
57
58
        try {
59
            return $this->templateController->show(1);
60
        } catch (\Exception $ex) {
61
            return redirect()->back()->with('fails', $ex->getMessage());
62
        }
63
    }
64
65
    public function Cart(Request $request)
66
    {
67
        try {
68
            $id = $request->input('id');
69
            //dd($id);
70
            if (!array_key_exists($id, Cart::getContent()->toArray())) {
71
                $items = $this->addProduct($id);
72
                Cart::add($items);
73
            }
74
75
            return redirect('show/cart');
76
        } catch (\Exception $ex) {
77
            dd($ex);
78
79
            return redirect()->back()->with('fails', $ex->getMessage());
80
        }
81
    }
82
83
    public function showCart()
84
    {
85
        try {
86
            $attributes = [];
87
            $cartCollection = Cart::getContent();
88
            foreach ($cartCollection as $item) {
89
                $attributes[] = $item->attributes;
90
            }
91
92
            return view('themes.default1.front.cart', compact('cartCollection', 'attributes'));
93
        } catch (\Exception $ex) {
94
            dd($ex);
95
96
            return redirect()->back()->with('fails', $ex->getMessage());
97
        }
98
    }
99
100
    public function checkTax($productid)
101
    {
102
        try {
103
            $tax_attribute[0] = ['name' => 'null', 'rate' => 0];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tax_attribute was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tax_attribute = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
104
            $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
0 ignored issues
show
Coding Style Comprehensibility introduced by
$taxCondition was never initialized. Although not strictly required by PHP, it is generally a good practice to add $taxCondition = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
105
                'name'   => 'null',
106
                'type'   => 'tax',
107
                'target' => 'item',
108
                'value'  => '0%',
109
            ]);
110
//dd($tax_attribute);
111
            $product = $this->product->findOrFail($productid);
112
113
            $location = \GeoIP::getLocation();
114
            $counrty_iso = $location['isoCode'];
115
            $state_code = $location['isoCode'].'-'.$location['state'];
116
            $geoip_country = '';
117
            $geoip_state = '';
118
            if (\Auth::user()) {
119
                $geoip_country = \Auth::user()->country;
120
                $geoip_state = \Auth::user()->state;
121
            }
122
            if ($geoip_country == '') {
123
                $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($counrty_iso);
124
            }
125
            $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
126
            if ($geoip_state == '') {
127
                if (array_key_exists('id', $geoip_state_array)) {
128
                    $geoip_state = $geoip_state_array['id'];
129
                }
130
            }
131
132
//dd($product);
133
            if ($this->tax_option->findOrFail(1)->inclusive == 0) {
134
                $tax_rule = $this->tax_option->findOrFail(1);
135
                $product1 = $tax_rule->inclusive;
136
                $shop = $tax_rule->shop_inclusive;
0 ignored issues
show
Unused Code introduced by
$shop is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
137
                $cart = $tax_rule->cart_inclusive;
0 ignored issues
show
Unused Code introduced by
$cart is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
138
                if ($product->tax()->first()) {
139
                    $tax_class_id = $product->tax()->first()->tax_class_id;
140
                    if ($this->tax_option->findOrFail(1)->tax_enable == 1) {
141
                        if ($product1 == 0) {
142
                            $taxes = $this->getTaxByPriority($tax_class_id);
143
                            $rate = 0;
144
                            foreach ($taxes as $key => $tax) {
145
                                if ($tax->country == $geoip_country || $tax->state == $geoip_state || ($tax->country == '' && $tax->state == '')) {
146
                                    if ($tax->compound == 1) {
147
                                        $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $tax->rate];
148
                                        $taxCondition[$key] = new \Darryldecode\Cart\CartCondition([
149
                                            'name'   => $tax->name,
150
                                            'type'   => 'tax',
151
                                            'target' => 'item',
152
                                            'value'  => $tax->rate.'%',
153
                                        ]);
154
                                    } else {
155
                                        $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $tax->rate];
156
                                        $rate += $tax->rate;
157
                                        $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
158
                                            'name'   => 'no compound',
159
                                            'type'   => 'tax',
160
                                            'target' => 'item',
161
                                            'value'  => $rate.'%',
162
                                        ]);
163
                                    }
164
                                }
165
                            }
166 View Code Duplication
                        } else {
167
                            if ($product->tax()->first()) {
168
                                $tax_class_id = $product->tax()->first()->tax_class_id;
169
                                if ($this->tax_option->findOrFail(1)->tax_enable == 1) {
170
                                    $taxes = $this->getTaxByPriority($tax_class_id);
171
                                    foreach ($taxes as $key => $tax) {
172
                                        $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $tax->rate];
173
                                    }
174
                                }
175
                            }
176
                        }
177 View Code Duplication
                    } else {
178
                        if ($product->tax()->first()) {
179
                            $tax_class_id = $product->tax()->first()->tax_class_id;
180
                            if ($this->tax_option->findOrFail(1)->tax_enable == 1) {
181
                                $taxes = $this->getTaxByPriority($tax_class_id);
182
                                foreach ($taxes as $key => $tax) {
183
                                    $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $tax->rate];
184
                                }
185
                            }
186
                        }
187
                    }
188
                }
189 View Code Duplication
            } else {
190
                if ($product->tax()->first()) {
191
                    $tax_class_id = $product->tax()->first()->tax_class_id;
192
                    if ($this->tax_option->findOrFail(1)->tax_enable == 1) {
193
                        $taxes = $this->getTaxByPriority($tax_class_id);
194
                        foreach ($taxes as $key => $tax) {
195
                            $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $tax->rate];
196
                        }
197
                    }
198
                }
199
            }
200
            $currency_attribute = $this->addCurrencyAttributes($productid);
201
202
            return ['conditions' => $taxCondition, 'attributes' => ['tax' => $tax_attribute, 'currency' => $currency_attribute]];
203
        } catch (\Exception $ex) {
204
            dd($ex);
205
            throw new \Exception('Can not check the tax');
206
        }
207
    }
208
209
    public function checkTaxOld($isTaxApply, $id)
210
    {
211
        try {
212
            $rate1 = 0;
213
            $rate2 = 0;
214
            $name1 = 'null';
215
            $name2 = 'null';
216
217
            if ($ruleEnabled) {
218
                $enabled = $ruleEnabled->status;
0 ignored issues
show
Bug introduced by
The variable $ruleEnabled does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
219
                $type = $ruleEnabled->type;
220
                $compound = $ruleEnabled->compound;
221
                if ($enabled == 1 && $type == 'exclusive') {
222
                    if ($isTaxApply == 1) {
223
                        $tax1 = $this->tax->where('level', 1)->first();
224
                        $tax2 = $this->tax->where('level', 2)->first();
225 View Code Duplication
                        if ($tax1) {
226
                            $name1 = $tax1->name;
227
                            $rate1 = $tax1->rate;
228
                            $taxCondition1 = new \Darryldecode\Cart\CartCondition([
229
                                'name'   => $name1,
230
                                'type'   => 'tax',
231
                                'target' => 'item',
232
                                'value'  => $rate1.'%',
233
                            ]);
234
                        } else {
235
                            $taxCondition1 = new \Darryldecode\Cart\CartCondition([
236
                                'name'   => $name1,
237
                                'type'   => 'tax',
238
                                'target' => 'item',
239
                                'value'  => $rate1,
240
                            ]);
241
                        }
242 View Code Duplication
                        if ($tax2) {
243
                            $name2 = $tax2->name;
244
                            $rate2 = $tax2->rate;
245
                            $taxCondition2 = new \Darryldecode\Cart\CartCondition([
246
                                'name'   => $name2,
247
                                'type'   => 'tax',
248
                                'target' => 'item',
249
                                'value'  => $rate2.'%',
250
                            ]);
251
                        } else {
252
                            $taxCondition2 = new \Darryldecode\Cart\CartCondition([
253
                                'name'   => $name2,
254
                                'type'   => 'tax',
255
                                'target' => 'item',
256
                                'value'  => $rate2,
257
                            ]);
258
                        }
259
                    } else {
260
                        $taxCondition1 = new \Darryldecode\Cart\CartCondition([
261
                            'name'   => $name1,
262
                            'type'   => 'tax',
263
                            'target' => 'item',
264
                            'value'  => $rate1,
265
                        ]);
266
                        $taxCondition2 = new \Darryldecode\Cart\CartCondition([
267
                            'name'   => $name2,
268
                            'type'   => 'tax',
269
                            'target' => 'item',
270
                            'value'  => $rate2,
271
                        ]);
272
                    }
273
                    $currency_attribute = $this->addCurrencyAttributes($id);
274
//dd($currency_attribute);
275
                    if ($compound == 1) {
276
                        return ['conditions' => [$taxCondition1, $taxCondition2], 'attributes' => ['tax' => [['name' => $name1, 'rate' => $rate1], ['name' => $name2, 'rate' => $rate2]], 'currency' => $currency_attribute]];
277
                    } else {
278
                        return ['conditions' => $taxCondition2, 'attributes' => ['tax' => [['name' => $name2, 'rate' => $rate2]], 'currency' => $currency_attribute]];
279
                    }
280
                }
281
            }
282
        } catch (\Exception $ex) {
283
            dd($ex);
284
            throw new \Exception('Can not check the tax');
285
        }
286
    }
287
288
    public function CartRemove(Request $request)
289
    {
290
        $id = $request->input('id');
291
//dd($id);
292
        Cart::remove($id);
293
294
        return 'success';
295
    }
296
297
    public function ReduseQty(Request $request)
298
    {
299
        $id = $request->input('id');
300
        Cart::update($id, [
301
            'quantity' => -1, // so if the current product has a quantity of 4, it will subtract 1 and will result to 3
302
        ]);
303
//dd(Cart::getContent());
304
        return 'success';
305
    }
306
307
    public function updateQty(Request $request)
308
    {
309
        $id = $request->input('productid');
310
        $qty = $request->input('qty');
311
        Cart::update($id, [
312
            'quantity' => [
313
                'relative' => false,
314
                'value'    => $qty,
315
            ],
316
        ]);
317
//dd(Cart::getContent());
318
        return 'success';
319
    }
320
321
    public function AddAddons($id)
322
    {
323
        $addon = $this->addons->where('id', $id)->first();
324
325
        $isTaxApply = $addon->tax_addon;
326
327
        $taxConditions = $this->CheckTax($isTaxApply);
328
329
        $items = ['id' => 'addon'.$addon->id, 'name' => $addon->name, 'price' => $addon->selling_price, 'quantity' => 1];
330
        $items = array_merge($items, $taxConditions);
331
332
//dd($items);
333
334
        return $items;
335
    }
336
337
    public function GetProductAddons($productId)
338
    {
339
        $addons = [];
340
        if ($this->addonRelation->where('product_id', $productId)->count() > 0) {
341
            $addid = $this->addonRelation->where('product_id', $productId)->pluck('addon_id')->toArray();
342
            $addons = $this->addons->whereIn('id', $addid)->get();
343
        }
344
345
        return $addons;
346
    }
347
348
    public function addProduct($id)
349
    {
350
        $location = \GeoIP::getLocation();
351
        //dd($location);
352
        $qty = 1;
353
        if ($location['country'] == 'India') {
354
            $currency = 'INR';
355
        } else {
356
            $currency = 'USD';
357
        }
358
        \Session::put('currency', $currency);
359
        if (!\Session::has('currency')) {
360
            \Session::put('currency', 'INR');
361
//dd(\Session::get('currency'));
362
        }
363
        $currency = \Session::get('currency');
364
        //dd($currency);
365
//        if (!$currency) {
366
//            $currency = 'USD';
367
//        }
368
        $product = $this->product->where('id', $id)->first();
369
370
        if ($product) {
371
            $productCurrency = $product->price()->where('currency', $currency)->first()->currency;
372
            $actualPrice = $product->price()->where('currency', $currency)->first()->sales_price;
373
            if (!$actualPrice) {
374
                $actualPrice = $product->price()->where('currency', $currency)->first()->price;
375
            }
376
            $currency = $this->currency->where('code', $productCurrency)->get()->toArray();
377
378
            $productName = $product->name;
379
380
            /*
381
             * Check the Tax is On
382
             */
383
            $isTaxApply = $product->tax_apply;
0 ignored issues
show
Unused Code introduced by
$isTaxApply is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
384
385
            $taxConditions = $this->checkTax($id);
386
//dd($taxConditions);
387
388
            /*
389
             * Check if this product allow multiple qty
390
             */
391
            if ($product->multiple_qty == 1) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
392
                // Allow
393
            } else {
394
                $qty = 1;
395
            }
396
            $items = ['id' => $id, 'name' => $productName, 'price' => $actualPrice, 'quantity' => $qty, 'attributes' => ['currency' => [[$currency]]]];
397
            $items = array_merge($items, $taxConditions);
398
            //dd($items);
399
            return $items;
400
        }
401
    }
402
403
    public function ClearCart()
404
    {
405
        foreach (Cart::getContent() as $item) {
406
            if (\Session::has('domain'.$item->id)) {
407
                \Session::forget('domain'.$item->id);
408
            }
409
        }
410
        Cart::clear();
411
412
        return redirect('show/cart')->with('warning', 'Your cart is empty! ');
413
    }
414
415
    public function LicenceCart($id)
416
    {
417
        try {
418
            $licence = $this->licence->where('id', $id)->first();
419
420
            $isTaxApply = 0;
421
422
            $taxConditions = $this->CheckTax($isTaxApply);
423
424
            $items = ['id' => $licence->id, 'name' => $licence->name, 'price' => $licence->price, 'quantity' => 1, 'attributes' => ['number_of_sla' => $licence->number_of_sla]];
425
            $items = array_merge($items, $taxConditions);
426
            Cart::clear();
427
            Cart::add($items);
428
429
            return view('themes.default1.front.cart', compact('cartCollection'));
430
        } catch (\Exception $ex) {
431
            dd($ex);
432
            throw new \Exception('Problem while adding licence to cart');
433
        }
434
    }
435
436
    public function cartUpdate($id, $key, $value)
437
    {
438
        try {
439
            Cart::update($id, [
440
                $key => $value, // new item name
441
                    ]
442
            );
443
        } catch (\Exception $ex) {
444
        }
445
    }
446
447
    public function addCurrencyAttributes($id)
448
    {
449
        try {
450
            $currency = \Session::get('currency');
451
            $product = $this->product->where('id', $id)->first();
452
//dd($product);
453 View Code Duplication
            if ($product) {
454
                $productCurrency = $product->price()->where('currency', $currency)->first()->currency;
455
                $currency = $this->currency->where('code', $productCurrency)->get()->toArray();
456
            } else {
457
                $currency = [];
458
            }
459
460
            return $currency;
461
        } catch (\Exception $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
462
        }
463
    }
464
465
    public function addCouponUpdate()
466
    {
467
        try {
468
            $code = \Input::get('coupon');
469
//dd($code);
470
            $cart = Cart::getContent();
471
            foreach ($cart as $item) {
472
                $id = $item->id;
473
            }
474
            $promo_controller = new \App\Http\Controllers\Payment\PromotionController();
475
            $result = $promo_controller->checkCode($code, $id);
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
476
//dd($result);
477
            if ($result == 'success') {
478
                return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
479
            }
480
        } catch (\Exception $ex) {
481
            dd($ex);
482
483
            return redirect()->back()->with('fails', $ex->getMessage());
484
        }
485
    }
486
487
    public function getTaxByPriority($tax_class_id)
488
    {
489
        try {
490
            $taxe_relation = $this->tax->where('tax_classes_id', $tax_class_id)->groupBy('level')->get();
491
492
            return $taxe_relation;
493
        } catch (\Exception $ex) {
494
            dd($ex);
495
            throw new \Exception('error in get tax priority');
496
        }
497
    }
498
499
    public static function rounding($price)
500
    {
501
        try {
502
            $tax_rule = new \App\Model\Payment\TaxOption();
503
            $rule = $tax_rule->findOrFail(1);
504
            $rounding = $rule->rounding;
505
            if ($rounding == 1) {
506
                return round($price);
507
            } else {
508
                return $price;
509
            }
510
        } catch (\Exception $ex) {
511
            dd($ex);
512
            throw new \Exception('error in get tax priority');
513
        }
514
    }
515
516
    public function contactUs()
517
    {
518
        try {
519
            return view('themes.default1.front.contact');
520
        } catch (\Exception $ex) {
521
            return redirect()->back()->with('fails', $ex->getMessage());
522
        }
523
    }
524
525
    public function postContactUs(Request $request)
526
    {
527
        $this->validate($request, [
528
            'name'    => 'required',
529
            'email'   => 'required|email',
530
            'message' => 'required',
531
        ]);
532
533
        $set = new \App\Model\Common\Setting();
534
        $set = $set->findOrFail(1);
535
536
        try {
537
            $from = $set->email;
538
            $fromname = $set->company;
539
            $toname = '';
540
            $to = '[email protected]';
541
            $data = '';
542
            $data .= 'Name: '.$request->input('name').'<br/s>';
543
            $data .= 'Email: '.$request->input('email').'<br/>';
544
            $data .= 'Message: '.$request->input('message').'<br/>';
545
            $data .= 'Mobile: '.$request->input('Mobile').'<br/>';
546
547
            $subject = 'Faveo billing enquiry';
548
            $this->templateController->Mailing($from, $to, $data, $subject, [], $fromname, $toname);
549
            //$this->templateController->Mailing($from, $to, $data, $subject);
550
            return redirect()->back()->with('success', 'Your message was sent successfully. Thanks.');
551
        } catch (\Exception $ex) {
552
            return redirect()->back()->with('fails', $ex->getMessage());
553
        }
554
    }
555
556
    public function addCartBySlug($slug)
557
    {
558
        try {
559
            if ($slug == 'helpdesk-with-kb-pro-edition') {
560
                $id = 8;
561
            }
562
            if ($slug == 'helpdesk-and-kb-community') {
563
                $id = 7;
564
            }
565
566
            return redirect("pricing?id=$id");
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
567
        } catch (\Exception $ex) {
568
            return redirect()->back()->with('fails', $ex->getMessage());
569
        }
570
    }
571
572 View Code Duplication
    public static function findCountryByGeoip($iso)
573
    {
574
        try {
575
            $country = \App\Model\Common\Country::where('country_code_char2', $iso)->first();
576
            if ($country) {
577
                return $country->country_code_char2;
578
            } else {
579
                return 'US';
580
            }
581
        } catch (\Exception $ex) {
582
            throw new \Exception($ex->getMessage());
583
        }
584
    }
585
586 View Code Duplication
    public static function getCountryByCode($code)
587
    {
588
        try {
589
            $country = \App\Model\Common\Country::where('country_code_char2', $code)->first();
590
            if ($country) {
591
                return $country->country_name;
592
            }
593
        } catch (\Exception $ex) {
594
            throw new \Exception($ex->getMessage());
595
        }
596
    }
597
598
    public static function findStateByRegionId($iso)
599
    {
600
        try {
601
            if ($iso) {
602
                $states = \App\Model\Common\State::where('country_code_char2', $iso)->lists('state_subdivision_name', 'state_subdivision_code')->toArray();
603
            } else {
604
                $states = [];
605
            }
606
607
            return $states;
608
        } catch (\Exception $ex) {
609
            throw new \Exception($ex->getMessage());
610
        }
611
    }
612
613
    public static function getTimezoneByName($name)
614
    {
615
        try {
616
            if ($name) {
617
                $timezone = \App\Model\Common\Timezone::where('name', $name)->first();
618
                if ($timezone) {
619
                    $timezone = $timezone->id;
620
                } else {
621
                    $timezone = '114';
622
                }
623
            } else {
624
                $timezone = '114';
625
            }
626
627
            return $timezone;
628
        } catch (\Exception $ex) {
629
            throw new \Exception($ex->getMessage());
630
        }
631
    }
632
633
    public static function getStateByCode($code)
634
    {
635
        try {
636
            if (is_int($code)) {
637
                return [];
638
            }
639
            if ($code) {
640
                $subregion = \App\Model\Common\State::where('state_subdivision_code', $code)->first();
641
                if (!$subregion) {
642
                    return [];
643
                }
644
645
                return ['id' => $subregion->state_subdivision_code, 'name' => $subregion->state_subdivision_name];
646
            } else {
647
                return [];
648
            }
649
        } catch (\Exception $ex) {
650
            throw new \Exception($ex->getMessage());
651
        }
652
    }
653
654
    public static function calculateTax($productid, $currency, $cart = 1, $cart1 = 0, $shop = 0)
655
    {
656
        try {
657
            $template_controller = new TemplateController();
658
            $result = $template_controller->checkTax($productid, $currency, $cart, $cart1, $shop);
659
            $result = self::rounding($result);
660
661
            return $result;
662
        } catch (\Exception $ex) {
663
            return redirect()->back()->with('fails', $ex->getMessage());
664
        }
665
    }
666
667
    public static function taxValue($rate, $price)
668
    {
669
        try {
670
            $tax = $price / (($rate / 100) + 1);
671
            $result = $price - $tax;
672
            $result = self::rounding($result);
673
674
            return $result;
675
        } catch (\Exception $ex) {
676
            return redirect()->back()->with('fails', $ex->getMessage());
677
        }
678
    }
679
680
    public static function addons()
681
    {
682
        try {
683
            $items = Cart::getContent();
684
            $cart_productids = [];
685
            if (count($items) > 0) {
686
                foreach ($items as $key => $item) {
687
                    $cart_productids[] = $key;
688
                }
689
            }
690
            //dd($productids);
691
            $_this = new self();
692
            $products = $_this->products($cart_productids);
693
694
            return $products;
695
        } catch (\Exception $ex) {
696
            return redirect()->back()->with('fails', $ex->getMessage());
697
        }
698
    }
699
700
    public function products($ids)
701
    {
702
        try {
703
            $parents = $this->product
704
                    ->whereNotNull('parent')
705
                    ->where('parent', '!=', 0)
706
                    ->where('category', 'addon')
707
                    ->lists('parent', 'id')
708
                    ->toArray();
709
            foreach ($parents as $key => $parent) {
710
                //dd($key);
711
                if (is_array($parent)) {
712
                    $parent = implode(',', $parent);
713
                }
714
                $parents_string[$key] = $parent;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$parents_string was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parents_string = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
715
            }
716
            $parent = [];
717
            foreach ($parents_string as $key => $value) {
0 ignored issues
show
Bug introduced by
The variable $parents_string does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
718
                if (strpos($value, ',') !== false) {
719
                    $value = explode(',', $value);
720
                }
721
                $parent[$key] = $value;
722
            }
723
724
            $productid = [];
725
            foreach ($parent as $key => $id) {
726
                if (in_array($id, $ids)) {
727
                    $productid[] = $key;
728
                }
729
                if (is_array($id)) {
730
                    foreach ($id as $i) {
731
                        if (in_array($i, $ids)) {
732
                            $productid[] = $key;
733
                        }
734
                    }
735
                }
736
            }
737
            $parent_products = $this->getProductById($productid);
738
739
            return $parent_products;
740
        } catch (\Exception $ex) {
741
            dd($ex);
742
            throw new \Exception($ex->getMessage());
743
        }
744
    }
745
746
    public function getProductById($ids)
747
    {
748
        try {
749
            $products = $this->product
750
                    ->whereIn('id', $ids)
751
                    ->get();
752
753
            return $products;
754
        } catch (\Exception $ex) {
755
            dd($ex);
756
            throw new \Exception($ex->getMessage());
757
        }
758
    }
759
}
760