Completed
Push — development ( c78083...b93250 )
by Ashutosh
11:07 queued 10s
created

ClientController::getVersionList()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 65
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 65
rs 8.2986
c 0
b 0
f 0
cc 7
nc 10
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Github\GithubApiController;
6
use App\Model\Github\Github;
7
use App\Model\Order\Invoice;
8
use App\Model\Order\Order;
9
use App\Model\Order\Payment;
10
use App\Model\Product\Product;
11
use App\Model\Product\ProductUpload;
12
use App\Model\Product\Subscription;
13
use App\User;
14
use Auth;
15
use Bugsnag;
16
use DateTime;
17
use DateTimeZone;
18
use Exception;
19
use GrahamCampbell\Markdown\Facades\Markdown;
20
21
class ClientController extends BaseClientController
22
{
23
    public $user;
24
    public $invoice;
25
    public $order;
26
    public $subscription;
27
    public $payment;
28
29
    public function __construct()
30
    {
31
        $this->middleware('auth');
32
33
        $user = new User();
34
        $this->user = $user;
35
36
        $invoice = new Invoice();
37
        $this->invoice = $invoice;
38
39
        $order = new Order();
40
        $this->order = $order;
41
42
        $subscription = new Subscription();
43
        $this->subscription = $subscription;
44
45
        $payment = new Payment();
46
        $this->payment = $payment;
47
48
        $product_upload = new ProductUpload();
49
        $this->product_upload = $product_upload;
50
51
        $product = new Product();
52
        $this->product = $product;
53
54
        $github_controller = new GithubApiController();
55
        $this->github_api = $github_controller;
56
57
        $model = new Github();
58
        $this->github = $model->firstOrFail();
59
60
        $this->client_id = $this->github->client_id;
61
        $this->client_secret = $this->github->client_secret;
62
    }
63
64
    public function invoices()
65
    {
66
        try {
67
            return view('themes.default1.front.clients.invoice');
68
        } catch (Exception $ex) {
69
            return redirect()->back()->with('fails', $ex->getMessage());
70
        }
71
    }
72
73
    public function getInvoices()
74
    {
75
        try {
76
            $invoices = Invoice::where('user_id', \Auth::user()->id)
77
                    ->select('number', 'created_at', 'grand_total', 'id', 'status');
78
79
            return \DataTables::of($invoices->get())
80
                            ->addColumn('number', function ($model) {
81
                                return $model->number;
82
                            })
83
                            ->addColumn('date', function ($model) {
84
                                $date = $model->created_at;
85
86
                                return $date;
87
                                // $myobject->created_at->timezone($this->auth->user()->timezone);
88
                            })
89
                            // ->showColumns('created_at')
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('id', 'product_id', 'version',
132
             'title', 'description', 'file', 'created_at')->get();
133
            $countVersions = count($versions);
134
            $countExpiry = 0;
135
             $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first();
136
             $order = Order::where('invoice_id', '=', $invoice_id)->first();
137
138
             $order_id = $order->id;
139
             $endDate = Subscription::select('ends_at')
140
                 ->where('product_id', $productid)->where('order_id', $order_id)->first();
141
142
            foreach ($versions as $version) {
143
               if ($version->created_at->toDateTimeString()
144
               < $endDate->ends_at->toDateTimeString())
145
               {
146
                   $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...
147
               }
148
            }
149
150
            return \DataTables::of($versions)
151
                            ->addColumn('id', function ($versions) {
152
                                return ucfirst($versions->id);
153
                            })
154
                            ->addColumn('version', function ($versions) {
155
                                return ucfirst($versions->version);
156
                            })
157
                            ->addColumn('title', function ($versions) {
158
                                return ucfirst($versions->title);
159
                            })
160
                            ->addColumn('description', function ($versions) {
161
                                return ucfirst($versions->description);
162
                            })
163
                            ->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 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...
164
                                $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first();
165
                                $order = Order::where('invoice_id', '=', $invoice_id)->first();
166
                                $order_id = $order->id;
167
                                $getDownloadCondition = Product::where('id', $productid)->value('deny_after_subscription');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 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...
168
                                $endDate = Subscription::select('ends_at')
169
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
170
171
                                //if product has expiry date ie sunscriptioon is generated
172
                                if ($endDate) {
173
                                    if ($getDownloadCondition == 1) {//Perpetual download till expiry selected
174
                                        $getDownload = $this->whenDownloadTillExpiry($endDate, $productid, $versions, $clientid, $invoiceid);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 141 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
176
                                        return $getDownload;
177
178
                         
179
                                  }elseif($getDownloadCondition == 0){//When download retires after subscription
180
                                          
181
                                        $getDownload =  $this->whenDownloadExpiresAfterExpiry($countExpiry, $countVersions,$endDate,$productid,$versions,$clientid,$invoiceid);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 175 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
                                    }
185
                                }
186
                            })
187
                            ->rawColumns(['version', 'title', 'description', 'file'])
188
                            ->make(true);
189
        } catch (Exception $ex) {
190
            Bugsnag::notifyException($ex);
191
            echo $ex->getMessage();
192
        }
193
    }
194
195
    public function whenDownloadTillExpiry($endDate, $productid, $versions, $clientid, $invoiceid)
196
    {
197
        if ($versions->created_at->toDateTimeString()
198
199
        < $endDate->ends_at->toDateTimeString()) {
200
            return '<p><a href='.url('download/'.$productid.'/'
201
            .$clientid.'/'.$invoiceid.'/'.$versions->id).
202
            " class='btn btn-sm btn-primary'><i class='fa fa-download'>
203
            </i>&nbsp;&nbsp;Download</a>".'&nbsp;
204
205
       </p>';
206
        } else {
207
            return '<button class="btn btn-primary 
208
        btn-sm disabled tooltip">Download <span class="tooltiptext">
209
        Please Renew!!</span></button>';
210
        }
211
    }
212
213
214
    public function whenDownloadExpiresAfterExpiry($countExpiry, $countVersions,$endDate,$productid,$versions,$clientid,$invoiceid)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 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...
215
    {
216
        if ($countExpiry == $countVersions) {
217
            return '<p><a href='.url('download/'.$productid.'/'
218
            .$clientid.'/'.$invoiceid.'/'.$versions->id).
219
            " class='btn btn-sm btn-primary'><i class='fa fa-download'>
220
            </i>&nbsp;&nbsp;Download</a>".'&nbsp;
221
222
       </p>';
223
        } else {
224
            return '<button class="btn btn-primary 
225
        btn-sm disabled tooltip">Download <span class="tooltiptext">
226
        Please Renew!!</span></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('ends_at')
257
                        ->where('product_id', $productid)->where('order_id', $order_id)->first();
258
             foreach ($link as $lin) {
259
              if(strtotime($lin['created_at']) < strtotime($orderEndDate->ends_at))
260
               {
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
            return \DataTables::of($link)
266
                            ->addColumn('version', function ($link) {
267
                                return ucfirst($link['tag_name']);
268
                            })
269
                            ->addColumn('name', function ($link) {
270
                                return ucfirst($link['name']);
271
                            })
272
                            ->addColumn('description', function ($link) {
273
                                $markdown = Markdown::convertToHtml(ucfirst($link['body']));
274
275
                                return $markdown;
276
                            })
277
                            ->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 124 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...
278
                                $order = Order::where('invoice_id', '=', $invoiceid)->first();
279
                                $order_id = $order->id;
280
                                $orderEndDate = Subscription::select('ends_at')
281
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
282
                                if ($orderEndDate) {
283
                                    $actionButton = $this->getActionButton($countExpiry,$countVersions,$link, $orderEndDate, $productid);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $actionButton is correct as $this->getActionButton($...derEndDate, $productid) targeting App\Http\Controllers\Fro...ller::getActionButton() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

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