Completed
Push — development ( c2ff46...95cb59 )
by Ashutosh
28:29 queued 18:30
created

ClientController::getGithubVersionList()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 64
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 64
rs 7.5571
c 0
b 0
f 0
cc 9
nc 13
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\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
200
    /**
201
     * Get list of all the versions from Github.
202
     *
203
     * @param type $productid
204
     * @param type $clientid
205
     * @param type $invoiceid
206
     */
207
    public function getGithubVersionList($productid, $clientid, $invoiceid)
208
    {
209
        try {
210
            $products = $this->product::where('id', $productid)
211
            ->select('name', 'version', 'github_owner', 'github_repository')->get();
212
            $owner = '';
213
            $repo = '';
214
            foreach ($products as $product) {
215
                $owner = $product->github_owner;
216
                $repo = $product->github_repository;
217
            }
218
            $url = "https://api.github.com/repos/$owner/$repo/releases";
219
            $countExpiry = 0;
220
            $link = $this->github_api->getCurl1($url);
221
            $link = $link['body'];
222
            $countVersions = 10; //because we are taking oly the first 10 versions
223
            $link = (array_slice($link, 0, 10, true));
224
            $order = Order::where('invoice_id', '=', $invoiceid)->first();
225
            $order_id = $order->id;
226
            $orderEndDate = Subscription::select('update_ends_at')
227
                        ->where('product_id', $productid)->where('order_id', $order_id)->first();
228
            if ($orderEndDate) {
229
                foreach ($link as $lin) {
230
                    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 157 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...
231
                        $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...
232
                    }
233
                }
234
            }
235
236
            return \DataTables::of($link)
237
                            ->addColumn('version', function ($link) {
238
                                return ucfirst($link['tag_name']);
239
                            })
240
                            ->addColumn('name', function ($link) {
241
                                return ucfirst($link['name']);
242
                            })
243
                            ->addColumn('description', function ($link) {
244
                                $markdown = Markdown::convertToHtml(ucfirst($link['body']));
245
246
                                return $markdown;
247
                            })
248
                            ->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...
249
                                $order = Order::where('invoice_id', '=', $invoiceid)->first();
250
                                $order_id = $order->id;
251
                                $orderEndDate = Subscription::select('update_ends_at')
252
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
253
                                if ($orderEndDate) {
254
                                    $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...
255
256
                                    return $actionButton;
257
                                } elseif (!$orderEndDate) {
258
                                    $link = $this->github_api->getCurl1($link['zipball_url']);
259
260
                                    return '<p><a href='.$link['header']['Location']
261
                                    ." class='btn btn-sm btn-primary'>Download  </a>"
262
                                            .'&nbsp;
263
                                   </p>';
264
                                }
265
                            })
266
                            ->rawColumns(['version', 'name', 'description', 'file'])
267
                            ->make(true);
268
        } catch (Exception $ex) {
269
            Bugsnag::notifyException($ex);
270
            echo $ex->getMessage();
271
        }
272
    }
273
274
275
276
    /*
277
     * Show all the orders for User
278
     */
279
280
    public function getOrders()
281
    {
282
        try {
283
            $orders = Order::where('client', \Auth::user()->id);
284
285
            return \DataTables::of($orders->get())
286
                            ->addColumn('id', function ($model) {
287
                                return $model->id;
288
                            })
289
                            ->addColumn('product_name', function ($model) {
290
                                return $model->product()->first()->name;
291
                            })
292
                            ->addColumn('expiry', function ($model) {
293
                                $tz = \Auth::user()->timezone()->first()->name;
294
                                $end = $this->getExpiryDate($model);
295
296
                                return $end;
297
                            })
298
299
                            ->addColumn('Action', function ($model) {
300
                                $sub = $model->subscription()->first();
301
                                $order = Order::where('id', $model->id)->select('product')->first();
302
                                $productid = $order->product;
303
                                $order_cont = new \App\Http\Controllers\Order\OrderController();
304
                                $status = $order_cont->checkInvoiceStatusByOrderId($model->id);
305
                                $url = '';
306
                                if ($status == 'success') {
307
                                    if ($sub) {
308
                                        $url = $this->renewPopup($sub->id, $productid);
309
                                    }
310
                                }
311
312
                                $listUrl = $this->getPopup($model, $productid);
313
314
                                return '<a href='.url('my-order/'.$model->id)." 
315
                                class='btn  btn-primary btn-xs' style='margin-right:5px;'>
316
                                <i class='fa fa-eye' title='Details of order'></i>&nbsp;View $listUrl $url </a>";
317
                            })
318
                            ->rawColumns(['id', 'created_at', 'ends_at', 'product', 'Action'])
319
                            ->make(true);
320
        } catch (Exception $ex) {
321
            app('log')->error($ex->getMessage());
322
            Bugsnag::notifyException($ex);
323
            echo $ex->getMessage();
324
        }
325
    }
326
327
328
329
    public function profile()
330
    {
331
        try {
332
            $user = $this->user->where('id', \Auth::user()->id)->first();
333
            //dd($user);
334
            $timezonesList = \App\Model\Common\Timezone::get();
335
            foreach ($timezonesList as $timezone) {
336
                $location = $timezone->location;
337
                if ($location) {
338
                    $start = strpos($location, '(');
339
                    $end = strpos($location, ')', $start + 1);
340
                    $length = $end - $start;
341
                    $result = substr($location, $start + 1, $length - 1);
342
                    $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]);
343
                }
344
            }
345
            //for display
346
            $timezones = array_column($display, 'name', 'id');
347
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state);
348
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country);
349
            $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray();
350
351
            return view(
352
                'themes.default1.front.clients.profile',
353
                compact('user', 'timezones', 'state', 'states', 'bussinesses')
354
            );
355
        } catch (Exception $ex) {
356
            Bugsnag::notifyException($ex);
357
358
            return redirect()->back()->with('fails', $ex->getMessage());
359
        }
360
    }
361
362
363
    public function getOrder($id)
364
    {
365
        try {
366
            $order = $this->order->findOrFail($id);
367
            $invoice = $order->invoice()->first();
368
            $items = $order->invoice()->first()->invoiceItem()->get();
369
            $subscription = '';
370
            $plan = '';
371
            if ($order->subscription) {
372
                $subscription = $order->subscription;
373
374
                $plan = $subscription->plan()->first();
375
            }
376
            $product = $order->product()->first();
377
            $price = $product->price()->first();
378
            $licenseStatus = StatusSetting::pluck('license_status')->first();
379
            $user = \Auth::user();
380
381
            return view(
382
                'themes.default1.front.clients.show-order',
383
                compact('invoice', 'order', 'user', 'plan', 'product', 'subscription', 'licenseStatus')
384
            );
385
        } catch (Exception $ex) {
386
            Bugsnag::notifyException($ex);
387
388
            return redirect('/')->with('fails', $ex->getMessage());
389
        }
390
    }
391
392
    public function getPaymentByOrderId($orderid, $userid)
393
    {
394
        try {
395
            // dd($orderid);
396
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
397
            // dd($order);
398
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
399
            if (count($relation) > 0) {
400
                $invoices = $relation;
401
            } else {
402
                $invoices = $order->invoice()->pluck('id')->toArray();
403
            }
404
            $payments = $this->payment->whereIn('invoice_id', $invoices)
405
                    ->select('id', 'invoice_id', 'user_id', 'amount', 'payment_method', 'payment_status', 'created_at');
406
407
            return \DataTables::of($payments->get())
408
                            ->addColumn('checkbox', function ($model) {
409
                                if (\Input::get('client') != 'true') {
410
                                    return "<input type='checkbox' class='payment_checkbox' 
411
                                    value=".$model->id.' name=select[] id=check>';
412
                                }
413
                            })
414
                            ->addColumn('number', function ($model) {
415
                                return $model->invoice()->first()->number;
416
                            })
417
                            ->addColumn('amount', 'payment_method', 'payment_status', 'created_at')
418
                            ->addColumn('total', function ($model) {
419
                                return $model->grand_total;
420
                            })
421
                            ->rawColumns(['checkbox', 'number', 'total',
422
                             'payment_method', 'payment_status', 'created_at', ])
423
                            ->make(true);
424
        } catch (Exception $ex) {
425
            Bugsnag::notifyException($ex);
426
427
            return redirect()->back()->with('fails', $ex->getMessage());
428
        }
429
    }
430
431
    public function getPaymentByOrderIdClient($orderid, $userid)
432
    {
433
        try {
434
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
435
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
436
            if (count($relation) > 0) {
437
                $invoices = $relation;
438
            } else {
439
                $invoices = $order->invoice()->pluck('id')->toArray();
440
            }
441
            $payments = $this->payment->whereIn('invoice_id', $invoices)
442
                    ->select('id', 'invoice_id', 'user_id', 'payment_method', 'payment_status', 'created_at', 'amount');
443
            //dd(\Input::all());
444
            return \DataTables::of($payments->get())
445
                            ->addColumn('number', function ($model) {
446
                                return $model->invoice()->first()->number;
447
                            })
448
                              ->addColumn('total', function ($model) {
449
                                  return $model->amount;
450
                              })
451
                               ->addColumn('created_at', function ($model) {
452
                                   $date1 = new DateTime($model->created_at);
453
                                   $tz = \Auth::user()->timezone()->first()->name;
454
                                   $date1->setTimezone(new DateTimeZone($tz));
455
                                   $date = $date1->format('M j, Y, g:i a');
456
457
                                   return $date;
458
                               })
459
460
                            ->addColumn('payment_method', 'payment_status', 'created_at')
461
462
                            ->rawColumns(['number', 'total', 'payment_method', 'payment_status', 'created_at'])
463
                            ->make(true);
464
        } catch (Exception $ex) {
465
            Bugsnag::notifyException($ex);
466
467
            return redirect()->back()->with('fails', $ex->getMessage());
468
        }
469
    }
470
}
471