| Total Complexity | 47 |
| Total Lines | 491 |
| 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 | $countVersions = count($versions); |
||
| 134 | $countExpiry = 0; |
||
| 135 | $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first(); |
||
| 136 | $order = Order::where('invoice_id', '=', $invoice_id)->first(); |
||
| 137 | |||
| 138 | $order_id = $order->id; |
||
| 139 | $endDate = Subscription::select('ends_at') |
||
| 140 | ->where('product_id', $productid)->where('order_id', $order_id)->first(); |
||
| 141 | |||
| 142 | foreach ($versions as $version) { |
||
| 143 | if ($version->created_at->toDateTimeString() |
||
| 144 | < $endDate->ends_at->toDateTimeString()) |
||
| 145 | { |
||
| 146 | $countExpiry = $countExpiry +1; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | return \DataTables::of($versions) |
||
| 151 | ->addColumn('id', function ($versions) { |
||
| 152 | return ucfirst($versions->id); |
||
| 153 | }) |
||
| 154 | ->addColumn('version', function ($versions) { |
||
| 155 | return ucfirst($versions->version); |
||
| 156 | }) |
||
| 157 | ->addColumn('title', function ($versions) { |
||
| 158 | return ucfirst($versions->title); |
||
| 159 | }) |
||
| 160 | ->addColumn('description', function ($versions) { |
||
| 161 | return ucfirst($versions->description); |
||
| 162 | }) |
||
| 163 | ->addColumn('file', function ($versions) use ($countExpiry, $countVersions,$clientid, $invoiceid, $productid) { |
||
| 164 | $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first(); |
||
| 165 | $order = Order::where('invoice_id', '=', $invoice_id)->first(); |
||
| 166 | $order_id = $order->id; |
||
| 167 | $getDownloadCondition = Product::where('id', $productid)->value('deny_after_subscription'); |
||
| 168 | $endDate = Subscription::select('ends_at') |
||
| 169 | ->where('product_id', $productid)->where('order_id', $order_id)->first(); |
||
| 170 | |||
| 171 | //if product has expiry date ie sunscriptioon is generated |
||
| 172 | if ($endDate) { |
||
| 173 | if ($getDownloadCondition == 1) {//Perpetual download till expiry selected |
||
| 174 | $getDownload = $this->whenDownloadTillExpiry($endDate, $productid, $versions, $clientid, $invoiceid); |
||
| 175 | |||
| 176 | return $getDownload; |
||
| 177 | |||
| 178 | |||
| 179 | }elseif($getDownloadCondition == 0){//When download retires after subscription |
||
| 180 | |||
| 181 | $getDownload = $this->whenDownloadExpiresAfterExpiry($countExpiry, $countVersions,$endDate,$productid,$versions,$clientid,$invoiceid); |
||
| 182 | |||
| 183 | return $getDownload; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | }) |
||
| 187 | ->rawColumns(['version', 'title', 'description', 'file']) |
||
| 188 | ->make(true); |
||
| 189 | } catch (Exception $ex) { |
||
| 190 | Bugsnag::notifyException($ex); |
||
| 191 | echo $ex->getMessage(); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | public function whenDownloadTillExpiry($endDate, $productid, $versions, $clientid, $invoiceid) |
||
| 196 | { |
||
| 197 | if ($versions->created_at->toDateTimeString() |
||
| 198 | |||
| 199 | < $endDate->ends_at->toDateTimeString()) { |
||
| 200 | return '<p><a href='.url('download/'.$productid.'/' |
||
| 201 | .$clientid.'/'.$invoiceid.'/'.$versions->id). |
||
| 202 | " class='btn btn-sm btn-primary'><i class='fa fa-download'> |
||
| 203 | </i> Download</a>".' |
||
| 204 | |||
| 205 | </p>'; |
||
| 206 | } else { |
||
| 207 | return '<button class="btn btn-primary |
||
| 208 | btn-sm disabled tooltip">Download <span class="tooltiptext"> |
||
| 209 | Please Renew!!</span></button>'; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | |||
| 214 | public function whenDownloadExpiresAfterExpiry($countExpiry, $countVersions,$endDate,$productid,$versions,$clientid,$invoiceid) |
||
| 215 | { |
||
| 216 | if ($countExpiry == $countVersions) { |
||
| 217 | return '<p><a href='.url('download/'.$productid.'/' |
||
| 218 | .$clientid.'/'.$invoiceid.'/'.$versions->id). |
||
| 219 | " class='btn btn-sm btn-primary'><i class='fa fa-download'> |
||
| 220 | </i> Download</a>".' |
||
| 221 | |||
| 222 | </p>'; |
||
| 223 | } else { |
||
| 224 | return '<button class="btn btn-primary |
||
| 225 | btn-sm disabled tooltip">Download <span class="tooltiptext"> |
||
| 226 | Please Renew!!</span></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('ends_at') |
||
| 257 | ->where('product_id', $productid)->where('order_id', $order_id)->first(); |
||
| 258 | foreach ($link as $lin) { |
||
| 259 | if(strtotime($lin['created_at']) < strtotime($orderEndDate->ends_at)) |
||
| 260 | { |
||
| 261 | $countExpiry = $countExpiry +1; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | return \DataTables::of($link) |
||
| 266 | ->addColumn('version', function ($link) { |
||
| 267 | return ucfirst($link['tag_name']); |
||
| 268 | }) |
||
| 269 | ->addColumn('name', function ($link) { |
||
| 270 | return ucfirst($link['name']); |
||
| 271 | }) |
||
| 272 | ->addColumn('description', function ($link) { |
||
| 273 | $markdown = Markdown::convertToHtml(ucfirst($link['body'])); |
||
| 274 | |||
| 275 | return $markdown; |
||
| 276 | }) |
||
| 277 | ->addColumn('file', function ($link) use ( $countExpiry,$countVersions,$invoiceid, $productid) { |
||
| 278 | $order = Order::where('invoice_id', '=', $invoiceid)->first(); |
||
| 279 | $order_id = $order->id; |
||
| 280 | $orderEndDate = Subscription::select('ends_at') |
||
| 281 | ->where('product_id', $productid)->where('order_id', $order_id)->first(); |
||
| 282 | if ($orderEndDate) { |
||
| 283 | $actionButton = $this->getActionButton($countExpiry,$countVersions,$link, $orderEndDate, $productid); |
||
| 284 | |||
| 285 | return $actionButton; |
||
| 286 | } elseif (!$orderEndDate) { |
||
| 287 | $link = $this->github_api->getCurl1($link['zipball_url']); |
||
| 288 | |||
| 289 | return '<p><a href='.$link['header']['Location'] |
||
| 290 | ." class='btn btn-sm btn-primary'>Download </a>" |
||
| 291 | .' |
||
| 292 | </p>'; |
||
| 293 | } |
||
| 294 | }) |
||
| 295 | ->rawColumns(['version', 'name', 'description', 'file']) |
||
| 296 | ->make(true); |
||
| 297 | } catch (Exception $ex) { |
||
| 298 | Bugsnag::notifyException($ex); |
||
| 299 | echo $ex->getMessage(); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | public function orders() |
||
| 304 | { |
||
| 305 | try { |
||
| 306 | return view('themes.default1.front.clients.order1'); |
||
| 307 | } catch (Exception $ex) { |
||
| 308 | Bugsnag::notifyException($ex); |
||
| 309 | |||
| 310 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | /* |
||
| 315 | * Show all the orders for User |
||
| 316 | */ |
||
| 317 | |||
| 318 | public function getOrders() |
||
| 319 | { |
||
| 320 | try { |
||
| 321 | $orders = Order::where('client', \Auth::user()->id); |
||
| 322 | |||
| 323 | return \DataTables::of($orders->get()) |
||
| 324 | ->addColumn('id', function ($model) { |
||
| 325 | return $model->id; |
||
| 326 | }) |
||
| 327 | ->addColumn('product_name', function ($model) { |
||
| 328 | return $model->product()->first()->name; |
||
| 329 | }) |
||
| 330 | ->addColumn('expiry', function ($model) { |
||
| 331 | $tz = \Auth::user()->timezone()->first()->name; |
||
| 332 | $end = $this->getExpiryDate($model); |
||
| 333 | |||
| 334 | return $end; |
||
| 335 | }) |
||
| 336 | |||
| 337 | ->addColumn('Action', function ($model) { |
||
| 338 | $sub = $model->subscription()->first(); |
||
| 339 | $order = Order::where('id', $model->id)->select('product')->first(); |
||
| 340 | $productid = $order->product; |
||
| 341 | $order_cont = new \App\Http\Controllers\Order\OrderController(); |
||
| 342 | $status = $order_cont->checkInvoiceStatusByOrderId($model->id); |
||
| 343 | $url = ''; |
||
| 344 | if ($status == 'success') { |
||
| 345 | if ($sub) { |
||
| 346 | $url = $this->renewPopup($sub->id, $productid); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | $listUrl = $this->getPopup($model, $productid); |
||
| 351 | |||
| 352 | return '<a href='.url('my-order/'.$model->id)." |
||
| 353 | class='btn btn-primary btn-xs' style='margin-right:5px;'> |
||
| 354 | <i class='fa fa-eye' title='Details of order'></i> View $listUrl $url </a>"; |
||
| 355 | }) |
||
| 356 | ->rawColumns(['id', 'created_at', 'ends_at', 'product', 'Action']) |
||
| 357 | ->make(true); |
||
| 358 | } catch (Exception $ex) { |
||
| 359 | Bugsnag::notifyException($ex); |
||
| 360 | echo $ex->getMessage(); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | public function subscriptions() |
||
| 365 | { |
||
| 366 | try { |
||
| 367 | return view('themes.default1.front.clients.subscription'); |
||
| 368 | } catch (Exception $ex) { |
||
| 369 | Bugsnag::notifyException($ex); |
||
| 370 | |||
| 371 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | public function profile() |
||
| 376 | { |
||
| 377 | try { |
||
| 378 | $user = $this->user->where('id', \Auth::user()->id)->first(); |
||
| 379 | //dd($user); |
||
| 380 | $timezones = new \App\Model\Common\Timezone(); |
||
| 381 | $timezones = $timezones->pluck('name', 'id')->toArray(); |
||
| 382 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state); |
||
| 383 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country); |
||
| 384 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
| 385 | |||
| 386 | return view('themes.default1.front.clients.profile', |
||
| 387 | compact('user', 'timezones', 'state', 'states', 'bussinesses')); |
||
| 388 | } catch (Exception $ex) { |
||
| 389 | Bugsnag::notifyException($ex); |
||
| 390 | |||
| 391 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | public function getInvoice($id) |
||
| 396 | { |
||
| 397 | try { |
||
| 398 | $invoice = $this->invoice->findOrFail($id); |
||
| 399 | $items = $invoice->invoiceItem()->get(); |
||
| 400 | $user = \Auth::user(); |
||
| 401 | |||
| 402 | return view('themes.default1.front.clients.show-invoice', compact('invoice', 'items', 'user')); |
||
| 403 | } catch (Exception $ex) { |
||
| 404 | Bugsnag::notifyException($ex); |
||
| 405 | |||
| 406 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | public function getOrder($id) |
||
| 411 | { |
||
| 412 | try { |
||
| 413 | $order = $this->order->findOrFail($id); |
||
| 414 | $invoice = $order->invoice()->first(); |
||
| 415 | $items = $order->invoice()->first()->invoiceItem()->get(); |
||
| 416 | $subscription = ''; |
||
| 417 | $plan = ''; |
||
| 418 | if ($order->subscription) { |
||
| 419 | $subscription = $order->subscription; |
||
| 420 | |||
| 421 | $plan = $subscription->plan()->first(); |
||
| 422 | } |
||
| 423 | $product = $order->product()->first(); |
||
| 424 | $price = $product->price()->first(); |
||
| 425 | $user = \Auth::user(); |
||
| 426 | |||
| 427 | return view('themes.default1.front.clients.show-order', |
||
| 428 | compact('invoice', 'order', 'user', 'plan', 'product', 'subscription')); |
||
| 429 | } catch (Exception $ex) { |
||
| 430 | Bugsnag::notifyException($ex); |
||
| 431 | |||
| 432 | return redirect('/')->with('fails', $ex->getMessage()); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | public function getPaymentByOrderId($orderid, $userid) |
||
| 472 | } |
||
| 473 | } |
||
| 474 | |||
| 475 | public function getPaymentByOrderIdClient($orderid, $userid) |
||
| 512 | } |
||
| 513 | } |
||
| 514 | } |
||
| 515 |
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.