Completed
Push — development ( ac1baf...0dc2ec )
by Ashutosh
09:55
created

BaseTemplateController   F

Complexity

Total Complexity 65

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 65
eloc 113
dl 0
loc 203
rs 3.2
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrice() 0 9 2
B calculateTotalcart() 0 10 7
B ifStatement() 0 39 6
A withoutTaxRelation() 0 12 2
F getCartResult() 0 29 25
A getResult() 0 8 5
A prices() 0 18 2
A calculateTotal() 0 6 1
A taxProcess() 0 19 4
A getTaxAmount() 0 18 4
B calculateSub() 0 9 7

How to fix   Complexity   

Complex Class

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

1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Payment\Plan;
7
8
class BaseTemplateController extends Controller
9
{
10
    public function ifStatement($rate, $price, $cart1, $shop1, $country = '', $state = '')
11
    {
12
        try {
13
            $tax_rule = $this->tax_rule->find(1);
14
            $product = $tax_rule->inclusive;
15
            $shop = $tax_rule->shop_inclusive;
16
            $cart = $tax_rule->cart_inclusive;
17
            $result = $price;
18
            $controller = new \App\Http\Controllers\Front\GetPageTemplateController();
19
            $location = $controller->getLocation();
20
21
            $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']);
22
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']);
23
            $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
24
            $state_code = $location['countryCode'].'-'.$location['region'];
25
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
26
            $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']);
27
            $country_iso = $location['countryCode'];
28
29
            $geoip_country = '';
30
            $geoip_state = '';
31
            if (\Auth::user()) {
32
                $geoip_country = \Auth::user()->country;
33
                $geoip_state = \Auth::user()->state;
34
            }
35
            if ($geoip_country == '') {
36
                $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($country_iso);
37
            }
38
            $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
39
            if ($geoip_state == '') {
40
                if (array_key_exists('id', $geoip_state_array)) {
41
                    $geoip_state = $geoip_state_array['id'];
42
                }
43
            }
44
            $result = $this->getResult($country, $geoip_country, $state, $geoip_state, $shop, $cart, $cart1, $shop1, $rate, $product, $price);
45
        } catch (\Exception $ex) {
46
            Bugsnag::notifyException($ex);
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Common\Bugsnag was not found. Did you mean Bugsnag? If so, make sure to prefix the type with \.
Loading history...
47
48
            throw new \Exception($ex->getMessage());
49
        }
50
    }
51
52
    public function getResult($country, $geoip_country, $state, $geoip_state, $shop, $cart, $cart1, $shop1, $rate, $product, $price)
53
    {
54
55
    	  if ($country == $geoip_country || $state == $geoip_state || ($country == '' && $state == '')) {
56
            $result = $this->getCartResult($product,$shop,$cart,$rate,$price,$cart1,$shop1);
57
           
58
          }
59
         return $result;
60
    }
61
62
    public function getCartResult($product,$shop,$cart,$rate,$price,$cart1,$shop1)
63
    {
64
         if ($product == 1 && $shop == 1 && $cart == 1) {
65
66
                $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1 = 0);
67
            }
68
            if ($product == 1 && $shop == 0 && $cart == 0) {
69
                $result = $this->calculateSub($rate, $price, $cart1 = 1, $shop1 = 1);
70
            }
71
            if ($product == 1 && $shop == 1 && $cart == 0) {
72
                $result = $this->calculateSub($rate, $price, $cart1, $shop1 = 0);
73
            }
74
            if ($product == 1 && $shop == 0 && $cart == 1) {
75
                $result = $this->calculateSub($rate, $price, $cart1 = 0, $shop1);
76
            }
77
            if ($product == 0 && $shop == 0 && $cart == 0) {
78
                $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1 = 0);
79
            }
80
            if ($product == 0 && $shop == 1 && $cart == 1) {
81
                $result = $this->calculateTotalcart($rate, $price, $cart1, $shop1);
82
            }
83
            if ($product == 0 && $shop == 1 && $cart == 0) {
84
                $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1);
85
            }
86
            if ($product == 0 && $shop == 0 && $cart == 1) {
87
                $result = $this->calculateTotalcart($rate, $price, $cart = 1, $shop = 1);
88
            }
89
90
            return $result;
91
92
    }
93
94
    public function calculateTotalcart($rate, $price, $cart, $shop)
95
    {
96
        if (($cart == 1 && $shop == 1) || ($cart == 1 && $shop == 0) || ($cart == 0 && $shop == 1)) {
97
            $tax_amount = $price * ($rate / 100);
98
            $total = $price + $tax_amount;
99
100
            return $total;
101
        }
102
103
        return $price;
104
    }
105
106
    public function calculateSub($rate, $price, $cart, $shop)
107
    {
108
        if (($cart == 1 && $shop == 1) || ($cart == 1 && $shop == 0) || ($cart == 0 && $shop == 1)) {
109
            $total = $price / (($rate / 100) + 1);
110
111
            return $total;
112
        }
113
114
        return $price;
115
    }
116
117
    public function getPrice($price, $currency, $value, $cost)
118
    {
119
        if ($currency == 'INR') {
120
            $price[$value->id] = '₹'.' '.$cost;
121
        } else {
122
            $price[$value->id] = '$'.' '.$cost;
123
        }
124
125
        return $price;
126
    }
127
128
    public function prices($id)
129
    {
130
        $plan = new Plan();
131
        $plans = $plan->where('product', $id)->get();
132
        $price = [];
133
        $cart_controller = new \App\Http\Controllers\Front\CartController();
134
        $currency = $cart_controller->currency();
135
136
        foreach ($plans as $value) {
137
            $cost = $value->planPrice()->where('currency', $currency)->first()->add_price;
138
139
            $cost = \App\Http\Controllers\Front\CartController::rounding($cost);
140
            $months = round($value->days / 30 / 12);
141
            $price = $this->getPrice($price, $currency, $value, $cost);
142
        }
143
        $this->leastAmount($id);
144
145
        return $price;
146
    }
147
148
    public function withoutTaxRelation($productid, $currency)
149
    {
150
        try {
151
            $product = $this->product->findOrFail($productid);
152
            $controller = new \App\Http\Controllers\Front\CartController();
153
            $price = $controller->cost($productid);
154
155
            return $price;
156
        } catch (\Exception $ex) {
157
            Bugsnag::notifyException($ex);
158
159
            throw new \Exception($ex->getMessage());
160
        }
161
    }
162
163
    public function taxProcess($taxes, $price, $cart, $shop)
164
    {
165
        try {
166
            $rate = '';
167
            $tax_amount = '';
168
            foreach ($taxes as $tax) {
169
                if ($tax->compound != 1) {
170
                    $rate += $tax->rate;
171
                } else {
172
                    $rate = $tax->rate;
173
                }
174
                $tax_amount = $this->ifStatement($rate, $price, $cart, $shop, $tax->country, $tax->state);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $tax_amount is correct as $this->ifStatement($rate...->country, $tax->state) targeting App\Http\Controllers\Com...ntroller::ifStatement() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
175
            }
176
177
            return $tax_amount;
178
        } catch (\Exception $ex) {
179
            Bugsnag::notifyException($ex);
180
181
            throw new \Exception($ex->getMessage());
182
        }
183
    }
184
185
    public function getTaxAmount($cart, $taxes, $price, $cart1, $shop)
186
    {
187
        if ($cart == 1) {
188
            $tax_amount = $this->taxProcess($taxes, $price, $cart1, $shop);
189
        } else {
190
            $rate = '';
191
            foreach ($taxes as $tax) {
192
                if ($tax->compound != 1) {
193
                    $rate += $tax->rate;
194
                } else {
195
                    $rate = $tax->rate;
196
                    $price = $this->calculateTotal($rate, $price);
197
                }
198
                $tax_amount = $this->calculateTotal($rate, $price);
199
            }
200
        }
201
202
        return $tax_amount;
203
    }
204
205
    public function calculateTotal($rate, $price)
206
    {
207
        $tax_amount = $price * ($rate / 100);
208
        $total = $price + $tax_amount;
209
210
        return $total;
211
    }
212
}
213