| Total Complexity | 55 |
| Total Lines | 490 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like OrderController 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 OrderController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class OrderController extends BaseOrderController |
||
| 21 | { |
||
| 22 | public $order; |
||
| 23 | public $user; |
||
| 24 | public $promotion; |
||
| 25 | public $product; |
||
| 26 | public $subscription; |
||
| 27 | public $invoice; |
||
| 28 | public $invoice_items; |
||
| 29 | public $price; |
||
| 30 | public $plan; |
||
| 31 | |||
| 32 | public function __construct() |
||
| 33 | { |
||
| 34 | $this->middleware('auth'); |
||
| 35 | $this->middleware('admin'); |
||
| 36 | |||
| 37 | $order = new Order(); |
||
| 38 | $this->order = $order; |
||
| 39 | |||
| 40 | $user = new User(); |
||
| 41 | $this->user = $user; |
||
| 42 | |||
| 43 | $promotion = new Promotion(); |
||
| 44 | $this->promotion = $promotion; |
||
| 45 | |||
| 46 | $product = new Product(); |
||
| 47 | $this->product = $product; |
||
| 48 | |||
| 49 | $subscription = new Subscription(); |
||
| 50 | $this->subscription = $subscription; |
||
| 51 | |||
| 52 | $invoice = new Invoice(); |
||
| 53 | $this->invoice = $invoice; |
||
| 54 | |||
| 55 | $invoice_items = new InvoiceItem(); |
||
| 56 | $this->invoice_items = $invoice_items; |
||
| 57 | |||
| 58 | $plan = new Plan(); |
||
| 59 | $this->plan = $plan; |
||
| 60 | |||
| 61 | $price = new Price(); |
||
| 62 | $this->price = $price; |
||
| 63 | |||
| 64 | $product_upload = new ProductUpload(); |
||
| 65 | $this->product_upload = $product_upload; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Display a listing of the resource. |
||
| 70 | * |
||
| 71 | * @return \Response |
||
| 72 | */ |
||
| 73 | public function index(Request $request) |
||
| 74 | { |
||
| 75 | try { |
||
| 76 | $products = $this->product->where('id', '!=', 1)->pluck('name', 'id')->toArray(); |
||
| 77 | $order_no = $request->input('order_no'); |
||
| 78 | $product_id = $request->input('product_id'); |
||
| 79 | $expiry = $request->input('expiry'); |
||
| 80 | $from = $request->input('from'); |
||
| 81 | $till = $request->input('till'); |
||
| 82 | $domain = $request->input('domain'); |
||
| 83 | |||
| 84 | return view('themes.default1.order.index', compact('products', 'order_no', 'product_id', 'expiry', 'from', 'till', 'domain')); |
||
| 85 | } catch (\Exception $e) { |
||
| 86 | Bugsnag::notifyExeption($e); |
||
| 87 | |||
| 88 | return redirect('orders')->with('fails', $e->getMessage()); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | public function getOrders(Request $request) |
||
| 93 | { |
||
| 94 | $order_no = $request->input('order_no'); |
||
| 95 | $product_id = $request->input('product_id'); |
||
| 96 | $expiry = $request->input('expiry'); |
||
| 97 | $from = $request->input('from'); |
||
| 98 | $till = $request->input('till'); |
||
| 99 | $domain = $request->input('domain'); |
||
| 100 | $query = $this->advanceSearch($order_no, $product_id, $expiry, $from, $till, $domain); |
||
| 101 | |||
| 102 | return\ DataTables::of($query->get()) |
||
| 103 | |||
| 104 | ->addColumn('checkbox', function ($model) { |
||
| 105 | return "<input type='checkbox' class='order_checkbox' value=".$model->id.' name=select[] id=check>'; |
||
| 106 | }) |
||
| 107 | ->addColumn('date', function ($model) { |
||
| 108 | $date = $model->created_at; |
||
| 109 | |||
| 110 | return "<span style='display:none'>$model->id</span>".$date->format('l, F j, Y H:m A'); |
||
| 111 | }) |
||
| 112 | ->addColumn('client', function ($model) { |
||
| 113 | $user = $this->user->where('id', $model->client)->first(); |
||
| 114 | $first = $user->first_name; |
||
| 115 | $last = $user->last_name; |
||
| 116 | $id = $user->id; |
||
| 117 | |||
| 118 | return '<a href='.url('clients/'.$id).'>'.ucfirst($first).' '.ucfirst($last).'<a>'; |
||
| 119 | }) |
||
| 120 | ->addColumn('number', function ($model) { |
||
| 121 | return ucfirst($model->number); |
||
| 122 | }) |
||
| 123 | ->addColumn('price_override', function ($model) { |
||
| 124 | return ucfirst($model->price_override); |
||
| 125 | }) |
||
| 126 | ->addColumn('order_status', function ($model) { |
||
| 127 | return ucfirst($model->order_status); |
||
| 128 | }) |
||
| 129 | // ->showColumns('number', 'price_override', 'order_status') |
||
| 130 | ->addColumn('ends_at', function ($model) { |
||
| 131 | $end = $this->getEndDate($model); |
||
| 132 | |||
| 133 | return $end; |
||
| 134 | }) |
||
| 135 | ->addColumn('action', function ($model) { |
||
| 136 | $sub = $model->subscription()->first(); |
||
| 137 | $status = $this->checkInvoiceStatusByOrderId($model->id); |
||
| 138 | $url = $this->getUrl($model, $status, $sub); |
||
| 139 | |||
| 140 | return $url; |
||
| 141 | }) |
||
| 142 | |||
| 143 | ->rawColumns(['checkbox', 'date', 'client', 'number', 'price_override', 'order_status', 'ends_at', 'action']) |
||
| 144 | ->make(true); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Show the form for creating a new resource. |
||
| 149 | * |
||
| 150 | * @return \Response |
||
| 151 | */ |
||
| 152 | public function create() |
||
| 153 | { |
||
| 154 | try { |
||
| 155 | $clients = $this->user->pluck('first_name', 'id')->toArray(); |
||
| 156 | $product = $this->product->pluck('name', 'id')->toArray(); |
||
| 157 | $subscription = $this->subscription->pluck('name', 'id')->toArray(); |
||
| 158 | $promotion = $this->promotion->pluck('code', 'id')->toArray(); |
||
| 159 | |||
| 160 | return view('themes.default1.order.create', compact('clients', 'product', 'subscription', 'promotion')); |
||
| 161 | } catch (\Exception $e) { |
||
| 162 | Bugsnag::notifyExeption($e); |
||
| 163 | |||
| 164 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Store a newly created resource in storage. |
||
| 170 | * |
||
| 171 | * @return \Response |
||
| 172 | */ |
||
| 173 | public function show($id) |
||
| 174 | { |
||
| 175 | try { |
||
| 176 | $order = $this->order->findOrFail($id); |
||
| 177 | $subscription = $order->subscription()->first(); |
||
| 178 | $invoiceid = $order->invoice_id; |
||
| 179 | $invoice = $this->invoice->where('id', $invoiceid)->first(); |
||
| 180 | if (!$invoice) { |
||
| 181 | return redirect()->back()->with('fails', 'no orders'); |
||
| 182 | } |
||
| 183 | $invoiceItems = $this->invoice_items->where('invoice_id', $invoiceid)->get(); |
||
| 184 | $user = $this->user->find($invoice->user_id); |
||
| 185 | |||
| 186 | return view('themes.default1.order.show', compact('invoiceItems', 'invoice', 'user', 'order', 'subscription')); |
||
| 187 | } catch (\Exception $ex) { |
||
| 188 | Bugsnag::notifyExeption($ex); |
||
| 189 | |||
| 190 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Show the form for editing the specified resource. |
||
| 196 | * |
||
| 197 | * @param int $id |
||
| 198 | * |
||
| 199 | * @return \Response |
||
| 200 | */ |
||
| 201 | public function edit($id) |
||
| 202 | { |
||
| 203 | try { |
||
| 204 | $order = $this->order->where('id', $id)->first(); |
||
| 205 | $clients = $this->user->pluck('first_name', 'id')->toArray(); |
||
| 206 | $product = $this->product->pluck('name', 'id')->toArray(); |
||
| 207 | $subscription = $this->subscription->pluck('name', 'id')->toArray(); |
||
| 208 | $promotion = $this->promotion->pluck('code', 'id')->toArray(); |
||
| 209 | |||
| 210 | return view('themes.default1.order.edit', compact('clients', 'product', 'subscription', 'promotion', 'order')); |
||
| 211 | } catch (\Exception $e) { |
||
| 212 | Bugsnag::notifyExeption($e); |
||
| 213 | |||
| 214 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Update the specified resource in storage. |
||
| 220 | * |
||
| 221 | * @param int $id |
||
| 222 | * |
||
| 223 | * @return \Response |
||
| 224 | */ |
||
| 225 | public function update($id, OrderRequest $request) |
||
| 226 | { |
||
| 227 | try { |
||
| 228 | $order = $this->order->where('id', $id)->first(); |
||
| 229 | $order->fill($request->input())->save(); |
||
| 230 | |||
| 231 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 232 | } catch (\Exception $e) { |
||
| 233 | Bugsnag::notifyExeption($e); |
||
| 234 | |||
| 235 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Remove the specified resource from storage. |
||
| 241 | * |
||
| 242 | * @param int $id |
||
| 243 | * |
||
| 244 | * @return \Response |
||
| 245 | */ |
||
| 246 | public function destroy(Request $request) |
||
| 247 | { |
||
| 248 | try { |
||
| 249 | // dd('df'); |
||
| 250 | $ids = $request->input('select'); |
||
| 251 | if (!empty($ids)) { |
||
| 252 | foreach ($ids as $id) { |
||
| 253 | $order = $this->order->where('id', $id)->first(); |
||
| 254 | if ($order) { |
||
| 255 | $order->delete(); |
||
| 256 | } else { |
||
| 257 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 258 | <i class='fa fa-ban'></i> |
||
| 259 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 260 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 261 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 262 | './* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
||
| 263 | </div>'; |
||
| 264 | //echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 268 | <i class='fa fa-ban'></i> |
||
| 269 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 270 | /* @scrutinizer ignore-type */\Lang::get('message.success').' |
||
| 271 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 272 | './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
||
| 273 | </div>'; |
||
| 274 | } else { |
||
| 275 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 276 | <i class='fa fa-ban'></i> |
||
| 277 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 278 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 279 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 280 | './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').' |
||
| 281 | </div>'; |
||
| 282 | //echo \Lang::get('message.select-a-row'); |
||
| 283 | } |
||
| 284 | } catch (\Exception $e) { |
||
| 285 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 286 | <i class='fa fa-ban'></i> |
||
| 287 | <b>"./** @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 288 | /** @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 289 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 290 | '.$e->getMessage().' |
||
| 291 | </div>'; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Create orders. |
||
| 297 | * |
||
| 298 | * @param Request $request |
||
| 299 | * |
||
| 300 | * @return type |
||
| 301 | */ |
||
| 302 | public function orderExecute(Request $request) |
||
| 303 | { |
||
| 304 | try { |
||
| 305 | $invoiceid = $request->input('invoiceid'); |
||
| 306 | $execute = $this->executeOrder($invoiceid); |
||
| 307 | if ($execute == 'success') { |
||
| 308 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 309 | } else { |
||
| 310 | return redirect()->back()->with('fails', \Lang::get('message.not-saved-successfully')); |
||
| 311 | } |
||
| 312 | } catch (\Exception $ex) { |
||
| 313 | Bugsnag::notifyException($ex); |
||
| 314 | |||
| 315 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * generating serial key if product type is downloadable. |
||
| 321 | * |
||
| 322 | * @param type $product_type |
||
| 323 | * |
||
| 324 | * @throws \Exception |
||
| 325 | * |
||
| 326 | * @return type |
||
| 327 | */ |
||
| 328 | public function generateSerialKey($product_type) |
||
| 329 | { |
||
| 330 | try { |
||
| 331 | if ($product_type == 2) { |
||
| 332 | $str = str_random(16); |
||
| 333 | $str = strtoupper($str); |
||
| 334 | $str = Crypt::encrypt($str); |
||
| 335 | // dd($str); |
||
| 336 | return $str; |
||
| 337 | } |
||
| 338 | } catch (\Exception $ex) { |
||
| 339 | Bugsnag::notifyException($ex); |
||
| 340 | |||
| 341 | throw new \Exception($ex->getMessage()); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | public function generateNumber() |
||
| 346 | { |
||
| 347 | try { |
||
| 348 | return rand('10000000', '99999999'); |
||
| 349 | } catch (\Exception $ex) { |
||
| 350 | throw new \Exception($ex->getMessage()); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | public function domainChange(Request $request) |
||
| 355 | { |
||
| 356 | $domain = $request->input('domain'); |
||
| 357 | $id = $request->input('id'); |
||
| 358 | $order = $this->order->find($id); |
||
| 359 | $order->domain = $domain; |
||
| 360 | $order->save(); |
||
| 361 | } |
||
| 362 | |||
| 363 | public function deleleById($id) |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | public function advanceSearch($order_no = '', $product_id = '', $expiry = '', $from = '', $till = '', $domain = '') |
||
| 382 | { |
||
| 383 | $join = $this->order |
||
| 384 | ->leftJoin('subscriptions', 'orders.id', '=', 'subscriptions.order_id'); |
||
| 385 | if ($order_no) { |
||
| 386 | $join = $join->where('number', $order_no); |
||
| 387 | } |
||
| 388 | if ($product_id) { |
||
| 389 | $join = $join->where('product', $product_id); |
||
| 390 | } |
||
| 391 | if ($expiry) { |
||
| 392 | $join = $join->where('ends_at', 'LIKE', '%'.$expiry.'%'); |
||
| 393 | } |
||
| 394 | if ($from) { |
||
| 395 | |||
| 396 | $fromdate = date_create($from); |
||
| 397 | $from = date_format($fromdate, 'Y-m-d H:m:i'); |
||
| 398 | $tills = date('Y-m-d H:m:i'); |
||
| 399 | $tillDate = $this->getTillDate($from,$till,$tills); |
||
| 400 | $join = $join->whereBetween('orders.created_at', [$from, $tillDate]); |
||
| 401 | } |
||
| 402 | if ($till) { |
||
| 403 | $tilldate = date_create($till); |
||
| 404 | $till = date_format($tilldate, 'Y-m-d H:m:i'); |
||
| 405 | $froms = $this->order->first()->created_at; |
||
| 406 | $fromDate = $this->getFromDate($from,$froms); |
||
| 407 | $join = $join->whereBetween('orders.created_at', [$fromDate, $till]); |
||
| 408 | } |
||
| 409 | if ($domain) { |
||
| 410 | if (str_finish($domain, '/')) { |
||
| 411 | $domain = substr_replace($domain, '', -1, 0); |
||
| 412 | } |
||
| 413 | $join = $join->where('domain', 'LIKE', '%'.$domain.'%'); |
||
| 414 | } |
||
| 415 | |||
| 416 | $join = $join->select('orders.id', 'orders.created_at', 'client', 'price_override', 'order_status', 'number', 'serial_key'); |
||
| 417 | |||
| 418 | return $join; |
||
| 419 | } |
||
| 420 | |||
| 421 | public function getTillDate($from,$till,$tills) |
||
| 422 | { |
||
| 423 | if ($till) |
||
| 424 | { |
||
| 425 | $todate = date_create($till); |
||
| 426 | $tills = date_format($todate, 'Y-m-d H:m:i'); |
||
| 427 | } |
||
| 428 | return $tills; |
||
| 429 | } |
||
| 430 | |||
| 431 | public function getFromDate($from,$froms) |
||
| 432 | { |
||
| 433 | if ($from) { |
||
| 434 | $fromdate = date_create($from); |
||
| 435 | $froms = date_format($fromdate, 'Y-m-d H:m:i'); |
||
| 436 | } |
||
| 437 | return $froms; |
||
| 438 | } |
||
| 439 | |||
| 440 | public function plan($invoice_item_id) |
||
| 441 | { |
||
| 442 | try { |
||
| 443 | $planid = 0; |
||
| 444 | $item = $this->invoice_items->find($invoice_item_id); |
||
| 445 | if ($item) { |
||
| 446 | $planid = $item->plan_id; |
||
| 447 | } |
||
| 448 | |||
| 449 | return $planid; |
||
| 450 | } catch (\Exception $ex) { |
||
| 451 | Bugsnag::notifyException($ex); |
||
| 452 | |||
| 453 | throw new \Exception($ex->getMessage()); |
||
| 454 | } |
||
| 455 | } |
||
| 456 | |||
| 457 | public function checkInvoiceStatusByOrderId($orderid) |
||
| 458 | { |
||
| 459 | try { |
||
| 460 | $status = 'pending'; |
||
| 461 | $order = $this->order->find($orderid); |
||
| 462 | if ($order) { |
||
| 463 | $invoiceid = $order->invoice_id; |
||
| 464 | $invoice = $this->invoice->find($invoiceid); |
||
| 465 | if ($invoice) { |
||
| 466 | if ($invoice->status == 'Success') { |
||
| 467 | $status = 'success'; |
||
| 468 | } |
||
| 469 | } |
||
| 470 | } |
||
| 471 | |||
| 472 | return $status; |
||
| 473 | } catch (\Exception $ex) { |
||
| 474 | Bugsnag::notifyException($ex); |
||
| 475 | |||
| 476 | throw new \Exception($ex->getMessage()); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | public function product($itemid) |
||
| 481 | { |
||
| 482 | $invoice_items = new InvoiceItem(); |
||
| 483 | $invoice_item = $invoice_items->find($itemid); |
||
| 484 | $product = $invoice_item->product_name; |
||
| 485 | |||
| 486 | return $product; |
||
| 487 | } |
||
| 488 | |||
| 489 | public function subscription($orderid) |
||
| 490 | { |
||
| 491 | $sub = $this->subscription->where('order_id', $orderid)->first(); |
||
| 492 | |||
| 493 | return $sub; |
||
| 494 | } |
||
| 495 | |||
| 496 | public function expiry($orderid) |
||
| 504 | } |
||
| 505 | |||
| 506 | public function renew($orderid) |
||
| 510 | } |
||
| 511 | } |
||
| 512 |