Test Setup Failed
Push — development ( ac5058...9fa3be )
by Ashutosh
11:12
created

ClientController::getGithubVersionList()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 49
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 49
rs 9.0168
c 0
b 0
f 0
cc 5
nc 6
nop 3
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Github\GithubApiController;
6
use App\Model\Common\Timezone;
7
use App\Model\Github\Github;
8
use App\Model\Order\Invoice;
9
use App\Model\Order\Order;
10
use App\Model\Order\Payment;
11
use App\Model\Product\Product;
12
use App\Model\Product\ProductUpload;
13
use App\Model\Product\Subscription;
14
use App\User;
15
use Auth;
16
use Bugsnag;
17
use DateTime;
18
use DateTimeZone;
19
use Exception;
20
use GrahamCampbell\Markdown\Facades\Markdown;
21
22
class ClientController extends BaseClientController
23
{
24
    public $user;
25
    public $invoice;
26
    public $order;
27
    public $subscription;
28
    public $payment;
29
30
    public function __construct()
31
    {
32
        $this->middleware('auth');
33
34
        $user = new User();
35
        $this->user = $user;
36
37
        $invoice = new Invoice();
38
        $this->invoice = $invoice;
39
40
        $order = new Order();
41
        $this->order = $order;
42
43
        $subscription = new Subscription();
44
        $this->subscription = $subscription;
45
46
        $payment = new Payment();
47
        $this->payment = $payment;
48
49
        $product_upload = new ProductUpload();
50
        $this->product_upload = $product_upload;
51
52
        $product = new Product();
53
        $this->product = $product;
54
55
        $github_controller = new GithubApiController();
56
        $this->github_api = $github_controller;
57
58
        $model = new Github();
59
        $this->github = $model->firstOrFail();
60
61
        $this->client_id = $this->github->client_id;
62
        $this->client_secret = $this->github->client_secret;
63
    }
64
65
    public function invoices()
66
    {
67
        try {
68
            return view('themes.default1.front.clients.invoice');
69
        } catch (Exception $ex) {
70
            return redirect()->back()->with('fails', $ex->getMessage());
71
        }
72
    }
73
74
    public function getInvoices()
75
    {
76
        try {
77
            $invoices = Invoice::where('user_id', \Auth::user()->id)
78
                    ->select('number', 'created_at', 'grand_total', 'id', 'status');
79
80
            return \DataTables::of($invoices->get())
81
                            ->addColumn('number', function ($model) {
82
                                return $model->number;
83
                            })
84
                            ->addColumn('date', function ($model) {
85
86
                                // $timezone = Timezone::where('id',Auth::user()->timezone_id)->pluck('location')->first();
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...
87
                                $date = $model->created_at;
88
89
                                return $date;
90
                                // $myobject->created_at->timezone($this->auth->user()->timezone);
91
                            })
92
                            // ->showColumns('created_at')
93
                            ->addColumn('total', function ($model) {
94
                                return $model->grand_total;
95
                            })
96
                            ->addColumn('Action', function ($model) {
97
                                $status = $model->status;
98
                                $payment = '';
99
                                if ($status == 'Pending' && $model->grand_total > 0) {
100
                                    $payment = '  <a href='.url('paynow/'.$model->id)." class='btn btn-primary btn-xs'>Pay Now</a>";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 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...
101
                                }
102
103
                                return '<p><a href='.url('my-invoice/'.$model->id)." class='btn btn-primary btn-xs'>View</a>".$payment.'</p>';
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...
104
                            })
105
                            ->rawColumns(['number', 'created_at', 'total', 'Action'])
106
                            // ->orderColumns('number', 'created_at', 'total')
107
                            ->make(true);
108
        } catch (Exception $ex) {
109
            Bugsnag::notifyException($ex);
110
            echo $ex->getMessage();
111
        }
112
    }
113
114
    /**
115
     * Get list of all the versions from Filesystem.
116
     *
117
     * @param type $productid
118
     * @param type $clientid
119
     * @param type $invoiceid
120
     *
121
     * Get list of all the versions from Filesystem.
122
     * @param type $productid
123
     * @param type $clientid
124
     * @param type $invoiceid
125
     *
126
     * @return type
127
     */
128
    public function getVersionList($productid, $clientid, $invoiceid)
129
    {
130
        try {
131
            $versions = ProductUpload::where('product_id', $productid)->select('id', 'product_id', 'version', 'title', 'description', 'file', 'created_at')->get();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 163 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...
132
133
            return \DataTables::of($versions)
134
                            ->addColumn('id', function ($versions) {
135
                                return ucfirst($versions->id);
136
                            })
137
                            ->addColumn('version', function ($versions) {
138
                                return ucfirst($versions->version);
139
                            })
140
                            ->addColumn('title', function ($versions) {
141
                                return ucfirst($versions->title);
142
                            })
143
                            ->addColumn('description', function ($versions) {
144
                                return ucfirst($versions->description);
145
                            })
146
                            ->addColumn('file', function ($versions) use ($clientid, $invoiceid, $productid) {
147
                                $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first();
148
                                $order = Order::where('invoice_id', '=', $invoice_id)->first();
149
                                $order_id = $order->id;
150
                                $endDate = Subscription::select('ends_at')->where('product_id', $productid)->where('order_id', $order_id)->first();
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...
151
                                if ($versions->created_at->toDateTimeString() < $endDate->ends_at->toDateTimeString()) {
152
                                    return '<p><a href='.url('download/'.$productid.'/'.$clientid.'/'.$invoiceid.'/'.$versions->id)." class='btn btn-sm btn-primary'><i class='fa fa-download'></i>&nbsp;&nbsp;Download</a>"
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 220 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...
153
                                            .'&nbsp;
154
155
                                   </p>';
156
                                } else {
157
                                    return '<button class="btn btn-primary btn-sm disabled tooltip">Download <span class="tooltiptext">Please Renew!!</span></button>';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 167 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...
158
                                }
159
                            })
160
                            ->rawColumns(['version', 'title', 'description', 'file'])
161
                            ->make(true);
162
        } catch (Exception $ex) {
163
            Bugsnag::notifyException($ex);
164
            echo $ex->getMessage();
165
        }
166
    }
167
168
    /**
169
     * Get list of all the versions from Github.
170
     *
171
     * @param type $productid
172
     * @param type $clientid
173
     * @param type $invoiceid
174
     */
175
    public function getGithubVersionList($productid, $clientid, $invoiceid)
176
    {
177
        try {
178
            $products = $this->product::where('id', $productid)->select('name', 'version', 'github_owner', 'github_repository')->get();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
179
            $owner = '';
180
            $repo = '';
181
            foreach ($products as $product) {
182
                $owner = $product->github_owner;
183
                $repo = $product->github_repository;
184
            }
185
            $url = "https://api.github.com/repos/$owner/$repo/releases";
186
            $link = $this->github_api->getCurl1($url);
187
            $link = $link['body'];
188
            $link = (array_slice($link, 0, 10, true));
189
190
            return \DataTables::of($link)
191
                            ->addColumn('version', function ($link) {
192
                                return ucfirst($link['tag_name']);
193
                            })
194
                            ->addColumn('name', function ($link) {
195
                                return ucfirst($link['name']);
196
                            })
197
                            ->addColumn('description', function ($link) {
198
                                $markdown = Markdown::convertToHtml(ucfirst($link['body']));
199
200
                                return $markdown;
201
                            })
202
                            ->addColumn('file', function ($link) use ($invoiceid, $productid) {
203
                                $order = Order::where('invoice_id', '=', $invoiceid)->first();
204
                                $order_id = $order->id;
205
                                $orderEndDate = Subscription::select('ends_at')->where('product_id', $productid)->where('order_id', $order_id)->first();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 152 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...
206
                                if ($orderEndDate) {
207
                                    $actionButton = $this->getActionButton($link, $orderEndDate);
208
209
                                    return $actionButton;
210
                                } elseif (!$orderEndDate) {
211
                                    $link = $this->github_api->getCurl1($link['zipball_url']);
212
213
                                    return '<p><a href='.$link['header']['Location']." class='btn btn-sm btn-primary'>Download  </a>"
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 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...
214
                                            .'&nbsp;
215
216
                                   </p>';
217
                                }
218
                            })
219
                            ->rawColumns(['version', 'name', 'description', 'file'])
220
                            ->make(true);
221
        } catch (Exception $ex) {
222
            Bugsnag::notifyException($ex);
223
            echo $ex->getMessage();
224
        }
225
    }
226
227
    public function orders()
228
    {
229
        try {
230
            return view('themes.default1.front.clients.order1');
231
        } catch (Exception $ex) {
232
            Bugsnag::notifyException($ex);
233
234
            return redirect()->back()->with('fails', $ex->getMessage());
235
        }
236
    }
237
238
    /*
239
     * Show all the orders for User
240
     */
241
242
    public function getOrders()
243
    {
244
        try {
245
            $orders = Order:: where('client', \Auth::user()->id);
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after double colon; 1 found

This check looks for references to static members where there are spaces between the name of the type and the actual member.

An example:

Certificate:: TRIPLEDES_CBC

will actually work, but is not very readable.

Loading history...
246
247
            return \DataTables::of($orders->get())
248
                            ->addColumn('id', function ($model) {
249
                                return $model->id;
250
                            })
251
                            ->addColumn('product_name', function ($model) {
252
                                return $model->product()->first()->name;
253
                            })
254
                            ->addColumn('expiry', function ($model) {
255
                                $tz = \Auth::user()->timezone()->first()->name;
256
                                $end = $this->getExpiryDate($model);
257
258
                                return $end;
259
                            })
260
261
                            ->addColumn('Action', function ($model) {
262
                                $sub = $model->subscription()->first();
263
                                $order = Order::where('id', $model->id)->select('product')->first();
264
                                $productid = $order->product;
265
                                $order_cont = new \App\Http\Controllers\Order\OrderController();
266
                                $status = $order_cont->checkInvoiceStatusByOrderId($model->id);
267
                                $url = '';
268
                                if ($status == 'success') {
269
                                    if ($sub) {
270
                                        $url = $this->renewPopup($sub->id, $productid);
271
                                    }
272
                                }
273
274
                                $listUrl = $this->getPopup($model, $productid);
275
276
                                return '<a href='.url('my-order/'.$model->id)." class='btn  btn-primary btn-xs' style='margin-right:5px;'><i class='fa fa-eye' title='Details of order'></i> $listUrl $url </a>";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 209 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...
277
                            })
278
                            ->rawColumns(['id', 'created_at', 'ends_at', 'product', 'Action'])
279
                            ->make(true);
280
        } catch (Exception $ex) {
281
            Bugsnag::notifyException($ex);
282
            echo $ex->getMessage();
283
        }
284
    }
285
286
    public function subscriptions()
287
    {
288
        try {
289
            return view('themes.default1.front.clients.subscription');
290
        } catch (Exception $ex) {
291
            Bugsnag::notifyException($ex);
292
293
            return redirect()->back()->with('fails', $ex->getMessage());
294
        }
295
    }
296
297
    public function profile()
298
    {
299
        try {
300
            $user = $this->user->where('id', \Auth::user()->id)->first();
301
            //dd($user);
302
            $timezones = new \App\Model\Common\Timezone();
303
            $timezones = $timezones->pluck('name', 'id')->toArray();
304
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state);
305
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country);
306
            $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray();
307
308
            return view('themes.default1.front.clients.profile', compact('user', 'timezones', 'state', 'states', 'bussinesses'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 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...
309
        } catch (Exception $ex) {
310
            Bugsnag::notifyException($ex);
311
312
            return redirect()->back()->with('fails', $ex->getMessage());
313
        }
314
    }
315
316
    public function getInvoice($id)
317
    {
318
        try {
319
            $invoice = $this->invoice->findOrFail($id);
320
            $items = $invoice->invoiceItem()->get();
321
            $user = \Auth::user();
322
323
            return view('themes.default1.front.clients.show-invoice', compact('invoice', 'items', 'user'));
324
        } catch (Exception $ex) {
325
            Bugsnag::notifyException($ex);
326
327
            return redirect()->back()->with('fails', $ex->getMessage());
328
        }
329
    }
330
331
    public function getOrder($id)
332
    {
333
        try {
334
            $order = $this->order->findOrFail($id);
335
            $invoice = $order->invoice()->first();
336
            $items = $order->invoice()->first()->invoiceItem()->get();
337
            $subscription = '';
338
            $plan = '';
339
            if ($order->subscription) {
340
                $subscription = $order->subscription;
341
342
                $plan = $subscription->plan()->first();
343
            }
344
            $product = $order->product()->first();
345
            $price = $product->price()->first();
346
            $user = \Auth::user();
347
348
            return view('themes.default1.front.clients.show-order', compact('invoice', 'order', 'user', 'plan', 'product', 'subscription'));
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...
349
        } catch (Exception $ex) {
350
            Bugsnag::notifyException($ex);
351
352
            return redirect('/')->with('fails', $ex->getMessage());
353
        }
354
    }
355
356
    public function getPaymentByOrderId($orderid, $userid)
357
    {
358
        try {
359
            // dd($orderid);
360
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
361
            // dd($order);
362
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
363
            if (count($relation) > 0) {
364
                $invoices = $relation;
365
            } else {
366
                $invoices = $order->invoice()->pluck('id')->toArray();
367
            }
368
            $payments = $this->payment->whereIn('invoice_id', $invoices)
369
                    ->select('id', 'invoice_id', 'user_id', 'amount', 'payment_method', 'payment_status', 'created_at');
370
371
            return \DataTables::of($payments->get())
372
                            ->addColumn('checkbox', function ($model) {
373
                                if (\Input::get('client') != 'true') {
374
                                    return "<input type='checkbox' class='payment_checkbox' value=".$model->id.' name=select[] id=check>';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
375
                                }
376
                            })
377
                            ->addColumn('number', function ($model) {
378
                                return $model->invoice()->first()->number;
379
                            })
380
                            ->addColumn('amount', 'payment_method', 'payment_status', 'created_at')
381
                            ->addColumn('total', function ($model) {
382
                                return $model->grand_total;
383
                            })
384
                            ->rawColumns(['checkbox', 'number', 'total', 'payment_method', 'payment_status', 'created_at'])
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...
385
                            ->make(true);
386
        } catch (Exception $ex) {
387
            Bugsnag::notifyException($ex);
388
389
            return redirect()->back()->with('fails', $ex->getMessage());
390
        }
391
    }
392
393
    public function getPaymentByOrderIdClient($orderid, $userid)
394
    {
395
        try {
396
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
397
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
398
            if (count($relation) > 0) {
399
                $invoices = $relation;
400
            } else {
401
                $invoices = $order->invoice()->pluck('id')->toArray();
402
            }
403
            $payments = $this->payment->whereIn('invoice_id', $invoices)
404
                    ->select('id', 'invoice_id', 'user_id', 'payment_method', 'payment_status', 'created_at', 'amount');
405
            //dd(\Input::all());
406
            return \DataTables::of($payments->get())
407
                            ->addColumn('number', function ($model) {
408
                                return $model->invoice()->first()->number;
409
                            })
410
                              ->addColumn('total', function ($model) {
411
                                  return $model->amount;
412
                              })
413
                               ->addColumn('created_at', function ($model) {
414
                                   $date1 = new DateTime($model->created_at);
415
                                   $tz = \Auth::user()->timezone()->first()->name;
416
                                   $date1->setTimezone(new DateTimeZone($tz));
417
                                   $date = $date1->format('M j, Y, g:i a');
418
419
                                   return $date;
420
                               })
421
422
                            ->addColumn('payment_method', 'payment_status', 'created_at')
423
424
                            ->rawColumns(['number', 'total', 'payment_method', 'payment_status', 'created_at'])
425
                            ->make(true);
426
        } catch (Exception $ex) {
427
            Bugsnag::notifyException($ex);
428
429
            return redirect()->back()->with('fails', $ex->getMessage());
430
        }
431
    }
432
}
433