Completed
Push — development ( 2be1cb...034e49 )
by Ashutosh
11:52
created

ClientController   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 507
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 53
eloc 309
dl 0
loc 507
rs 6.96
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getPaymentByOrderId() 0 36 4
A whenDownloadTillExpiry() 0 12 2
A getPaymentByOrderIdClient() 0 37 3
A getOrder() 0 26 3
A orders() 0 8 2
A getInvoices() 0 35 4
A profile() 0 30 4
A __construct() 0 33 1
A invoices() 0 6 2
A getOrders() 0 44 4
A getInvoice() 0 12 2
A whenDownloadExpiresAfterExpiry() 0 11 2
A subscriptions() 0 8 2
B getGithubVersionList() 0 64 9
B getVersionList() 0 69 9

How to fix   Complexity   

Complex Class

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
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Github\GithubApiController;
6
use App\Http\Controllers\License\LicensePermissionsController;
7
use App\Model\Common\StatusSetting;
8
use App\Model\Github\Github;
9
use App\Model\Order\Invoice;
10
use App\Model\Order\Order;
11
use App\Model\Order\Payment;
12
use App\Model\Product\Product;
13
use App\Model\Product\ProductUpload;
14
use App\Model\Product\Subscription;
15
use App\User;
16
use Auth;
17
use Bugsnag;
18
use DateTime;
19
use DateTimeZone;
20
use Exception;
21
use GrahamCampbell\Markdown\Facades\Markdown;
22
23
class ClientController extends BaseClientController
24
{
25
    public $user;
26
    public $invoice;
27
    public $order;
28
    public $subscription;
29
    public $payment;
30
31
    public function __construct()
32
    {
33
        $this->middleware('auth');
34
35
        $user = new User();
36
        $this->user = $user;
37
38
        $invoice = new Invoice();
39
        $this->invoice = $invoice;
40
41
        $order = new Order();
42
        $this->order = $order;
43
44
        $subscription = new Subscription();
45
        $this->subscription = $subscription;
46
47
        $payment = new Payment();
48
        $this->payment = $payment;
49
50
        $product_upload = new ProductUpload();
51
        $this->product_upload = $product_upload;
52
53
        $product = new Product();
54
        $this->product = $product;
55
56
        $github_controller = new GithubApiController();
57
        $this->github_api = $github_controller;
58
59
        $model = new Github();
60
        $this->github = $model->firstOrFail();
61
62
        $this->client_id = $this->github->client_id;
63
        $this->client_secret = $this->github->client_secret;
64
    }
65
66
    public function invoices()
67
    {
68
        try {
69
            return view('themes.default1.front.clients.invoice');
70
        } catch (Exception $ex) {
71
            return redirect()->back()->with('fails', $ex->getMessage());
72
        }
73
    }
74
75
    public function getInvoices()
76
    {
77
        try {
78
            $invoices = Invoice::where('user_id', \Auth::user()->id)
79
                    ->select('number', 'created_at', 'grand_total', 'id', 'status');
80
81
            return \DataTables::of($invoices->get())
82
                            ->addColumn('number', function ($model) {
83
                                return $model->number;
84
                            })
85
                            ->addColumn('date', function ($model) {
86
                                $date = $model->created_at;
87
88
                                return $date;
89
                            })
90
                            ->addColumn('total', function ($model) {
91
                                return $model->grand_total;
92
                            })
93
                            ->addColumn('Action', function ($model) {
94
                                $status = $model->status;
95
                                $payment = '';
96
                                if ($status == 'Pending' && $model->grand_total > 0) {
97
                                    $payment = '  <a href='.url('paynow/'.$model->id).
98
                                    " class='btn btn-primary btn-xs'><i class='fa fa-credit-card'></i>&nbsp;Pay Now</a>";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
99
                                }
100
101
                                return '<p><a href='.url('my-invoice/'.$model->id).
102
                                " class='btn btn-primary btn-xs'><i class='fa fa-eye'></i>&nbsp;View</a>".$payment.'</p>';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
103
                            })
104
                            ->rawColumns(['number', 'created_at', 'total', 'Action'])
105
                            // ->orderColumns('number', 'created_at', 'total')
106
                            ->make(true);
107
        } catch (Exception $ex) {
108
            Bugsnag::notifyException($ex);
109
            echo $ex->getMessage();
110
        }
111
    }
112
113
    /**
114
     * Get list of all the versions from Filesystem.
115
     *
116
     * @param type $productid
117
     * @param type $clientid
118
     * @param type $invoiceid
119
     *
120
     * Get list of all the versions from Filesystem.
121
     * @param type $productid
122
     * @param type $clientid
123
     * @param type $invoiceid
124
     *
125
     * @return type
126
     */
127
    public function getVersionList($productid, $clientid, $invoiceid)
128
    {
129
        try {
130
            $versions = ProductUpload::where('product_id', $productid)
131
            ->select(
132
                'id',
133
                'product_id',
134
                'version',
135
                'title',
136
                'description',
137
                'file',
138
                'created_at'
139
            )->get();
140
            $countVersions = count($versions);
141
            $countExpiry = 0;
142
            $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first();
143
            $order = Order::where('invoice_id', '=', $invoice_id)->first();
144
145
            $order_id = $order->id;
146
            $updatesEndDate = Subscription::select('update_ends_at')
147
                 ->where('product_id', $productid)->where('order_id', $order_id)->first();
148
            if ($updatesEndDate) {
149
                foreach ($versions as $version) {
150
                    if ($version->created_at->toDateTimeString()
151
                    < $updatesEndDate->update_ends_at || $updatesEndDate->update_ends_at =='0000-00-00 00:00:00') {
152
                        $countExpiry = $countExpiry + 1;
0 ignored issues
show
Coding Style introduced by
Increment operators should be used where possible; found "$countExpiry = $countExpiry + 1;" but expected "$countExpiry++"
Loading history...
153
                    }
154
                }
155
            }
156
157
            return \DataTables::of($versions)
158
                            ->addColumn('id', function ($versions) {
159
                                return ucfirst($versions->id);
160
                            })
161
                            ->addColumn('version', function ($versions) {
162
                                return ucfirst($versions->version);
163
                            })
164
                            ->addColumn('title', function ($versions) {
165
                                return ucfirst($versions->title);
166
                            })
167
                            ->addColumn('description', function ($versions) {
168
                                return ucfirst($versions->description);
169
                            })
170
                            ->addColumn('file', function ($versions) use ($countExpiry, $countVersions, $clientid, $invoiceid, $productid) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
171
                                $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first();
172
                                $order = Order::where('invoice_id', '=', $invoice_id)->first();
173
                                $order_id = $order->id;
174
                                $downloadPermission = LicensePermissionsController::getPermissionsForProduct($productid);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
175
                                $updateEndDate = Subscription::select('update_ends_at')
176
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
177
                                
178
                                //if product has Update expiry date ie subscription is generated
179
                                if ($updateEndDate) {
180
                                    if ($downloadPermission['allowDownloadTillExpiry'] == 1) {//Perpetual download till expiry permission selected
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 146 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
181
                                        $getDownload = $this->whenDownloadTillExpiry($updateEndDate, $productid, $versions, $clientid, $invoiceid);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 147 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
182
183
                                        return $getDownload;
184
                                    } elseif ($downloadPermission['allowDownloadTillExpiry'] == 0) {//When download retires after subscription
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
185
                                        $getDownload = $this->whenDownloadExpiresAfterExpiry($countExpiry, $countVersions, $updateEndDate, $productid, $versions, $clientid, $invoiceid);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 185 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
186
187
                                        return $getDownload;
188
                                    }
189
                                }
190
                            })
191
                            ->rawColumns(['version', 'title', 'description', 'file'])
192
                            ->make(true);
193
        } catch (Exception $ex) {
194
            Bugsnag::notifyException($ex);
195
            echo $ex->getMessage();
196
        }
197
    }
198
199
    public function whenDownloadTillExpiry($updateEndDate, $productid, $versions, $clientid, $invoiceid)
200
    {
201
        if ($versions->created_at->toDateTimeString()
202
        < $updateEndDate->update_ends_at) {
203
            return '<p><a href='.url('download/'.$productid.'/'
204
            .$clientid.'/'.$invoiceid.'/'.$versions->id).
205
            " class='btn btn-sm btn-primary'><i class='fa fa-download'>
206
            </i>&nbsp;&nbsp;Download</a>".'&nbsp;
207
208
       </p>';
209
        } else {
210
            return '<button class="btn btn-danger 
211
        btn-sm disabled">Please Renew </button>';
212
        }
213
    }
214
215
    public function whenDownloadExpiresAfterExpiry($countExpiry, $countVersions, $updatesEndDate, $productid, $versions, $clientid, $invoiceid)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 143 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
216
    {
217
        if ($countExpiry == $countVersions) {
218
            return '<p><a href='.url('download/'.$productid.'/'
219
            .$clientid.'/'.$invoiceid.'/'.$versions->id).
220
            " class='btn btn-sm btn-primary'><i class='fa fa-download'>
221
            </i>&nbsp;&nbsp;Download</a>".'&nbsp;
222
223
       </p>';
224
        } else {
225
            return '<button class="btn btn-danger 
226
        btn-sm disabled">Please Renew </button>';
227
        }
228
    }
229
230
    /**
231
     * Get list of all the versions from Github.
232
     *
233
     * @param type $productid
234
     * @param type $clientid
235
     * @param type $invoiceid
236
     */
237
    public function getGithubVersionList($productid, $clientid, $invoiceid)
238
    {
239
        try {
240
            $products = $this->product::where('id', $productid)
241
            ->select('name', 'version', 'github_owner', 'github_repository')->get();
242
            $owner = '';
243
            $repo = '';
244
            foreach ($products as $product) {
245
                $owner = $product->github_owner;
246
                $repo = $product->github_repository;
247
            }
248
            $url = "https://api.github.com/repos/$owner/$repo/releases";
249
            $countExpiry = 0;
250
            $link = $this->github_api->getCurl1($url);
251
            $link = $link['body'];
252
            $countVersions = 10; //because we are taking oly the first 10 versions
253
            $link = (array_slice($link, 0, 10, true));
254
            $order = Order::where('invoice_id', '=', $invoiceid)->first();
255
            $order_id = $order->id;
256
            $orderEndDate = Subscription::select('update_ends_at')
257
                        ->where('product_id', $productid)->where('order_id', $order_id)->first();
258
            if ($orderEndDate) {
259
                foreach ($link as $lin) {
260
                    if (strtotime($lin['created_at']) < strtotime($orderEndDate->update_ends_at) || $orderEndDate->update_ends_at =='0000-00-00 00:00:00') {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 156 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
261
                        $countExpiry = $countExpiry + 1;
0 ignored issues
show
Coding Style introduced by
Increment operators should be used where possible; found "$countExpiry = $countExpiry + 1;" but expected "$countExpiry++"
Loading history...
262
                    }
263
                }
264
            }
265
266
            return \DataTables::of($link)
267
                            ->addColumn('version', function ($link) {
268
                                return ucfirst($link['tag_name']);
269
                            })
270
                            ->addColumn('name', function ($link) {
271
                                return ucfirst($link['name']);
272
                            })
273
                            ->addColumn('description', function ($link) {
274
                                $markdown = Markdown::convertToHtml(ucfirst($link['body']));
275
276
                                return $markdown;
277
                            })
278
                            ->addColumn('file', function ($link) use ($countExpiry, $countVersions, $invoiceid, $productid) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
279
                                $order = Order::where('invoice_id', '=', $invoiceid)->first();
280
                                $order_id = $order->id;
281
                                $orderEndDate = Subscription::select('update_ends_at')
282
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
283
                                if ($orderEndDate) {
284
                                    $actionButton = $this->getActionButton($countExpiry, $countVersions, $link, $orderEndDate, $productid);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 139 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
285
286
                                    return $actionButton;
287
                                } elseif (!$orderEndDate) {
288
                                    $link = $this->github_api->getCurl1($link['zipball_url']);
289
290
                                    return '<p><a href='.$link['header']['Location']
291
                                    ." class='btn btn-sm btn-primary'>Download  </a>"
292
                                            .'&nbsp;
293
                                   </p>';
294
                                }
295
                            })
296
                            ->rawColumns(['version', 'name', 'description', 'file'])
297
                            ->make(true);
298
        } catch (Exception $ex) {
299
            Bugsnag::notifyException($ex);
300
            echo $ex->getMessage();
301
        }
302
    }
303
304
    public function orders()
305
    {
306
        try {
307
            return view('themes.default1.front.clients.order1');
308
        } catch (Exception $ex) {
309
            Bugsnag::notifyException($ex);
310
311
            return redirect()->back()->with('fails', $ex->getMessage());
312
        }
313
    }
314
315
    /*
316
     * Show all the orders for User
317
     */
318
319
    public function getOrders()
320
    {
321
        try {
322
            $orders = Order::where('client', \Auth::user()->id);
323
324
            return \DataTables::of($orders->get())
325
                            ->addColumn('id', function ($model) {
326
                                return $model->id;
327
                            })
328
                            ->addColumn('product_name', function ($model) {
329
                                return $model->product()->first()->name;
330
                            })
331
                            ->addColumn('expiry', function ($model) {
332
                                $tz = \Auth::user()->timezone()->first()->name;
333
                                $end = $this->getExpiryDate($model);
334
335
                                return $end;
336
                            })
337
338
                            ->addColumn('Action', function ($model) {
339
                                $sub = $model->subscription()->first();
340
                                $order = Order::where('id', $model->id)->select('product')->first();
341
                                $productid = $order->product;
342
                                $order_cont = new \App\Http\Controllers\Order\OrderController();
343
                                $status = $order_cont->checkInvoiceStatusByOrderId($model->id);
344
                                $url = '';
345
                                if ($status == 'success') {
346
                                    if ($sub) {
347
                                        $url = $this->renewPopup($sub->id, $productid);
348
                                    }
349
                                }
350
351
                                $listUrl = $this->getPopup($model, $productid);
352
353
                                return '<a href='.url('my-order/'.$model->id)." 
354
                                class='btn  btn-primary btn-xs' style='margin-right:5px;'>
355
                                <i class='fa fa-eye' title='Details of order'></i>&nbsp;View $listUrl $url </a>";
356
                            })
357
                            ->rawColumns(['id', 'created_at', 'ends_at', 'product', 'Action'])
358
                            ->make(true);
359
        } catch (Exception $ex) {
360
            app('log')->error($ex->getMessage());
361
            Bugsnag::notifyException($ex);
362
            echo $ex->getMessage();
363
        }
364
    }
365
366
    public function subscriptions()
367
    {
368
        try {
369
            return view('themes.default1.front.clients.subscription');
370
        } catch (Exception $ex) {
371
            Bugsnag::notifyException($ex);
372
373
            return redirect()->back()->with('fails', $ex->getMessage());
374
        }
375
    }
376
377
    public function profile()
378
    {
379
        try {
380
            $user = $this->user->where('id', \Auth::user()->id)->first();
381
            //dd($user);
382
            $timezonesList = \App\Model\Common\Timezone::get();
383
            foreach ($timezonesList as $timezone) {
384
                $location = $timezone->location;
385
                if ($location) {
386
                    $start = strpos($location, '(');
387
                    $end = strpos($location, ')', $start + 1);
388
                    $length = $end - $start;
389
                    $result = substr($location, $start + 1, $length - 1);
390
                    $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]);
391
                }
392
            }
393
            //for display
394
            $timezones = array_column($display, 'name', 'id');
395
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state);
396
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country);
397
            $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray();
398
399
            return view(
400
                'themes.default1.front.clients.profile',
401
                compact('user', 'timezones', 'state', 'states', 'bussinesses')
402
            );
403
        } catch (Exception $ex) {
404
            Bugsnag::notifyException($ex);
405
406
            return redirect()->back()->with('fails', $ex->getMessage());
407
        }
408
    }
409
410
    public function getInvoice($id)
411
    {
412
        try {
413
            $invoice = $this->invoice->findOrFail($id);
414
            $items = $invoice->invoiceItem()->get();
415
            $user = \Auth::user();
416
417
            return view('themes.default1.front.clients.show-invoice', compact('invoice', 'items', 'user'));
418
        } catch (Exception $ex) {
419
            Bugsnag::notifyException($ex);
420
421
            return redirect()->back()->with('fails', $ex->getMessage());
422
        }
423
    }
424
425
    public function getOrder($id)
426
    {
427
        try {
428
            $order = $this->order->findOrFail($id);
429
            $invoice = $order->invoice()->first();
430
            $items = $order->invoice()->first()->invoiceItem()->get();
431
            $subscription = '';
432
            $plan = '';
433
            if ($order->subscription) {
434
                $subscription = $order->subscription;
435
436
                $plan = $subscription->plan()->first();
437
            }
438
            $product = $order->product()->first();
439
            $price = $product->price()->first();
440
            $licenseStatus = StatusSetting::pluck('license_status')->first();
441
            $user = \Auth::user();
442
443
            return view(
444
                'themes.default1.front.clients.show-order',
445
                compact('invoice', 'order', 'user', 'plan', 'product', 'subscription', 'licenseStatus')
446
            );
447
        } catch (Exception $ex) {
448
            Bugsnag::notifyException($ex);
449
450
            return redirect('/')->with('fails', $ex->getMessage());
451
        }
452
    }
453
454
    public function getPaymentByOrderId($orderid, $userid)
455
    {
456
        try {
457
            // dd($orderid);
458
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
459
            // dd($order);
460
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
461
            if (count($relation) > 0) {
462
                $invoices = $relation;
463
            } else {
464
                $invoices = $order->invoice()->pluck('id')->toArray();
465
            }
466
            $payments = $this->payment->whereIn('invoice_id', $invoices)
467
                    ->select('id', 'invoice_id', 'user_id', 'amount', 'payment_method', 'payment_status', 'created_at');
468
469
            return \DataTables::of($payments->get())
470
                            ->addColumn('checkbox', function ($model) {
471
                                if (\Input::get('client') != 'true') {
472
                                    return "<input type='checkbox' class='payment_checkbox' 
473
                                    value=".$model->id.' name=select[] id=check>';
474
                                }
475
                            })
476
                            ->addColumn('number', function ($model) {
477
                                return $model->invoice()->first()->number;
478
                            })
479
                            ->addColumn('amount', 'payment_method', 'payment_status', 'created_at')
480
                            ->addColumn('total', function ($model) {
481
                                return $model->grand_total;
482
                            })
483
                            ->rawColumns(['checkbox', 'number', 'total',
484
                             'payment_method', 'payment_status', 'created_at', ])
485
                            ->make(true);
486
        } catch (Exception $ex) {
487
            Bugsnag::notifyException($ex);
488
489
            return redirect()->back()->with('fails', $ex->getMessage());
490
        }
491
    }
492
493
    public function getPaymentByOrderIdClient($orderid, $userid)
494
    {
495
        try {
496
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
497
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
498
            if (count($relation) > 0) {
499
                $invoices = $relation;
500
            } else {
501
                $invoices = $order->invoice()->pluck('id')->toArray();
502
            }
503
            $payments = $this->payment->whereIn('invoice_id', $invoices)
504
                    ->select('id', 'invoice_id', 'user_id', 'payment_method', 'payment_status', 'created_at', 'amount');
505
            //dd(\Input::all());
506
            return \DataTables::of($payments->get())
507
                            ->addColumn('number', function ($model) {
508
                                return $model->invoice()->first()->number;
509
                            })
510
                              ->addColumn('total', function ($model) {
511
                                  return $model->amount;
512
                              })
513
                               ->addColumn('created_at', function ($model) {
514
                                   $date1 = new DateTime($model->created_at);
515
                                   $tz = \Auth::user()->timezone()->first()->name;
516
                                   $date1->setTimezone(new DateTimeZone($tz));
517
                                   $date = $date1->format('M j, Y, g:i a');
518
519
                                   return $date;
520
                               })
521
522
                            ->addColumn('payment_method', 'payment_status', 'created_at')
523
524
                            ->rawColumns(['number', 'total', 'payment_method', 'payment_status', 'created_at'])
525
                            ->make(true);
526
        } catch (Exception $ex) {
527
            Bugsnag::notifyException($ex);
528
529
            return redirect()->back()->with('fails', $ex->getMessage());
530
        }
531
    }
532
}
533