| Total Complexity | 50 |
| Total Lines | 220 |
| 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 |
||
| 15 | class BaseInvoiceController extends Controller |
||
| 16 | { |
||
| 17 | public function invoiceGenerateByForm(Request $request, $user_id = '') |
||
| 18 | { |
||
| 19 | $qty = 1; |
||
| 20 | |||
| 21 | try { |
||
| 22 | if ($user_id == '') { |
||
| 23 | $user_id = \Request::input('user'); |
||
| 24 | } |
||
| 25 | $productid = $request->input('product'); |
||
| 26 | $code = $request->input('code'); |
||
| 27 | $total = $request->input('price'); |
||
| 28 | $plan = $request->input('plan'); |
||
| 29 | $description = $request->input('description'); |
||
| 30 | if ($request->has('domain')) { |
||
| 31 | $domain = $request->input('domain'); |
||
| 32 | $this->setDomain($productid, $domain); |
||
| 33 | } |
||
| 34 | $controller = new \App\Http\Controllers\Front\CartController(); |
||
| 35 | $currency = $controller->currency($user_id); |
||
| 36 | $number = rand(11111111, 99999999); |
||
| 37 | $date = \Carbon\Carbon::now(); |
||
| 38 | $product = Product::find($productid); |
||
| 39 | $cost = $controller->cost($productid, $user_id, $plan); |
||
| 40 | if ($cost != $total) { |
||
| 41 | $grand_total = $total; |
||
| 42 | } |
||
| 43 | $grand_total = $this->getGrandTotal($code, $total, $cost, $productid, $currency); |
||
| 44 | $grand_total = $qty * $grand_total; |
||
| 45 | |||
| 46 | $tax = $this->checkTax($product->id, $user_id); |
||
| 47 | $tax_name = ''; |
||
| 48 | $tax_rate = ''; |
||
| 49 | if (!empty($tax)) { |
||
| 50 | $tax_name = $tax[0]; |
||
| 51 | $tax_rate = $tax[1]; |
||
| 52 | } |
||
| 53 | |||
| 54 | $grand_total = $this->calculateTotal($tax_rate, $grand_total); |
||
| 55 | $grand_total = \App\Http\Controllers\Front\CartController::rounding($grand_total); |
||
| 56 | |||
| 57 | $invoice = Invoice::create(['user_id' => $user_id, 'number' => $number, 'date' => $date, 'grand_total' => $grand_total, 'currency' => $currency, 'status' => 'pending', 'description' => $description]); |
||
| 58 | |||
| 59 | $items = $this->createInvoiceItemsByAdmin($invoice->id, $productid, $code, $total, $currency, $qty, $plan, $user_id, $tax_name, $tax_rate); |
||
| 60 | $result = $this->getMessage($items, $user_id); |
||
| 61 | } catch (\Exception $ex) { |
||
| 62 | app('log')->useDailyFiles(storage_path().'/laravel.log'); |
||
| 63 | app('log')->info($ex->getMessage()); |
||
| 64 | Bugsnag::notifyException($ex); |
||
| 65 | $result = ['fails' => $ex->getMessage()]; |
||
| 66 | } |
||
| 67 | |||
| 68 | return response()->json(compact('result')); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | *Tax When state is not empty. |
||
| 73 | */ |
||
| 74 | public function getTaxWhenState($user_state, $productid, $origin_state) |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | *Tax When from other Country. |
||
| 114 | */ |
||
| 115 | public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid) |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | public function whenDateNotSet($start, $end) |
||
| 145 | { |
||
| 146 | //both not set, always true |
||
| 147 | if (($start == null || $start == '0000-00-00 00:00:00') && |
||
| 148 | ($end == null || $end == '0000-00-00 00:00:00')) { |
||
| 149 | return 'success'; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | public function whenStartDateSet($start, $end, $now) |
||
| 154 | { |
||
| 155 | //only starting date set, check the date is less or equel to today |
||
| 156 | if (($start != null || $start != '0000-00-00 00:00:00') |
||
| 157 | && ($end == null || $end == '0000-00-00 00:00:00')) { |
||
| 158 | if ($start <= $now) { |
||
| 159 | return 'success'; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | public function whenEndDateSet($start, $end, $now) |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | public function whenBothSet($start, $end, $now) |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | public function postPayment($invoiceid, Request $request) |
||
| 185 | { |
||
| 186 | $this->validate($request, [ |
||
| 187 | 'payment_method' => 'required', |
||
| 188 | 'amount' => 'required|numeric', |
||
| 189 | 'payment_date' => 'required|date_format:Y-m-d', |
||
| 190 | ]); |
||
| 191 | |||
| 192 | try { |
||
| 193 | $payment_method = $request->input('payment_method'); |
||
| 194 | $payment_status = 'success'; |
||
| 195 | $payment_date = $request->input('payment_date'); |
||
| 196 | $amount = $request->input('amount'); |
||
| 197 | $payment = $this->updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount); |
||
| 198 | |||
| 199 | return redirect()->back()->with('success', 'Payment Accepted Successfully'); |
||
| 200 | } catch (\Exception $ex) { |
||
| 201 | Bugsnag::notifyException($ex); |
||
| 202 | |||
| 203 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | public function setDomain($productid, $domain) |
||
| 210 | { |
||
| 211 | try { |
||
| 212 | if (\Session::has('domain'.$productid)) { |
||
| 213 | \Session::forget('domain'.$productid); |
||
| 214 | } |
||
| 215 | \Session::put('domain'.$productid, $domain); |
||
| 216 | } catch (\Exception $ex) { |
||
| 217 | Bugsnag::notifyException($ex); |
||
| 218 | |||
| 219 | throw new \Exception($ex->getMessage()); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | public function domain($id) |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 |