Completed
Push — development ( 0dc251...8c4906 )
by Bhanu
24:04 queued 12:13
created

ClientController::getGithubVersionList()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 51
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 51
rs 8.9848
c 0
b 0
f 0
cc 5
nc 6
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'>Pay Now</a>";
99
                                }
100
101
                                return '<p><a href='.url('my-invoice/'.$model->id).
102
                                " class='btn btn-primary btn-xs'>View</a>".$payment.'</p>';
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
134
            return \DataTables::of($versions)
135
                            ->addColumn('id', function ($versions) {
136
                                return ucfirst($versions->id);
137
                            })
138
                            ->addColumn('version', function ($versions) {
139
                                return ucfirst($versions->version);
140
                            })
141
                            ->addColumn('title', function ($versions) {
142
                                return ucfirst($versions->title);
143
                            })
144
                            ->addColumn('description', function ($versions) {
145
                                return ucfirst($versions->description);
146
                            })
147
                            ->addColumn('file', function ($versions) use ($clientid, $invoiceid, $productid) {
148
                                $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first();
149
                                $order = Order::where('invoice_id', '=', $invoice_id)->first();
150
                                $order_id = $order->id;
151
                                $endDate = Subscription::select('ends_at')
152
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
153
                                if ($versions->created_at->toDateTimeString()
154
                                    < $endDate->ends_at->toDateTimeString()) {
155
                                    return '<p><a href='.url('download/'.$productid.'/'
156
                                        .$clientid.'/'.$invoiceid.'/'.$versions->id).
157
                                " class='btn btn-sm btn-primary'><i class='fa fa-download'>
158
                                </i>&nbsp;&nbsp;Download</a>".'&nbsp;
159
160
                                   </p>';
161
                                } else {
162
                                    return '<button class="btn btn-primary 
163
                                    btn-sm disabled tooltip">Download <span class="tooltiptext">
164
                                    Please Renew!!</span></button>';
165
                                }
166
                            })
167
                            ->rawColumns(['version', 'title', 'description', 'file'])
168
                            ->make(true);
169
        } catch (Exception $ex) {
170
            Bugsnag::notifyException($ex);
171
            echo $ex->getMessage();
172
        }
173
    }
174
175
    /**
176
     * Get list of all the versions from Github.
177
     *
178
     * @param type $productid
179
     * @param type $clientid
180
     * @param type $invoiceid
181
     */
182
    public function getGithubVersionList($productid, $clientid, $invoiceid)
183
    {
184
        try {
185
            $products = $this->product::where('id', $productid)
186
            ->select('name', 'version', 'github_owner', 'github_repository')->get();
187
            $owner = '';
188
            $repo = '';
189
            foreach ($products as $product) {
190
                $owner = $product->github_owner;
191
                $repo = $product->github_repository;
192
            }
193
            $url = "https://api.github.com/repos/$owner/$repo/releases";
194
            $link = $this->github_api->getCurl1($url);
195
            $link = $link['body'];
196
            $link = (array_slice($link, 0, 10, true));
197
198
            return \DataTables::of($link)
199
                            ->addColumn('version', function ($link) {
200
                                return ucfirst($link['tag_name']);
201
                            })
202
                            ->addColumn('name', function ($link) {
203
                                return ucfirst($link['name']);
204
                            })
205
                            ->addColumn('description', function ($link) {
206
                                $markdown = Markdown::convertToHtml(ucfirst($link['body']));
207
208
                                return $markdown;
209
                            })
210
                            ->addColumn('file', function ($link) use ($invoiceid, $productid) {
211
                                $order = Order::where('invoice_id', '=', $invoiceid)->first();
212
                                $order_id = $order->id;
213
                                $orderEndDate = Subscription::select('ends_at')
214
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
215
                                if ($orderEndDate) {
216
                                    $actionButton = $this->getActionButton($link, $orderEndDate);
217
218
                                    return $actionButton;
219
                                } elseif (!$orderEndDate) {
220
                                    $link = $this->github_api->getCurl1($link['zipball_url']);
221
222
                                    return '<p><a href='.$link['header']['Location']
223
                                    ." class='btn btn-sm btn-primary'>Download  </a>"
224
                                            .'&nbsp;
225
                                   </p>';
226
                                }
227
                            })
228
                            ->rawColumns(['version', 'name', 'description', 'file'])
229
                            ->make(true);
230
        } catch (Exception $ex) {
231
            Bugsnag::notifyException($ex);
232
            echo $ex->getMessage();
233
        }
234
    }
235
236
    public function orders()
237
    {
238
        try {
239
            return view('themes.default1.front.clients.order1');
240
        } catch (Exception $ex) {
241
            Bugsnag::notifyException($ex);
242
243
            return redirect()->back()->with('fails', $ex->getMessage());
244
        }
245
    }
246
247
    /*
248
     * Show all the orders for User
249
     */
250
251
    public function getOrders()
252
    {
253
        try {
254
            $orders = Order::where('client', \Auth::user()->id);
255
256
            return \DataTables::of($orders->get())
257
                            ->addColumn('id', function ($model) {
258
                                return $model->id;
259
                            })
260
                            ->addColumn('product_name', function ($model) {
261
                                return $model->product()->first()->name;
262
                            })
263
                            ->addColumn('expiry', function ($model) {
264
                                $tz = \Auth::user()->timezone()->first()->name;
265
                                $end = $this->getExpiryDate($model);
266
267
                                return $end;
268
                            })
269
270
                            ->addColumn('Action', function ($model) {
271
                                $sub = $model->subscription()->first();
272
                                $order = Order::where('id', $model->id)->select('product')->first();
273
                                $productid = $order->product;
274
                                $order_cont = new \App\Http\Controllers\Order\OrderController();
275
                                $status = $order_cont->checkInvoiceStatusByOrderId($model->id);
276
                                $url = '';
277
                                if ($status == 'success') {
278
                                    if ($sub) {
279
                                        $url = $this->renewPopup($sub->id, $productid);
280
                                    }
281
                                }
282
283
                                $listUrl = $this->getPopup($model, $productid);
284
285
                                return '<a href='.url('my-order/'.$model->id)." 
286
                                class='btn  btn-primary btn-xs' style='margin-right:5px;'>
287
                                <i class='fa fa-eye' title='Details of order'></i> $listUrl $url </a>";
288
                            })
289
                            ->rawColumns(['id', 'created_at', 'ends_at', 'product', 'Action'])
290
                            ->make(true);
291
        } catch (Exception $ex) {
292
            Bugsnag::notifyException($ex);
293
            echo $ex->getMessage();
294
        }
295
    }
296
297
    public function subscriptions()
298
    {
299
        try {
300
            return view('themes.default1.front.clients.subscription');
301
        } catch (Exception $ex) {
302
            Bugsnag::notifyException($ex);
303
304
            return redirect()->back()->with('fails', $ex->getMessage());
305
        }
306
    }
307
308
    public function profile()
309
    {
310
        try {
311
            $user = $this->user->where('id', \Auth::user()->id)->first();
312
            //dd($user);
313
            $timezones = new \App\Model\Common\Timezone();
314
            $timezones = $timezones->pluck('name', 'id')->toArray();
315
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state);
316
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country);
317
            $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray();
318
319
            return view('themes.default1.front.clients.profile',
320
             compact('user', 'timezones', 'state', 'states', 'bussinesses'));
321
        } catch (Exception $ex) {
322
            Bugsnag::notifyException($ex);
323
324
            return redirect()->back()->with('fails', $ex->getMessage());
325
        }
326
    }
327
328
    public function getInvoice($id)
329
    {
330
        try {
331
            $invoice = $this->invoice->findOrFail($id);
332
            $items = $invoice->invoiceItem()->get();
333
            $user = \Auth::user();
334
335
            return view('themes.default1.front.clients.show-invoice', compact('invoice', 'items', 'user'));
336
        } catch (Exception $ex) {
337
            Bugsnag::notifyException($ex);
338
339
            return redirect()->back()->with('fails', $ex->getMessage());
340
        }
341
    }
342
343
    public function getOrder($id)
344
    {
345
        try {
346
            $order = $this->order->findOrFail($id);
347
            $invoice = $order->invoice()->first();
348
            $items = $order->invoice()->first()->invoiceItem()->get();
349
            $subscription = '';
350
            $plan = '';
351
            if ($order->subscription) {
352
                $subscription = $order->subscription;
353
354
                $plan = $subscription->plan()->first();
355
            }
356
            $product = $order->product()->first();
357
            $price = $product->price()->first();
358
            $user = \Auth::user();
359
360
            return view('themes.default1.front.clients.show-order',
361
                compact('invoice', 'order', 'user', 'plan', 'product', 'subscription'));
362
        } catch (Exception $ex) {
363
            Bugsnag::notifyException($ex);
364
365
            return redirect('/')->with('fails', $ex->getMessage());
366
        }
367
    }
368
369
    public function getPaymentByOrderId($orderid, $userid)
370
    {
371
        try {
372
            // dd($orderid);
373
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
374
            // dd($order);
375
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
376
            if (count($relation) > 0) {
377
                $invoices = $relation;
378
            } else {
379
                $invoices = $order->invoice()->pluck('id')->toArray();
380
            }
381
            $payments = $this->payment->whereIn('invoice_id', $invoices)
382
                    ->select('id', 'invoice_id', 'user_id', 'amount', 'payment_method', 'payment_status', 'created_at');
383
384
            return \DataTables::of($payments->get())
385
                            ->addColumn('checkbox', function ($model) {
386
                                if (\Input::get('client') != 'true') {
387
                                    return "<input type='checkbox' class='payment_checkbox' 
388
                                    value=".$model->id.' name=select[] id=check>';
389
                                }
390
                            })
391
                            ->addColumn('number', function ($model) {
392
                                return $model->invoice()->first()->number;
393
                            })
394
                            ->addColumn('amount', 'payment_method', 'payment_status', 'created_at')
395
                            ->addColumn('total', function ($model) {
396
                                return $model->grand_total;
397
                            })
398
                            ->rawColumns(['checkbox', 'number', 'total',
399
                             'payment_method', 'payment_status', 'created_at', ])
400
                            ->make(true);
401
        } catch (Exception $ex) {
402
            Bugsnag::notifyException($ex);
403
404
            return redirect()->back()->with('fails', $ex->getMessage());
405
        }
406
    }
407
408
    public function getPaymentByOrderIdClient($orderid, $userid)
409
    {
410
        try {
411
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
412
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
413
            if (count($relation) > 0) {
414
                $invoices = $relation;
415
            } else {
416
                $invoices = $order->invoice()->pluck('id')->toArray();
417
            }
418
            $payments = $this->payment->whereIn('invoice_id', $invoices)
419
                    ->select('id', 'invoice_id', 'user_id', 'payment_method', 'payment_status', 'created_at', 'amount');
420
            //dd(\Input::all());
421
            return \DataTables::of($payments->get())
422
                            ->addColumn('number', function ($model) {
423
                                return $model->invoice()->first()->number;
424
                            })
425
                              ->addColumn('total', function ($model) {
426
                                  return $model->amount;
427
                              })
428
                               ->addColumn('created_at', function ($model) {
429
                                   $date1 = new DateTime($model->created_at);
430
                                   $tz = \Auth::user()->timezone()->first()->name;
431
                                   $date1->setTimezone(new DateTimeZone($tz));
432
                                   $date = $date1->format('M j, Y, g:i a');
433
434
                                   return $date;
435
                               })
436
437
                            ->addColumn('payment_method', 'payment_status', 'created_at')
438
439
                            ->rawColumns(['number', 'total', 'payment_method', 'payment_status', 'created_at'])
440
                            ->make(true);
441
        } catch (Exception $ex) {
442
            Bugsnag::notifyException($ex);
443
444
            return redirect()->back()->with('fails', $ex->getMessage());
445
        }
446
    }
447
}
448