Total Complexity | 58 |
Total Lines | 259 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BaseInvoiceController 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 BaseInvoiceController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class BaseInvoiceController extends ExtendedBaseInvoiceController |
||
17 | { |
||
18 | /** |
||
19 | *Tax When state is not empty. |
||
20 | */ |
||
21 | public function getTaxWhenState($user_state, $productid, $origin_state) |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | *Tax When from other Country. |
||
65 | */ |
||
66 | public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid) |
||
92 | } |
||
93 | |||
94 | public function getExpiryStatus($start, $end, $now) |
||
95 | { |
||
96 | $whenDateNotSet = $this->whenDateNotSet($start, $end); |
||
97 | if ($whenDateNotSet) { |
||
98 | return $whenDateNotSet; |
||
99 | } |
||
100 | $whenStartDateSet = $this->whenStartDateSet($start, $end, $now); |
||
101 | if ($whenStartDateSet) { |
||
102 | return $whenStartDateSet; |
||
103 | } |
||
104 | $whenEndDateSet = $this->whenEndDateSet($start, $end, $now); |
||
105 | if ($whenEndDateSet) { |
||
106 | return $whenEndDateSet; |
||
107 | } |
||
108 | $whenBothAreSet = $this->whenBothSet($start, $end, $now); |
||
109 | if ($whenBothAreSet) { |
||
110 | return $whenBothAreSet; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | public function whenDateNotSet($start, $end) |
||
115 | { |
||
116 | //both not set, always true |
||
117 | if (($start == null || $start == '0000-00-00 00:00:00') && |
||
118 | ($end == null || $end == '0000-00-00 00:00:00')) { |
||
119 | return 'success'; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | public function whenStartDateSet($start, $end, $now) |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | public function whenEndDateSet($start, $end, $now) |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | public function whenBothSet($start, $end, $now) |
||
145 | { |
||
146 | //both set |
||
147 | if (($end != null || $start != '0000-00-00 00:00:00') && ($start != null || $start != '0000-00-00 00:00:00')) { |
||
148 | if ($end >= $now && $start <= $now) { |
||
149 | return 'success'; |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | |||
154 | public function postPayment($invoiceid, Request $request) |
||
155 | { |
||
156 | $this->validate($request, [ |
||
157 | 'payment_method' => 'required', |
||
158 | 'amount' => 'required|numeric', |
||
159 | 'payment_date' => 'required|date_format:Y-m-d', |
||
160 | ]); |
||
161 | |||
162 | try { |
||
163 | $payment_method = $request->input('payment_method'); |
||
164 | $payment_status = 'success'; |
||
165 | $payment_date = $request->input('payment_date'); |
||
166 | $amount = $request->input('amount'); |
||
167 | $payment = $this->updateInvoicePayment( |
||
168 | $invoiceid, |
||
169 | $payment_method, |
||
170 | $payment_status, |
||
171 | $payment_date, |
||
172 | $amount |
||
173 | ); |
||
174 | |||
175 | return redirect()->back()->with('success', 'Payment Accepted Successfully'); |
||
176 | } catch (\Exception $ex) { |
||
177 | Bugsnag::notifyException($ex); |
||
178 | |||
179 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | public function domain($id) |
||
184 | { |
||
185 | try { |
||
186 | if (\Session::has('domain'.$id)) { |
||
187 | $domain = \Session::get('domain'.$id); |
||
188 | } else { |
||
189 | $domain = ''; |
||
190 | } |
||
191 | |||
192 | return $domain; |
||
193 | } catch (\Exception $ex) { |
||
194 | Bugsnag::notifyException($ex); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | public function checkExpiry($code = '') |
||
199 | { |
||
200 | try { |
||
201 | if ($code != '') { |
||
202 | $promotion = Promotion::where('code', $code)->first(); |
||
203 | |||
204 | $start = $promotion->start; |
||
205 | $end = $promotion->expiry; |
||
206 | $now = \Carbon\Carbon::now(); |
||
207 | $getExpiryStatus = $this->getExpiryStatus($start, $end, $now); |
||
208 | |||
209 | return $getExpiryStatus; |
||
210 | } |
||
211 | } catch (\Exception $ex) { |
||
212 | throw new \Exception(\Lang::get('message.check-expiry')); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /* |
||
217 | *Edit Invoice Total. |
||
218 | */ |
||
219 | public function invoiceTotalChange(Request $request) |
||
229 | } |
||
230 | |||
231 | public function calculateTotal($rate, $total) |
||
232 | { |
||
233 | try { |
||
234 | $total = intval($total); |
||
235 | $rates = explode(',', $rate); |
||
236 | $rule = new TaxOption(); |
||
237 | $rule = $rule->findOrFail(1); |
||
238 | if ($rule->inclusive == 0) { |
||
239 | foreach ($rates as $rate1) { |
||
240 | if ($rate1 != '') { |
||
241 | $rateTotal = str_replace('%', '', $rate1); |
||
242 | $total += $total * ($rateTotal / 100); |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | |||
247 | return intval(round($total)); |
||
248 | } catch (\Exception $ex) { |
||
249 | app('log')->warning($ex->getMessage()); |
||
250 | Bugsnag::notifyException($ex); |
||
251 | |||
252 | throw new \Exception($ex->getMessage()); |
||
253 | } |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Check if Session has Code and Value of Code. |
||
258 | * |
||
259 | * @author Ashutosh Pathak <[email protected]> |
||
260 | * |
||
261 | * @date 2019-02-22T13:10:50+0530 |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | protected function getCodeFromSession() |
||
275 | } |
||
276 | } |
||
277 |