| Total Complexity | 53 |
| Total Lines | 507 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ClientController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ClientController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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> Pay Now</a>"; |
||
|
|
|||
| 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> 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( |
||
| 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; |
||
| 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) { |
||
| 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); |
||
| 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 |
||
| 181 | $getDownload = $this->whenDownloadTillExpiry($updateEndDate, $productid, $versions, $clientid, $invoiceid); |
||
| 182 | |||
| 183 | return $getDownload; |
||
| 184 | } elseif ($downloadPermission['allowDownloadTillExpiry'] == 0) {//When download retires after subscription |
||
| 185 | $getDownload = $this->whenDownloadExpiresAfterExpiry($countExpiry, $countVersions, $updateEndDate, $productid, $versions, $clientid, $invoiceid); |
||
| 186 | |||
| 187 | return $getDownload; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | }) |
||
| 191 | ->rawColumns(['version', 'title', 'description', 'file']) |
||
| 192 | ->make(true); |
||
| 193 | } catch (Exception $ex) { |
||
| 194 | Bugsnag::notifyException($ex); |
||
| 195 | echo $ex->getMessage(); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | public function whenDownloadTillExpiry($updateEndDate, $productid, $versions, $clientid, $invoiceid) |
||
| 200 | { |
||
| 201 | if ($versions->created_at->toDateTimeString() |
||
| 202 | < $updateEndDate->update_ends_at) { |
||
| 203 | return '<p><a href='.url('download/'.$productid.'/' |
||
| 204 | .$clientid.'/'.$invoiceid.'/'.$versions->id). |
||
| 205 | " class='btn btn-sm btn-primary'><i class='fa fa-download'> |
||
| 206 | </i> Download</a>".' |
||
| 207 | |||
| 208 | </p>'; |
||
| 209 | } else { |
||
| 210 | return '<button class="btn btn-danger |
||
| 211 | btn-sm disabled">Please Renew </button>'; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | public function whenDownloadExpiresAfterExpiry($countExpiry, $countVersions, $updatesEndDate, $productid, $versions, $clientid, $invoiceid) |
||
| 216 | { |
||
| 217 | if ($countExpiry == $countVersions) { |
||
| 218 | return '<p><a href='.url('download/'.$productid.'/' |
||
| 219 | .$clientid.'/'.$invoiceid.'/'.$versions->id). |
||
| 220 | " class='btn btn-sm btn-primary'><i class='fa fa-download'> |
||
| 221 | </i> Download</a>".' |
||
| 222 | |||
| 223 | </p>'; |
||
| 224 | } else { |
||
| 225 | return '<button class="btn btn-danger |
||
| 226 | btn-sm disabled">Please Renew </button>'; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get list of all the versions from Github. |
||
| 232 | * |
||
| 233 | * @param type $productid |
||
| 234 | * @param type $clientid |
||
| 235 | * @param type $invoiceid |
||
| 236 | */ |
||
| 237 | public function getGithubVersionList($productid, $clientid, $invoiceid) |
||
| 238 | { |
||
| 239 | try { |
||
| 240 | $products = $this->product::where('id', $productid) |
||
| 241 | ->select('name', 'version', 'github_owner', 'github_repository')->get(); |
||
| 242 | $owner = ''; |
||
| 243 | $repo = ''; |
||
| 244 | foreach ($products as $product) { |
||
| 245 | $owner = $product->github_owner; |
||
| 246 | $repo = $product->github_repository; |
||
| 247 | } |
||
| 248 | $url = "https://api.github.com/repos/$owner/$repo/releases"; |
||
| 249 | $countExpiry = 0; |
||
| 250 | $link = $this->github_api->getCurl1($url); |
||
| 251 | $link = $link['body']; |
||
| 252 | $countVersions = 10; //because we are taking oly the first 10 versions |
||
| 253 | $link = (array_slice($link, 0, 10, true)); |
||
| 254 | $order = Order::where('invoice_id', '=', $invoiceid)->first(); |
||
| 255 | $order_id = $order->id; |
||
| 256 | $orderEndDate = Subscription::select('update_ends_at') |
||
| 257 | ->where('product_id', $productid)->where('order_id', $order_id)->first(); |
||
| 258 | if ($orderEndDate) { |
||
| 259 | foreach ($link as $lin) { |
||
| 260 | if (strtotime($lin['created_at']) < strtotime($orderEndDate->update_ends_at) || $orderEndDate->update_ends_at =='0000-00-00 00:00:00') { |
||
| 261 | $countExpiry = $countExpiry + 1; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | return \DataTables::of($link) |
||
| 267 | ->addColumn('version', function ($link) { |
||
| 268 | return ucfirst($link['tag_name']); |
||
| 269 | }) |
||
| 270 | ->addColumn('name', function ($link) { |
||
| 271 | return ucfirst($link['name']); |
||
| 272 | }) |
||
| 273 | ->addColumn('description', function ($link) { |
||
| 274 | $markdown = Markdown::convertToHtml(ucfirst($link['body'])); |
||
| 275 | |||
| 276 | return $markdown; |
||
| 277 | }) |
||
| 278 | ->addColumn('file', function ($link) use ($countExpiry, $countVersions, $invoiceid, $productid) { |
||
| 279 | $order = Order::where('invoice_id', '=', $invoiceid)->first(); |
||
| 280 | $order_id = $order->id; |
||
| 281 | $orderEndDate = Subscription::select('update_ends_at') |
||
| 282 | ->where('product_id', $productid)->where('order_id', $order_id)->first(); |
||
| 283 | if ($orderEndDate) { |
||
| 284 | $actionButton = $this->getActionButton($countExpiry, $countVersions, $link, $orderEndDate, $productid); |
||
| 285 | |||
| 286 | return $actionButton; |
||
| 287 | } elseif (!$orderEndDate) { |
||
| 288 | $link = $this->github_api->getCurl1($link['zipball_url']); |
||
| 289 | |||
| 290 | return '<p><a href='.$link['header']['Location'] |
||
| 291 | ." class='btn btn-sm btn-primary'>Download </a>" |
||
| 292 | .' |
||
| 293 | </p>'; |
||
| 294 | } |
||
| 295 | }) |
||
| 296 | ->rawColumns(['version', 'name', 'description', 'file']) |
||
| 297 | ->make(true); |
||
| 298 | } catch (Exception $ex) { |
||
| 299 | Bugsnag::notifyException($ex); |
||
| 300 | echo $ex->getMessage(); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | public function orders() |
||
| 305 | { |
||
| 306 | try { |
||
| 307 | return view('themes.default1.front.clients.order1'); |
||
| 308 | } catch (Exception $ex) { |
||
| 309 | Bugsnag::notifyException($ex); |
||
| 310 | |||
| 311 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | /* |
||
| 316 | * Show all the orders for User |
||
| 317 | */ |
||
| 318 | |||
| 319 | public function getOrders() |
||
| 320 | { |
||
| 321 | try { |
||
| 322 | $orders = Order::where('client', \Auth::user()->id); |
||
| 323 | |||
| 324 | return \DataTables::of($orders->get()) |
||
| 325 | ->addColumn('id', function ($model) { |
||
| 326 | return $model->id; |
||
| 327 | }) |
||
| 328 | ->addColumn('product_name', function ($model) { |
||
| 329 | return $model->product()->first()->name; |
||
| 330 | }) |
||
| 331 | ->addColumn('expiry', function ($model) { |
||
| 332 | $tz = \Auth::user()->timezone()->first()->name; |
||
| 333 | $end = $this->getExpiryDate($model); |
||
| 334 | |||
| 335 | return $end; |
||
| 336 | }) |
||
| 337 | |||
| 338 | ->addColumn('Action', function ($model) { |
||
| 339 | $sub = $model->subscription()->first(); |
||
| 340 | $order = Order::where('id', $model->id)->select('product')->first(); |
||
| 341 | $productid = $order->product; |
||
| 342 | $order_cont = new \App\Http\Controllers\Order\OrderController(); |
||
| 343 | $status = $order_cont->checkInvoiceStatusByOrderId($model->id); |
||
| 344 | $url = ''; |
||
| 345 | if ($status == 'success') { |
||
| 346 | if ($sub) { |
||
| 347 | $url = $this->renewPopup($sub->id, $productid); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | $listUrl = $this->getPopup($model, $productid); |
||
| 352 | |||
| 353 | return '<a href='.url('my-order/'.$model->id)." |
||
| 354 | class='btn btn-primary btn-xs' style='margin-right:5px;'> |
||
| 355 | <i class='fa fa-eye' title='Details of order'></i> View $listUrl $url </a>"; |
||
| 356 | }) |
||
| 357 | ->rawColumns(['id', 'created_at', 'ends_at', 'product', 'Action']) |
||
| 358 | ->make(true); |
||
| 359 | } catch (Exception $ex) { |
||
| 360 | app('log')->error($ex->getMessage()); |
||
| 361 | Bugsnag::notifyException($ex); |
||
| 362 | echo $ex->getMessage(); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | public function subscriptions() |
||
| 367 | { |
||
| 368 | try { |
||
| 369 | return view('themes.default1.front.clients.subscription'); |
||
| 370 | } catch (Exception $ex) { |
||
| 371 | Bugsnag::notifyException($ex); |
||
| 372 | |||
| 373 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | public function profile() |
||
| 378 | { |
||
| 379 | try { |
||
| 380 | $user = $this->user->where('id', \Auth::user()->id)->first(); |
||
| 381 | //dd($user); |
||
| 382 | $timezonesList = \App\Model\Common\Timezone::get(); |
||
| 383 | foreach ($timezonesList as $timezone) { |
||
| 384 | $location = $timezone->location; |
||
| 385 | if ($location) { |
||
| 386 | $start = strpos($location, '('); |
||
| 387 | $end = strpos($location, ')', $start + 1); |
||
| 388 | $length = $end - $start; |
||
| 389 | $result = substr($location, $start + 1, $length - 1); |
||
| 390 | $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]); |
||
| 391 | } |
||
| 392 | } |
||
| 393 | //for display |
||
| 394 | $timezones = array_column($display, 'name', 'id'); |
||
| 395 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state); |
||
| 396 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country); |
||
| 397 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
| 398 | |||
| 399 | return view( |
||
| 400 | 'themes.default1.front.clients.profile', |
||
| 401 | compact('user', 'timezones', 'state', 'states', 'bussinesses') |
||
| 402 | ); |
||
| 403 | } catch (Exception $ex) { |
||
| 404 | Bugsnag::notifyException($ex); |
||
| 405 | |||
| 406 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | public function getInvoice($id) |
||
| 411 | { |
||
| 412 | try { |
||
| 413 | $invoice = $this->invoice->findOrFail($id); |
||
| 414 | $items = $invoice->invoiceItem()->get(); |
||
| 415 | $user = \Auth::user(); |
||
| 416 | |||
| 417 | return view('themes.default1.front.clients.show-invoice', compact('invoice', 'items', 'user')); |
||
| 418 | } catch (Exception $ex) { |
||
| 419 | Bugsnag::notifyException($ex); |
||
| 420 | |||
| 421 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | public function getOrder($id) |
||
| 426 | { |
||
| 427 | try { |
||
| 428 | $order = $this->order->findOrFail($id); |
||
| 429 | $invoice = $order->invoice()->first(); |
||
| 430 | $items = $order->invoice()->first()->invoiceItem()->get(); |
||
| 431 | $subscription = ''; |
||
| 432 | $plan = ''; |
||
| 433 | if ($order->subscription) { |
||
| 434 | $subscription = $order->subscription; |
||
| 435 | |||
| 436 | $plan = $subscription->plan()->first(); |
||
| 437 | } |
||
| 438 | $product = $order->product()->first(); |
||
| 439 | $price = $product->price()->first(); |
||
| 440 | $licenseStatus = StatusSetting::pluck('license_status')->first(); |
||
| 441 | $user = \Auth::user(); |
||
| 442 | |||
| 443 | return view( |
||
| 444 | 'themes.default1.front.clients.show-order', |
||
| 445 | compact('invoice', 'order', 'user', 'plan', 'product', 'subscription', 'licenseStatus') |
||
| 446 | ); |
||
| 447 | } catch (Exception $ex) { |
||
| 448 | Bugsnag::notifyException($ex); |
||
| 449 | |||
| 450 | return redirect('/')->with('fails', $ex->getMessage()); |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | public function getPaymentByOrderId($orderid, $userid) |
||
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | public function getPaymentByOrderIdClient($orderid, $userid) |
||
| 530 | } |
||
| 531 | } |
||
| 532 | } |
||
| 533 |
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.