| Total Complexity | 43 |
| Total Lines | 464 |
| 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 |
||
| 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() |
||
| 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 | $getDownloadCondition = Product::where('id', $productid)->value('deny_after_subscription'); |
||
| 152 | $endDate = Subscription::select('ends_at') |
||
| 153 | ->where('product_id', $productid)->where('order_id', $order_id)->first(); |
||
| 154 | |||
| 155 | //if product has expiry date ie sunscriptioon is generated |
||
| 156 | if ($endDate) { |
||
| 157 | if ($getDownloadCondition == 1){//Perpetual download till expiry selected |
||
| 158 | $getDownload = $this->whenDownloadTillExpiry($endDate,$productid,$versions,$clientid,$invoiceid); |
||
| 159 | return $getDownload; |
||
| 160 | |||
| 161 | }elseif($getDownloadCondition == 0){//When download retires after subscription |
||
| 162 | |||
| 163 | $getDownload = $this->whenDownloadExpiresAfterExpiry($endDate,$productid,$versions,$clientid,$invoiceid); |
||
| 164 | return $getDownload; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | }) |
||
| 168 | ->rawColumns(['version', 'title', 'description', 'file']) |
||
| 169 | ->make(true); |
||
| 170 | } catch (Exception $ex) { |
||
| 171 | Bugsnag::notifyException($ex); |
||
| 172 | echo $ex->getMessage(); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | public function whenDownloadTillExpiry($endDate,$productid,$versions,$clientid,$invoiceid) |
||
| 178 | { |
||
| 179 | if ($versions->created_at->toDateTimeString() |
||
| 180 | |||
| 181 | < $endDate->ends_at->toDateTimeString()) { |
||
| 182 | return '<p><a href='.url('download/'.$productid.'/' |
||
| 183 | .$clientid.'/'.$invoiceid.'/'.$versions->id). |
||
| 184 | " class='btn btn-sm btn-primary'><i class='fa fa-download'> |
||
| 185 | </i> Download</a>".' |
||
| 186 | |||
| 187 | </p>'; |
||
| 188 | } else { |
||
| 189 | return '<button class="btn btn-primary |
||
| 190 | btn-sm disabled tooltip">Download <span class="tooltiptext"> |
||
| 191 | Please Renew!!</span></button>'; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | public function whenDownloadExpiresAfterExpiry($endDate,$productid,$versions,$clientid,$invoiceid) |
||
| 196 | { |
||
| 197 | if ($versions->created_at->toDateTimeString() |
||
| 198 | < $endDate->ends_at->toDateTimeString()) { |
||
| 199 | return '<p><a href='.url('download/'.$productid.'/' |
||
| 200 | .$clientid.'/'.$invoiceid.'/'.$versions->id). |
||
| 201 | " class='btn btn-sm btn-primary'><i class='fa fa-download'> |
||
| 202 | </i> Download</a>".' |
||
| 203 | |||
| 204 | </p>'; |
||
| 205 | } else { |
||
| 206 | return '<button class="btn btn-primary |
||
| 207 | btn-sm disabled tooltip">Download <span class="tooltiptext"> |
||
| 208 | Please Renew!!</span></button>'; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * Get list of all the versions from Github. |
||
| 215 | * |
||
| 216 | * @param type $productid |
||
| 217 | * @param type $clientid |
||
| 218 | * @param type $invoiceid |
||
| 219 | */ |
||
| 220 | |||
| 221 | |||
| 222 | public function getGithubVersionList($productid, $clientid, $invoiceid) |
||
| 223 | { |
||
| 224 | try { |
||
| 225 | $products = $this->product::where('id', $productid) |
||
| 226 | ->select('name', 'version', 'github_owner', 'github_repository')->get(); |
||
| 227 | $owner = ''; |
||
| 228 | $repo = ''; |
||
| 229 | foreach ($products as $product) { |
||
| 230 | $owner = $product->github_owner; |
||
| 231 | $repo = $product->github_repository; |
||
| 232 | } |
||
| 233 | $url = "https://api.github.com/repos/$owner/$repo/releases"; |
||
| 234 | $link = $this->github_api->getCurl1($url); |
||
| 235 | $link = $link['body']; |
||
| 236 | $link = (array_slice($link, 0, 10, true)); |
||
| 237 | |||
| 238 | return \DataTables::of($link) |
||
| 239 | ->addColumn('version', function ($link) { |
||
| 240 | return ucfirst($link['tag_name']); |
||
| 241 | }) |
||
| 242 | ->addColumn('name', function ($link) { |
||
| 243 | return ucfirst($link['name']); |
||
| 244 | }) |
||
| 245 | ->addColumn('description', function ($link) { |
||
| 246 | $markdown = Markdown::convertToHtml(ucfirst($link['body'])); |
||
| 247 | |||
| 248 | return $markdown; |
||
| 249 | }) |
||
| 250 | ->addColumn('file', function ($link) use ($invoiceid, $productid) { |
||
| 251 | $order = Order::where('invoice_id', '=', $invoiceid)->first(); |
||
| 252 | $order_id = $order->id; |
||
| 253 | $orderEndDate = Subscription::select('ends_at') |
||
| 254 | ->where('product_id', $productid)->where('order_id', $order_id)->first(); |
||
| 255 | if ($orderEndDate) { |
||
| 256 | $actionButton = $this->getActionButton($link, $orderEndDate, $productid); |
||
| 257 | |||
| 258 | return $actionButton; |
||
| 259 | } elseif (!$orderEndDate) { |
||
| 260 | $link = $this->github_api->getCurl1($link['zipball_url']); |
||
| 261 | |||
| 262 | return '<p><a href='.$link['header']['Location'] |
||
| 263 | ." class='btn btn-sm btn-primary'>Download </a>" |
||
| 264 | .' |
||
| 265 | </p>'; |
||
| 266 | } |
||
| 267 | }) |
||
| 268 | ->rawColumns(['version', 'name', 'description', 'file']) |
||
| 269 | ->make(true); |
||
| 270 | } catch (Exception $ex) { |
||
| 271 | Bugsnag::notifyException($ex); |
||
| 272 | echo $ex->getMessage(); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | public function orders() |
||
| 277 | { |
||
| 278 | try { |
||
| 279 | return view('themes.default1.front.clients.order1'); |
||
| 280 | } catch (Exception $ex) { |
||
| 281 | Bugsnag::notifyException($ex); |
||
| 282 | |||
| 283 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | /* |
||
| 288 | * Show all the orders for User |
||
| 289 | */ |
||
| 290 | |||
| 291 | public function getOrders() |
||
| 292 | { |
||
| 293 | try { |
||
| 294 | $orders = Order::where('client', \Auth::user()->id); |
||
| 295 | |||
| 296 | return \DataTables::of($orders->get()) |
||
| 297 | ->addColumn('id', function ($model) { |
||
| 298 | return $model->id; |
||
| 299 | }) |
||
| 300 | ->addColumn('product_name', function ($model) { |
||
| 301 | return $model->product()->first()->name; |
||
| 302 | }) |
||
| 303 | ->addColumn('expiry', function ($model) { |
||
| 304 | $tz = \Auth::user()->timezone()->first()->name; |
||
| 305 | $end = $this->getExpiryDate($model); |
||
| 306 | |||
| 307 | return $end; |
||
| 308 | }) |
||
| 309 | |||
| 310 | ->addColumn('Action', function ($model) { |
||
| 311 | $sub = $model->subscription()->first(); |
||
| 312 | $order = Order::where('id', $model->id)->select('product')->first(); |
||
| 313 | $productid = $order->product; |
||
| 314 | $order_cont = new \App\Http\Controllers\Order\OrderController(); |
||
| 315 | $status = $order_cont->checkInvoiceStatusByOrderId($model->id); |
||
| 316 | $url = ''; |
||
| 317 | if ($status == 'success') { |
||
| 318 | if ($sub) { |
||
| 319 | $url = $this->renewPopup($sub->id, $productid); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | $listUrl = $this->getPopup($model, $productid); |
||
| 324 | |||
| 325 | return '<a href='.url('my-order/'.$model->id)." |
||
| 326 | class='btn btn-primary btn-xs' style='margin-right:5px;'> |
||
| 327 | <i class='fa fa-eye' title='Details of order'></i> View $listUrl $url </a>"; |
||
| 328 | }) |
||
| 329 | ->rawColumns(['id', 'created_at', 'ends_at', 'product', 'Action']) |
||
| 330 | ->make(true); |
||
| 331 | } catch (Exception $ex) { |
||
| 332 | Bugsnag::notifyException($ex); |
||
| 333 | echo $ex->getMessage(); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | public function subscriptions() |
||
| 338 | { |
||
| 339 | try { |
||
| 340 | return view('themes.default1.front.clients.subscription'); |
||
| 341 | } catch (Exception $ex) { |
||
| 342 | Bugsnag::notifyException($ex); |
||
| 343 | |||
| 344 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | public function profile() |
||
| 349 | { |
||
| 350 | try { |
||
| 351 | $user = $this->user->where('id', \Auth::user()->id)->first(); |
||
| 352 | //dd($user); |
||
| 353 | $timezones = new \App\Model\Common\Timezone(); |
||
| 354 | $timezones = $timezones->pluck('name', 'id')->toArray(); |
||
| 355 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state); |
||
| 356 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country); |
||
| 357 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
| 358 | |||
| 359 | return view('themes.default1.front.clients.profile', |
||
| 360 | compact('user', 'timezones', 'state', 'states', 'bussinesses')); |
||
| 361 | } catch (Exception $ex) { |
||
| 362 | Bugsnag::notifyException($ex); |
||
| 363 | |||
| 364 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | public function getInvoice($id) |
||
| 369 | { |
||
| 370 | try { |
||
| 371 | $invoice = $this->invoice->findOrFail($id); |
||
| 372 | $items = $invoice->invoiceItem()->get(); |
||
| 373 | $user = \Auth::user(); |
||
| 374 | |||
| 375 | return view('themes.default1.front.clients.show-invoice', compact('invoice', 'items', 'user')); |
||
| 376 | } catch (Exception $ex) { |
||
| 377 | Bugsnag::notifyException($ex); |
||
| 378 | |||
| 379 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | public function getOrder($id) |
||
| 384 | { |
||
| 385 | try { |
||
| 386 | $order = $this->order->findOrFail($id); |
||
| 387 | $invoice = $order->invoice()->first(); |
||
| 388 | $items = $order->invoice()->first()->invoiceItem()->get(); |
||
| 389 | $subscription = ''; |
||
| 390 | $plan = ''; |
||
| 391 | if ($order->subscription) { |
||
| 392 | $subscription = $order->subscription; |
||
| 393 | |||
| 394 | $plan = $subscription->plan()->first(); |
||
| 395 | } |
||
| 396 | $product = $order->product()->first(); |
||
| 397 | $price = $product->price()->first(); |
||
| 398 | $user = \Auth::user(); |
||
| 399 | |||
| 400 | return view('themes.default1.front.clients.show-order', |
||
| 401 | compact('invoice', 'order', 'user', 'plan', 'product', 'subscription')); |
||
| 402 | } catch (Exception $ex) { |
||
| 403 | Bugsnag::notifyException($ex); |
||
| 404 | |||
| 405 | return redirect('/')->with('fails', $ex->getMessage()); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | public function getPaymentByOrderId($orderid, $userid) |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | public function getPaymentByOrderIdClient($orderid, $userid) |
||
| 485 | } |
||
| 486 | } |
||
| 487 | } |
||
| 488 |
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.