Total Complexity | 55 |
Total Lines | 547 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 2 | Features | 0 |
Complex classes like ClientController 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 ClientController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ClientController extends BaseClientController |
||
21 | { |
||
22 | public $user; |
||
23 | public $invoice; |
||
24 | public $order; |
||
25 | public $subscription; |
||
26 | public $payment; |
||
27 | |||
28 | public function __construct() |
||
29 | { |
||
30 | $this->middleware('auth'); |
||
31 | |||
32 | $user = new User(); |
||
33 | $this->user = $user; |
||
34 | |||
35 | $invoice = new Invoice(); |
||
36 | $this->invoice = $invoice; |
||
37 | |||
38 | $order = new Order(); |
||
39 | $this->order = $order; |
||
40 | |||
41 | $subscription = new Subscription(); |
||
42 | $this->subscription = $subscription; |
||
43 | |||
44 | $payment = new Payment(); |
||
45 | $this->payment = $payment; |
||
46 | |||
47 | $product_upload = new ProductUpload(); |
||
48 | $this->product_upload = $product_upload; |
||
49 | |||
50 | $product = new Product(); |
||
51 | $this->product = $product; |
||
52 | |||
53 | $github_controller = new GithubApiController(); |
||
54 | $this->github_api = $github_controller; |
||
55 | |||
56 | $model = new Github(); |
||
57 | $this->github = $model->firstOrFail(); |
||
58 | |||
59 | $this->client_id = $this->github->client_id; |
||
60 | $this->client_secret = $this->github->client_secret; |
||
61 | } |
||
62 | |||
63 | public function invoices() |
||
64 | { |
||
65 | try { |
||
66 | return view('themes.default1.front.clients.invoice'); |
||
67 | } catch (Exception $ex) { |
||
68 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | public function getInvoices() |
||
73 | { |
||
74 | try { |
||
75 | $invoices = Invoice::leftJoin('order_invoice_relations', 'invoices.id', '=', 'order_invoice_relations.invoice_id') |
||
76 | ->select('invoices.id', 'invoices.user_id', 'invoices.date', 'invoices.number', 'invoices.grand_total', 'order_invoice_relations.order_id', 'invoices.is_renewed', 'invoices.status') |
||
77 | ->groupBy('invoices.number') |
||
78 | ->where('invoices.user_id', '=', \Auth::user()->id) |
||
79 | ->orderBy('invoices.created_at', 'desc') |
||
80 | ->get(); |
||
81 | |||
82 | return \DataTables::of($invoices) |
||
83 | ->addColumn('number', function ($model) { |
||
84 | if ($model->is_renewed) { |
||
85 | return '<a href='.url('my-invoice/'.$model->id).'>'.$model->number.'</a> '.getStatusLabel('renewed', 'badge'); |
||
86 | } else { |
||
87 | return '<a href='.url('my-invoice/'.$model->id).'>'.$model->number.'</a>'; |
||
88 | } |
||
89 | }) |
||
90 | ->addColumn('orderNo', function ($model) { |
||
91 | if ($model->is_renewed) { |
||
92 | return Order::find($model->order_id)->first()->getOrderLink($model->order_id, 'my-order'); |
||
93 | } else { |
||
94 | $allOrders = $model->order()->select('id', 'number')->get(); |
||
95 | $orderArray = ''; |
||
96 | foreach ($allOrders as $orders) { |
||
97 | $orderArray .= $orders->getOrderLink($orders->id, 'orders'); |
||
98 | } |
||
99 | |||
100 | return $orderArray; |
||
101 | } |
||
102 | }) |
||
103 | ->addColumn('date', function ($model) { |
||
104 | return getDateHtml($model->created_at); |
||
105 | }) |
||
106 | ->addColumn('total', function ($model) { |
||
107 | return currencyFormat($model->grand_total, $code = \Auth::user()->currency); |
||
108 | }) |
||
109 | ->addColumn('paid', function ($model) { |
||
110 | $payment = \App\Model\Order\Payment::where('invoice_id', $model->id)->select('amount')->get(); |
||
111 | $c = count($payment); |
||
112 | $sum = 0; |
||
113 | |||
114 | for ($i = 0; $i <= $c - 1; $i++) { |
||
115 | $sum = $sum + $payment[$i]->amount; |
||
116 | } |
||
117 | |||
118 | return currencyFormat($sum, $code = \Auth::user()->currency); |
||
119 | }) |
||
120 | ->addColumn('balance', function ($model) { |
||
121 | $payment = \App\Model\Order\Payment::where('invoice_id', $model->id)->select('amount')->get(); |
||
122 | $c = count($payment); |
||
123 | $sum = 0; |
||
124 | |||
125 | for ($i = 0; $i <= $c - 1; $i++) { |
||
126 | $sum = $sum + $payment[$i]->amount; |
||
127 | } |
||
128 | $pendingAmount = ($model->grand_total) - ($sum); |
||
129 | |||
130 | return currencyFormat($pendingAmount, $code = \Auth::user()->currency); |
||
131 | }) |
||
132 | ->addColumn('status', function ($model) { |
||
133 | return getStatusLabel($model->status, 'badge'); |
||
134 | }) |
||
135 | ->addColumn('Action', function ($model) { |
||
136 | $status = $model->status; |
||
137 | $payment = ''; |
||
138 | if ($status != 'Success' && $model->grand_total > 0) { |
||
139 | $payment = ' <a href='.url('paynow/'.$model->id). |
||
140 | " class='btn btn-primary btn-xs'><i class='fa fa-credit-card'></i> Pay Now</a>"; |
||
141 | } |
||
142 | |||
143 | return '<p><a href='.url('my-invoice/'.$model->id). |
||
144 | " class='btn btn-primary btn-xs'><i class='fa fa-eye'></i> View</a>".$payment.'</p>'; |
||
145 | }) |
||
146 | |||
147 | ->rawColumns(['number', 'orderNo', 'date', 'total', 'status', 'Action']) |
||
148 | // ->orderColumns('number', 'created_at', 'total') |
||
149 | ->make(true); |
||
150 | } catch (Exception $ex) { |
||
151 | Bugsnag::notifyException($ex); |
||
152 | echo $ex->getMessage(); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | public function getInvoice($id) |
||
157 | { |
||
158 | try { |
||
159 | $invoice = $this->invoice->findOrFail($id); |
||
160 | $user = \Auth::user(); |
||
161 | if ($invoice->user_id != $user->id) { |
||
162 | throw new \Exception('Cannot view invoice. Invalid modification of data.'); |
||
163 | } |
||
164 | $items = $invoice->invoiceItem()->get(); |
||
165 | $order = $this->order->getOrderLink($invoice->orderRelation()->value('order_id'), 'my-order'); |
||
166 | $currency = userCurrency($user->id); |
||
167 | $symbol = $currency['symbol']; |
||
168 | |||
169 | return view('themes.default1.front.clients.show-invoice', compact('invoice', 'items', 'user', 'currency', 'symbol', 'order')); |
||
170 | } catch (Exception $ex) { |
||
171 | Bugsnag::notifyException($ex); |
||
172 | |||
173 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get list of all the versions from Filesystem. |
||
179 | * |
||
180 | * @param type $productid |
||
181 | * @param type $clientid |
||
182 | * @param type $invoiceid |
||
183 | * |
||
184 | * Get list of all the versions from Filesystem. |
||
185 | * @param type $productid |
||
186 | * @param type $clientid |
||
187 | * @param type $invoiceid |
||
188 | * |
||
189 | * @return type |
||
190 | */ |
||
191 | public function getVersionList($productid, $clientid, $invoiceid) |
||
192 | { |
||
193 | try { |
||
194 | $versions = ProductUpload::where('product_id', $productid) |
||
195 | ->select( |
||
196 | 'id', |
||
197 | 'product_id', |
||
198 | 'version', |
||
199 | 'title', |
||
200 | 'description', |
||
201 | 'file', |
||
202 | 'created_at' |
||
203 | )->get(); |
||
204 | $countVersions = count($versions); |
||
205 | $countExpiry = 0; |
||
206 | $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first(); |
||
207 | $order = Order::where('invoice_id', '=', $invoice_id)->first(); |
||
208 | |||
209 | $order_id = $order->id; |
||
210 | $updatesEndDate = Subscription::select('update_ends_at') |
||
211 | ->where('product_id', $productid)->where('order_id', $order_id)->first(); |
||
212 | if ($updatesEndDate) { |
||
213 | foreach ($versions as $version) { |
||
214 | if ($version->created_at->toDateTimeString() |
||
215 | < $updatesEndDate->update_ends_at || $updatesEndDate->update_ends_at == '0000-00-00 00:00:00') { |
||
216 | $countExpiry = $countExpiry + 1; |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | return \DataTables::of($versions) |
||
222 | ->addColumn('id', function ($versions) { |
||
223 | return ucfirst($versions->id); |
||
224 | }) |
||
225 | ->addColumn('version', function ($versions) { |
||
226 | return ucfirst($versions->version); |
||
227 | }) |
||
228 | ->addColumn('title', function ($versions) { |
||
229 | return ucfirst($versions->title); |
||
230 | }) |
||
231 | ->addColumn('description', function ($versions) { |
||
232 | return ucfirst($versions->description); |
||
233 | }) |
||
234 | ->addColumn('file', function ($versions) use ($countExpiry, $countVersions, $clientid, $invoiceid, $productid) { |
||
235 | $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first(); |
||
236 | $order = Order::where('invoice_id', '=', $invoice_id)->first(); |
||
237 | $order_id = $order->id; |
||
238 | $downloadPermission = LicensePermissionsController::getPermissionsForProduct($productid); |
||
239 | $updateEndDate = Subscription::select('update_ends_at') |
||
240 | ->where('product_id', $productid)->where('order_id', $order_id)->first(); |
||
241 | |||
242 | //if product has Update expiry date ie subscription is generated |
||
243 | if ($updateEndDate) { |
||
244 | if ($downloadPermission['allowDownloadTillExpiry'] == 1) {//Perpetual download till expiry permission selected |
||
245 | $getDownload = $this->whenDownloadTillExpiry($updateEndDate, $productid, $versions, $clientid, $invoiceid); |
||
246 | |||
247 | return $getDownload; |
||
248 | } elseif ($downloadPermission['allowDownloadTillExpiry'] == 0) {//When download retires after subscription |
||
249 | $getDownload = $this->whenDownloadExpiresAfterExpiry($countExpiry, $countVersions, $updateEndDate, $productid, $versions, $clientid, $invoiceid); |
||
250 | |||
251 | return $getDownload; |
||
252 | } |
||
253 | } |
||
254 | }) |
||
255 | ->rawColumns(['version', 'title', 'description', 'file']) |
||
256 | ->make(true); |
||
257 | } catch (Exception $ex) { |
||
258 | Bugsnag::notifyException($ex); |
||
259 | echo $ex->getMessage(); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Get list of all the versions from Github. |
||
265 | * |
||
266 | * @param type $productid |
||
267 | * @param type $clientid |
||
268 | * @param type $invoiceid |
||
269 | */ |
||
270 | public function getGithubVersionList($productid, $clientid, $invoiceid) |
||
334 | } |
||
335 | } |
||
336 | |||
337 | /* |
||
338 | * Show all the orders for User |
||
339 | */ |
||
340 | |||
341 | public function getOrders() |
||
342 | { |
||
343 | try { |
||
344 | $orders = $this->getClientPanelOrdersData(); |
||
345 | |||
346 | return \DataTables::of($orders) |
||
347 | ->addColumn('id', function ($model) { |
||
348 | return $model->id; |
||
349 | }) |
||
350 | ->addColumn('product_name', function ($model) { |
||
351 | return $model->product_name; |
||
352 | }) |
||
353 | ->addColumn('number', function ($model) { |
||
354 | return '<a href='.url('my-order/'.$model->id).'>'.$model->number.'</a>'; |
||
355 | }) |
||
356 | ->addColumn('version', function ($model) { |
||
357 | return getVersionAndLabel($model->version, $model->product_id, 'badge'); |
||
358 | }) |
||
359 | ->addColumn('expiry', function ($model) { |
||
360 | return getExpiryLabel($model->update_ends_at, 'badge'); |
||
361 | }) |
||
362 | |||
363 | ->addColumn('Action', function ($model) { |
||
364 | $order_cont = new \App\Http\Controllers\Order\OrderController(); |
||
365 | $status = $order_cont->checkInvoiceStatusByOrderId($model->id); |
||
366 | $url = ''; |
||
367 | if ($status == 'success') { |
||
368 | $url = $this->renewPopup($model->sub_id, $model->product_id); |
||
369 | } |
||
370 | |||
371 | $listUrl = $this->getPopup($model, $model->product_id); |
||
372 | |||
373 | return '<a href='.url('my-order/'.$model->id)." |
||
374 | class='btn btn-primary btn-xs' style='margin-right:5px;'> |
||
375 | <i class='fa fa-eye' title='Details of order'></i> View $listUrl $url </a>"; |
||
376 | }) |
||
377 | ->rawColumns(['id', 'product_name', 'number', 'version', 'expiry', 'Action']) |
||
378 | ->make(true); |
||
379 | } catch (Exception $ex) { |
||
380 | app('log')->error($ex->getMessage()); |
||
381 | Bugsnag::notifyException($ex); |
||
382 | echo $ex->getMessage(); |
||
383 | } |
||
384 | } |
||
385 | |||
386 | public function getClientPanelOrdersData() |
||
387 | { |
||
388 | return Order::leftJoin('products', 'products.id', '=', 'orders.product') |
||
389 | ->leftJoin('subscriptions', 'orders.id', '=', 'subscriptions.order_id') |
||
390 | ->leftJoin('invoices', 'orders.invoice_id', 'invoices.id') |
||
391 | ->select('products.name as product_name', 'products.github_owner', 'products.github_repository', 'products.type', 'products.id as product_id', 'orders.id', 'orders.number', 'orders.client', 'subscriptions.id as sub_id', 'subscriptions.version', 'subscriptions.update_ends_at', 'products.name', 'orders.client', 'invoices.id as invoice_id', 'invoices.number as invoice_number') |
||
392 | ->where('orders.client', \Auth::user()->id) |
||
393 | ->get()->map(function ($element) { |
||
394 | $element->update_ends_at = strtotime($element->update_ends_at) > 1 ? $element->update_ends_at : '--'; |
||
395 | |||
396 | return $element; |
||
397 | }); |
||
398 | } |
||
399 | |||
400 | public function profile() |
||
401 | { |
||
402 | try { |
||
403 | $user = $this->user->where('id', \Auth::user()->id)->first(); |
||
404 | $is2faEnabled = $user->is_2fa_enabled; |
||
405 | $dateSinceEnabled = $user->google2fa_activation_date; |
||
406 | $timezonesList = \App\Model\Common\Timezone::get(); |
||
407 | foreach ($timezonesList as $timezone) { |
||
408 | $location = $timezone->location; |
||
409 | if ($location) { |
||
410 | $start = strpos($location, '('); |
||
411 | $end = strpos($location, ')', $start + 1); |
||
412 | $length = $end - $start; |
||
413 | $result = substr($location, $start + 1, $length - 1); |
||
414 | $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]); |
||
415 | } |
||
416 | } |
||
417 | //for display |
||
418 | $timezones = array_column($display, 'name', 'id'); |
||
419 | $state = getStateByCode($user->state); |
||
420 | $states = findStateByRegionId($user->country); |
||
421 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
422 | $selectedIndustry = \App\Model\Common\Bussiness::where('name', $user->bussiness) |
||
423 | ->pluck('name', 'short')->toArray(); |
||
424 | $selectedCompany = \DB::table('company_types')->where('name', $user->company_type) |
||
425 | ->pluck('name', 'short')->toArray(); |
||
426 | $selectedCompanySize = \DB::table('company_sizes')->where('short', $user->company_size) |
||
427 | ->pluck('name', 'short')->toArray(); |
||
428 | |||
429 | return view( |
||
430 | 'themes.default1.front.clients.profile', |
||
431 | compact('user', 'timezones', 'state', 'states', 'bussinesses', 'is2faEnabled', 'dateSinceEnabled', 'selectedIndustry', 'selectedCompany', 'selectedCompanySize') |
||
432 | ); |
||
433 | } catch (Exception $ex) { |
||
434 | Bugsnag::notifyException($ex); |
||
435 | |||
436 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
437 | } |
||
438 | } |
||
439 | |||
440 | public function getOrder($id) |
||
441 | { |
||
442 | try { |
||
443 | $user = \Auth::user(); |
||
444 | $order = $this->order->findOrFail($id); |
||
445 | if ($order->client != $user->id) { |
||
446 | throw new \Exception('Cannot view order. Invalid modification of data.'); |
||
447 | } |
||
448 | $invoice = $order->invoice()->first(); |
||
449 | $items = $order->invoice()->first()->invoiceItem()->get(); |
||
450 | $subscription = $order->subscription()->first(); |
||
451 | $date = '--'; |
||
452 | $licdate = '--'; |
||
453 | $versionLabel = '--'; |
||
454 | if ($subscription) { |
||
455 | $date = strtotime($subscription->update_ends_at) > 1 ? getExpiryLabel($subscription->update_ends_at, 'badge') : '--'; |
||
456 | $licdate = strtotime($subscription->ends_at) > 1 ? getExpiryLabel($subscription->ends_at, 'badge') : '--'; |
||
457 | $versionLabel = getVersionAndLabel($subscription->version, $order->product, 'badge'); |
||
458 | } |
||
459 | |||
460 | $installationDetails = []; |
||
461 | $licenseStatus = StatusSetting::pluck('license_status')->first(); |
||
462 | if ($licenseStatus == 1) { |
||
463 | $cont = new \App\Http\Controllers\License\LicenseController(); |
||
464 | $installationDetails = $cont->searchInstallationPath($order->serial_key, $order->product); |
||
465 | } |
||
466 | $product = $order->product()->first(); |
||
467 | $price = $product->price()->first(); |
||
468 | $licenseStatus = StatusSetting::pluck('license_status')->first(); |
||
469 | $allowDomainStatus = StatusSetting::pluck('domain_check')->first(); |
||
470 | |||
471 | return view( |
||
472 | 'themes.default1.front.clients.show-order', |
||
473 | compact('invoice', 'order', 'user', 'product', 'subscription', 'licenseStatus', 'installationDetails', 'allowDomainStatus', 'date', 'licdate', 'versionLabel') |
||
474 | ); |
||
475 | } catch (Exception $ex) { |
||
476 | Bugsnag::notifyException($ex); |
||
477 | |||
478 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
479 | } |
||
480 | } |
||
481 | |||
482 | public function getPaymentByOrderId($orderid, $userid) |
||
527 | } |
||
528 | } |
||
529 | |||
530 | public function getPaymentByOrderIdClient($orderid, $userid) |
||
567 | } |
||
568 | } |
||
569 | } |
||
570 |