| Total Complexity | 43 |
| Total Lines | 445 |
| 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() |
||
| 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) |
||
| 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') { |
||
| 231 | $countExpiry = $countExpiry + 1; |
||
| 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) { |
||
| 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); |
||
| 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 | .' |
||
| 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> 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) |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | public function getPaymentByOrderIdClient($orderid, $userid) |
||
| 468 | } |
||
| 469 | } |
||
| 470 | } |
||
| 471 |
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.