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

BaseTemplateController   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 48
eloc 119
dl 0
loc 209
rs 8.5599
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrice() 0 9 2
B ifStatement() 0 39 6
A withoutTaxRelation() 0 12 2
A getResult() 0 7 5
A getCartResult() 0 11 3
A prices() 0 17 2
B getTotalCart() 0 16 9
A calculateTotal() 0 6 1
A taxProcess() 0 18 4
A smtpConfig() 0 11 1
B getTotalSub() 0 16 9
A getTaxAmount() 0 18 4

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
use Bugsnag;
8
use Config;
9
10
class BaseTemplateController extends Controller
11
{
12
    public function smtpConfig($driver, $port, $host, $enc, $email, $password, $name)
13
    {
14
        Config::set('mail.driver', $driver);
15
        Config::set('mail.password', $password);
16
        Config::set('mail.username', $email);
17
        Config::set('mail.encryption', $enc);
18
        Config::set('mail.from', ['address' => $email, 'name' => $name]);
19
        Config::set('mail.port', intval($port));
20
        Config::set('mail.host', $host);
21
22
        return 'success';
23
    }
24
25
26
    public function ifStatement($rate, $price, $cart1, $shop1, $country = '', $state = '')
27
    {
28
        try {
29
            $tax_rule = $this->tax_rule->find(1);
30
            $product = $tax_rule->inclusive;
31
            $shop = $tax_rule->shop_inclusive;
32
            $cart = $tax_rule->cart_inclusive;
33
            $result = $price;
34
            $controller = new \App\Http\Controllers\Front\GetPageTemplateController();
35
            $location = $controller->getLocation();
36
37
            $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']);
38
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']);
39
            $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
40
            $state_code = $location['countryCode'].'-'.$location['region'];
41
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
42
            $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']);
43
            $country_iso = $location['countryCode'];
44
45
            $geoip_country = '';
46
            $geoip_state = '';
47
            if (\Auth::user()) {
48
                $geoip_country = \Auth::user()->country;
49
                $geoip_state = \Auth::user()->state;
50
            }
51
            if ($geoip_country == '') {
52
                $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($country_iso);
53
            }
54
            $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
55
            if ($geoip_state == '') {
56
                if (array_key_exists('id', $geoip_state_array)) {
57
                    $geoip_state = $geoip_state_array['id'];
58
                }
59
            }
60
            $result = $this->getResult($country, $geoip_country, $state, $geoip_state, $shop, $cart, $cart1, $shop1, $rate, $product, $price);
61
            return $result;
62
        } catch (\Exception $ex) {
63
            Bugsnag::notifyException($ex);
64
            throw new \Exception($ex->getMessage());
65
        }
66
    }
67
68
    public function getResult($country, $geoip_country, $state, $geoip_state, $shop, $cart, $cart1, $shop1, $rate, $product, $price)
69
    {
70
        if ($country == $geoip_country || $state == $geoip_state || ($country == '' && $state == '')) {
71
            $result = $this->getCartResult($product, $shop, $cart, $rate, $price, $cart1, $shop1);
72
        }
73
74
        return $result;
75
    }
76
77
    public function getCartResult($product, $shop, $cart, $rate, $price, $cart1, $shop1)
78
    {
79
        if ($product == 1) {
80
            $result = $this->getTotalSub($shop, $cart, $rate, $price, $cart1, $shop1);
81
        }
82
83
        if ($product == 0) {
84
            $result = $this->getTotalCart($shop, $cart, $price, $cart1, $shop1);
0 ignored issues
show
Bug introduced by
The call to App\Http\Controllers\Com...troller::getTotalCart() has too few arguments starting with shop1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
            /** @scrutinizer ignore-call */ 
85
            $result = $this->getTotalCart($shop, $cart, $price, $cart1, $shop1);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
85
        }
86
87
        return $result;
88
    }
89
90
    public function getTotalCart($shop, $cart, $rate, $price, $cart1, $shop1)
91
    {
92
        if ($shop == 0 && $cart == 0) {
93
            $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1 = 0);
94
        }
95
        if ($shop == 1 && $cart == 1) {
96
            $result = $this->calculateTotalcart($rate, $price, $cart1, $shop1);
97
        }
98
        if ($shop == 1 && $cart == 0) {
99
            $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1);
100
        }
101
        if ($shop == 0 && $cart == 1) {
102
            $result = $this->calculateTotalcart($rate, $price, $cart = 1, $shop = 1);
103
        }
104
105
        return $result;
106
    }
107
108
    public function getTotalSub($shop, $cart, $rate, $price, $cart1, $shop1)
109
    {
110
        if ($shop == 1 && $cart == 1) {
111
            $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1 = 0);
112
        }
113
        if ($shop == 0 && $cart == 0) {
114
            $result = $this->calculateSub($rate, $price, $cart1 = 1, $shop1 = 1);
115
        }
116
        if ($shop == 1 && $cart == 0) {
117
            $result = $this->calculateSub($rate, $price, $cart1, $shop1 = 0);
118
        }
119
        if ($shop == 0 && $cart == 1) {
120
            $result = $this->calculateSub($rate, $price, $cart1 = 0, $shop1);
121
        }
122
123
        return $result;
124
    }
125
126
   
127
    public function getPrice($price, $currency, $value, $cost)
128
    {
129
        if ($currency == 'INR') {
130
            $price[$value->id] = '₹'.' '.$cost;
131
        } else {
132
            $price[$value->id] = '$'.' '.$cost;
133
        }
134
135
        return $price;
136
    }
137
138
    public function prices($id)
139
    {
140
        $plan = new Plan();
141
        $plans = $plan->where('product', $id)->get();
142
        $price = [];
143
        $cart_controller = new \App\Http\Controllers\Front\CartController();
144
        $currency = $cart_controller->currency();
145
146
        foreach ($plans as $value) {
147
            $cost = $value->planPrice()->where('currency', $currency)->first()->add_price;
148
            $cost = \App\Http\Controllers\Front\CartController::rounding($cost);
149
            $months = round($value->days / 30 / 12);
150
            $price = $this->getPrice($price, $currency, $value, $cost);
151
        }
152
        $this->leastAmount($id);
153
154
        return $price;
155
    }
156
157
    public function withoutTaxRelation($productid, $currency)
158
    {
159
        try {
160
            $product = $this->product->findOrFail($productid);
161
            $controller = new \App\Http\Controllers\Front\CartController();
162
            $price = $controller->cost($productid);
163
164
            return $price;
165
        } catch (\Exception $ex) {
166
            Bugsnag::notifyException($ex);
167
168
            throw new \Exception($ex->getMessage());
169
        }
170
    }
171
172
    public function taxProcess($taxes, $price, $cart, $shop)
173
    {
174
        try {
175
            $rate = '';
176
            $tax_amount = '';
177
            foreach ($taxes as $tax) {
178
                if ($tax->compound != 1) {
179
                    $rate += $tax->rate;
180
                } else {
181
                    $rate = $tax->rate;
182
                }
183
                $tax_amount = $this->ifStatement($rate, $price, $cart, $shop, $tax->country, $tax->state);
184
            }
185
186
            return $tax_amount;
187
        } catch (\Exception $ex) {
188
            Bugsnag::notifyException($ex);
189
            throw new \Exception($ex->getMessage());
190
        }
191
    }
192
193
    public function getTaxAmount($cart, $taxes, $price, $cart1, $shop)
194
    {
195
        if ($cart == 1) {
196
            $tax_amount = $this->taxProcess($taxes, $price, $cart1, $shop);
197
        } else {
198
            $rate = '';
199
            foreach ($taxes as $tax) {
200
                if ($tax->compound != 1) {
201
                    $rate += $tax->rate;
202
                } else {
203
                    $rate = $tax->rate;
204
                    $price = $this->calculateTotal($rate, $price);
205
                }
206
                $tax_amount = $this->calculateTotal($rate, $price);
207
            }
208
        }
209
210
        return $tax_amount;
211
    }
212
213
    public function calculateTotal($rate, $price)
214
    {
215
        $tax_amount = $price * ($rate / 100);
216
        $total = $price + $tax_amount;
217
218
        return $total;
219
    }
220
}
221