| Total Complexity | 55 |
| Total Lines | 554 |
| 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 |
||
| 20 | class ClientController extends AdvanceSearchController |
||
| 21 | { |
||
| 22 | public $user; |
||
| 23 | public $activate; |
||
| 24 | public $product; |
||
| 25 | |||
| 26 | public function __construct() |
||
| 27 | { |
||
| 28 | $this->middleware('auth'); |
||
| 29 | // $this->middleware('admin'); |
||
| 30 | $user = new User(); |
||
| 31 | $this->user = $user; |
||
| 32 | $activate = new AccountActivate(); |
||
| 33 | $this->activate = $activate; |
||
| 34 | $product = new \App\Model\Product\Product(); |
||
| 35 | $this->product = $product; |
||
| 36 | $license = new LicenseController(); |
||
| 37 | $this->licensing = $license; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Display a listing of the resource. |
||
| 42 | * |
||
| 43 | * @return \Response |
||
| 44 | */ |
||
| 45 | public function index(Request $request) |
||
| 46 | { |
||
| 47 | $name = $request->input('name'); |
||
| 48 | $username = $request->input('username'); |
||
| 49 | $company = $request->input('company'); |
||
| 50 | $mobile = $request->input('mobile'); |
||
| 51 | $email = $request->input('email'); |
||
| 52 | $country = $request->input('country'); |
||
| 53 | $industry = $request->input('industry'); |
||
| 54 | $company_type = $request->input('company_type'); |
||
| 55 | $company_size = $request->input('company_size'); |
||
| 56 | $role = $request->input('role'); |
||
| 57 | $position = $request->input('position'); |
||
| 58 | $reg_from = $request->input('reg_from'); |
||
| 59 | $reg_till = $request->input('reg_till'); |
||
| 60 | |||
| 61 | return view('themes.default1.user.client.index', |
||
| 62 | compact('name', 'username', 'company', 'mobile', 'email', |
||
| 63 | 'country', 'industry', 'company_type', 'company_size', 'role', 'position', 'reg_from', 'reg_till')); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get Clients for yajra datatable. |
||
| 68 | */ |
||
| 69 | public function getClients(Request $request) |
||
| 70 | { |
||
| 71 | $name = $request->input('name'); |
||
| 72 | $username = $request->input('username'); |
||
| 73 | $company = $request->input('company'); |
||
| 74 | $mobile = $request->input('mobile'); |
||
| 75 | $email = $request->input('email'); |
||
| 76 | $country = $request->input('country'); |
||
| 77 | $industry = $request->input('industry'); |
||
| 78 | $company_type = $request->input('company_type'); |
||
| 79 | $company_size = $request->input('company_size'); |
||
| 80 | $role = $request->input('role'); |
||
| 81 | $position = $request->input('position'); |
||
| 82 | $reg_from = $request->input('reg_from'); |
||
| 83 | $reg_till = $request->input('reg_till'); |
||
| 84 | $user = $this->advanceSearch($name, $username, $company, |
||
| 85 | $mobile, $email, $country, $industry, $company_type, $company_size, $role, $position, $reg_from, $reg_till); |
||
| 86 | |||
| 87 | return\ DataTables::of($user->get()) |
||
| 88 | ->addColumn('checkbox', function ($model) { |
||
| 89 | return "<input type='checkbox' class='user_checkbox' |
||
| 90 | value=".$model->id.' name=select[] id=check>'; |
||
| 91 | }) |
||
| 92 | ->addColumn('first_name', function ($model) { |
||
| 93 | return '<a href='.url('clients/'.$model->id).'>' |
||
| 94 | .ucfirst($model->first_name).' '.ucfirst($model->last_name).'</a>'; |
||
| 95 | }) |
||
| 96 | ->addColumn('email', function ($model) { |
||
| 97 | return $model->email; |
||
| 98 | }) |
||
| 99 | ->addColumn('created_at', function ($model) { |
||
| 100 | $ends = $model->created_at; |
||
| 101 | if ($ends) { |
||
| 102 | $date1 = new DateTime($ends); |
||
| 103 | $tz = \Auth::user()->timezone()->first()->name; |
||
| 104 | $date1->setTimezone(new DateTimeZone($tz)); |
||
| 105 | $end = $date1->format('M j, Y, g:i a '); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $end; |
||
| 109 | }) |
||
| 110 | // ->showColumns('email', 'created_at') |
||
| 111 | ->addColumn('active', function ($model) { |
||
| 112 | if ($model->active == 1) { |
||
| 113 | $email = "<span class='glyphicon glyphicon-envelope' |
||
| 114 | style='color:green' title='verified email'></span>"; |
||
| 115 | } else { |
||
| 116 | $email = "<span class='glyphicon glyphicon-envelope' |
||
| 117 | style='color:red' title='unverified email'></span>"; |
||
| 118 | } |
||
| 119 | if ($model->mobile_verified == 1) { |
||
| 120 | $mobile = "<span class='glyphicon glyphicon-phone' |
||
| 121 | style='color:green' title='verified mobile'></span>"; |
||
| 122 | } else { |
||
| 123 | $mobile = "<span class='glyphicon glyphicon-phone' |
||
| 124 | style='color:red' title='unverified mobile'></span>"; |
||
| 125 | } |
||
| 126 | |||
| 127 | return $email.' '.$mobile; |
||
| 128 | }) |
||
| 129 | ->addColumn('action', function ($model) { |
||
| 130 | return '<a href='.url('clients/'.$model->id.'/edit') |
||
| 131 | ." class='btn btn-sm btn-primary btn-xs'> |
||
| 132 | <i class='fa fa-edit' style='color:white;'> </i> Edit</a>" |
||
| 133 | .' <a href='.url('clients/'.$model->id) |
||
| 134 | ." class='btn btn-sm btn-primary btn-xs'> |
||
| 135 | <i class='fa fa-eye' style='color:white;'> </i> View</a>"; |
||
| 136 | // return 'hhhh'; |
||
| 137 | }) |
||
| 138 | ->rawColumns(['checkbox', 'first_name', 'email', 'created_at', 'active', 'action']) |
||
| 139 | ->make(true); |
||
| 140 | |||
| 141 | // ->searchColumns('email', 'first_name') |
||
| 142 | // ->orderColumns('email', 'first_name', 'created_at') |
||
| 143 | // ->make(); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Show the form for creating a new resource. |
||
| 148 | * |
||
| 149 | * @return \Response |
||
| 150 | */ |
||
| 151 | public function create() |
||
| 152 | { |
||
| 153 | $timezones = new \App\Model\Common\Timezone(); |
||
| 154 | $timezones = $timezones->pluck('name', 'id')->toArray(); |
||
| 155 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
| 156 | $managers = User::where('role', 'admin')->where('position', 'manager') |
||
| 157 | ->pluck('first_name', 'id')->toArray(); |
||
| 158 | $timezonesList = \App\Model\Common\Timezone::get(); |
||
| 159 | foreach ($timezonesList as $timezone) { |
||
| 160 | $location = $timezone->location; |
||
| 161 | if ($location) { |
||
| 162 | $start = strpos($location, '('); |
||
| 163 | $end = strpos($location, ')', $start + 1); |
||
| 164 | $length = $end - $start; |
||
| 165 | $result = substr($location, $start + 1, $length - 1); |
||
| 166 | $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | $timezones = array_column($display, 'name', 'id'); |
||
| 170 | |||
| 171 | return view('themes.default1.user.client.create', compact('timezones', 'bussinesses', 'managers')); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Store a newly created resource in storage. |
||
| 176 | * |
||
| 177 | * @return \Response |
||
| 178 | */ |
||
| 179 | public function store(ClientRequest $request) |
||
| 180 | { |
||
| 181 | try { |
||
| 182 | $user = $this->user; |
||
| 183 | $str = 'demopass'; |
||
| 184 | $password = \Hash::make($str); |
||
| 185 | $user->password = $password; |
||
| 186 | $cont = new \App\Http\Controllers\Front\PageController(); |
||
| 187 | $location = $cont->getLocation(); |
||
| 188 | $user->ip = $location['ip']; |
||
| 189 | $user->fill($request->input())->save(); |
||
| 190 | // $this->sendWelcomeMail($user); |
||
| 191 | // $mailchimp = new \App\Http\Controllers\Common\MailChimpController(); |
||
| 192 | // $r = $mailchimp->addSubscriber($user->email); |
||
| 193 | $licenseStatus = StatusSetting::pluck('license_status')->first(); |
||
| 194 | if ($licenseStatus == 1) { |
||
| 195 | $addUserToLicensing = $this->licensing->addNewUser($request->first_name, $request->last_name, $request->email); |
||
| 196 | } |
||
| 197 | |||
| 198 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 199 | } catch (\Swift_TransportException $e) { |
||
| 200 | return redirect()->back()->with('warning', 'User has been created successfully |
||
| 201 | But email configuration has some problem!'); |
||
| 202 | } catch (\Exception $e) { |
||
| 203 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Display the specified resource. |
||
| 209 | * |
||
| 210 | * @param int $id |
||
| 211 | * |
||
| 212 | * @return \Response |
||
| 213 | */ |
||
| 214 | public function show($id) |
||
| 215 | { |
||
| 216 | try { |
||
| 217 | $invoice = new Invoice(); |
||
| 218 | $order = new Order(); |
||
| 219 | $invoices = $invoice->where('user_id', $id)->orderBy('created_at', 'desc')->get(); |
||
| 220 | $invoiceSum = $this->getTotalInvoice($invoices); |
||
| 221 | $amountReceived = $this->getAmountPaid($id); |
||
| 222 | $pendingAmount = $invoiceSum - $amountReceived; |
||
| 223 | if ($pendingAmount < 0) { |
||
| 224 | $pendingAmount = 0; |
||
| 225 | } |
||
| 226 | $extraAmt = $this->getExtraAmt($id); |
||
| 227 | $client = $this->user->where('id', $id)->first(); |
||
| 228 | |||
| 229 | // $client = ""; |
||
| 230 | $currency = $client->currency; |
||
| 231 | $orders = $order->where('client', $id)->get(); |
||
| 232 | $comments = $client->comments()->where('user_id', $client->id)->get(); |
||
| 233 | |||
| 234 | return view('themes.default1.user.client.show', |
||
| 235 | compact('id', 'client', 'invoices', 'model_popup', 'orders', |
||
| 236 | 'payments', 'invoiceSum', 'amountReceived', 'pendingAmount', 'currency', 'extraAmt', 'comments')); |
||
| 237 | } catch (\Exception $ex) { |
||
| 238 | app('log')->info($ex->getMessage()); |
||
| 239 | Bugsnag::notifyException($ex); |
||
| 240 | |||
| 241 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | public function getOrderDetail($id) |
||
| 246 | { |
||
| 247 | $client = $this->user->where('id', $id)->first(); |
||
| 248 | $responseData = []; |
||
| 249 | foreach ($client->order()->orderBy('created_at', 'desc')->get() as $order) { |
||
| 250 | $date = $order->created_at; |
||
| 251 | $productName = $order->product()->first() && $order->product()->first()->name ? |
||
| 252 | $order->product()->first()->name : 'Unknown'; |
||
| 253 | $number = $order->number; |
||
| 254 | $price = $order->price_override; |
||
| 255 | $status = $order->order_status; |
||
| 256 | array_push($responseData, (['date'=> $date, 'productName'=>$productName, |
||
| 257 | 'number' => $number, 'price' =>$price, 'status'=>$status, ])); |
||
| 258 | } |
||
| 259 | |||
| 260 | return $responseData; |
||
| 261 | } |
||
| 262 | |||
| 263 | //Get Paymetn Details on Invoice Page |
||
| 264 | public function getPaymentDetail($id) |
||
| 265 | { |
||
| 266 | $client = $this->user->where('id', $id)->first(); |
||
| 267 | $invoice = new Invoice(); |
||
| 268 | $invoices = $invoice->where('user_id', $id)->orderBy('created_at', 'desc')->get(); |
||
| 269 | $extraAmt = $this->getExtraAmt($id); |
||
| 270 | $date = ''; |
||
| 271 | $responseData = []; |
||
| 272 | if ($invoices) { |
||
| 273 | foreach ($client->payment()->orderBy('created_at', 'desc')->get() as $payment) { |
||
| 274 | $number = $payment->invoice()->first() ? $payment->invoice()->first()->number : '--'; |
||
| 275 | $date = $payment->created_at; |
||
| 276 | $date1 = new DateTime($date); |
||
| 277 | $tz = \Auth::user()->timezone()->first()->name; |
||
| 278 | $date1->setTimezone(new DateTimeZone($tz)); |
||
| 279 | $date = $date1->format('M j, Y, g:i a '); |
||
| 280 | $pay_method = $payment->payment_method; |
||
| 281 | if ($payment->invoice_id == 0) { |
||
| 282 | $amount = $extraAmt; |
||
| 283 | } else { |
||
| 284 | $amount = $payment->amount; |
||
| 285 | } |
||
| 286 | $status = ucfirst($payment->payment_status); |
||
| 287 | array_push($responseData, (['number'=>$number, 'pay_method'=>$pay_method, 'amount'=>$amount, 'status'=>$status, 'date'=>$date])); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | return $responseData; |
||
| 292 | } |
||
| 293 | |||
| 294 | public function getClientDetail($id) |
||
| 306 | } |
||
| 307 | |||
| 308 | public function getExtraAmt($userId) |
||
| 309 | { |
||
| 310 | try { |
||
| 311 | $amounts = Payment::where('user_id', $userId) |
||
| 312 | ->where('invoice_id', 0) |
||
| 313 | ->select('amt_to_credit')->get(); |
||
| 314 | $paidSum = 0; |
||
| 315 | foreach ($amounts as $amount) { |
||
| 316 | $paidSum = $paidSum + $amount->amt_to_credit; |
||
| 317 | } |
||
| 318 | |||
| 319 | return $paidSum; |
||
| 320 | } catch (\Exception $ex) { |
||
| 321 | app('log')->useDailyFiles(storage_path().'/logs/laravel.log'); |
||
| 322 | app('log')->info($ex->getMessage()); |
||
| 323 | Bugsnag::notifyException($ex); |
||
| 324 | |||
| 325 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get total Amount paid for a particular invoice. |
||
| 331 | */ |
||
| 332 | public function getAmountPaid($userId) |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get total of the Invoices for a User. |
||
| 354 | */ |
||
| 355 | public function getTotalInvoice($invoices) |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Show the form for editing the specified resource. |
||
| 367 | * |
||
| 368 | * @param int $id |
||
| 369 | * |
||
| 370 | * @return \Response |
||
| 371 | */ |
||
| 372 | public function edit($id) |
||
| 373 | { |
||
| 374 | try { |
||
| 375 | $user = $this->user->where('id', $id)->first(); |
||
| 376 | $timezonesList = \App\Model\Common\Timezone::get(); |
||
| 377 | foreach ($timezonesList as $timezone) { |
||
| 378 | $location = $timezone->location; |
||
| 379 | if ($location) { |
||
| 380 | $start = strpos($location, '('); |
||
| 381 | $end = strpos($location, ')', $start + 1); |
||
| 382 | $length = $end - $start; |
||
| 383 | $result = substr($location, $start + 1, $length - 1); |
||
| 384 | $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]); |
||
| 385 | } |
||
| 386 | } |
||
| 387 | //for display |
||
| 388 | $timezones = array_column($display, 'name', 'id'); |
||
| 389 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state); |
||
| 390 | $managers = User::where('role', 'admin') |
||
| 391 | ->where('position', 'manager') |
||
| 392 | ->pluck('first_name', 'id')->toArray(); |
||
| 393 | $selectedCurrency = Currency::where('code', $user->currency) |
||
| 394 | ->pluck('name', 'code')->toArray(); |
||
| 395 | $selectedCompany = \DB::table('company_types')->where('short', $user->company_type) |
||
| 396 | ->pluck('name', 'short')->toArray(); |
||
| 397 | $selectedIndustry = \App\Model\Common\Bussiness::where('short', $user->bussiness) |
||
| 398 | ->pluck('name', 'short')->toArray(); |
||
| 399 | $selectedCompanySize = \DB::table('company_sizes')->where('short', $user->company_size) |
||
| 400 | ->pluck('name', 'short')->toArray(); |
||
| 401 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country); |
||
| 402 | |||
| 403 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
| 404 | |||
| 405 | return view('themes.default1.user.client.edit', |
||
| 406 | compact('bussinesses', 'user', 'timezones', 'state', |
||
| 407 | 'states', 'managers', 'selectedCurrency', 'selectedCompany', |
||
| 408 | 'selectedIndustry', 'selectedCompanySize')); |
||
| 409 | } catch (\Exception $ex) { |
||
| 410 | app('log')->error($ex->getMessage()); |
||
| 411 | |||
| 412 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Update the specified resource in storage. |
||
| 418 | * |
||
| 419 | * @param int $id |
||
| 420 | * |
||
| 421 | * @return \Response |
||
| 422 | */ |
||
| 423 | public function update($id, Request $request) |
||
| 424 | { |
||
| 425 | try { |
||
| 426 | $user = $this->user->where('id', $id)->first(); |
||
| 427 | $symbol = Currency::where('code', $request->input('currency'))->pluck('symbol')->first(); |
||
| 428 | $user->currency_symbol = $symbol; |
||
| 429 | $user->fill($request->input())->save(); |
||
| 430 | $licenseStatus = StatusSetting::pluck('license_status')->first(); |
||
| 431 | if ($licenseStatus == 1) { |
||
| 432 | $editUserInLicensing = $this->licensing->editUserInLicensing($user->first_name, $user->last_name, $user->email); |
||
| 433 | } |
||
| 434 | |||
| 435 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 436 | } catch (\Exception $ex) { |
||
| 437 | app('log')->error($ex->getMessage()); |
||
| 438 | Bugsnag::notifyException($ex); |
||
| 439 | |||
| 440 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Remove the specified resource from storage. |
||
| 446 | * |
||
| 447 | * @param int $id |
||
| 448 | * |
||
| 449 | * @return \Response |
||
| 450 | */ |
||
| 451 | public function destroy(Request $request) |
||
| 486 | </div>'; |
||
| 487 | } |
||
| 488 | } |
||
| 489 | |||
| 490 | public function getUsers(Request $request) |
||
| 498 | } |
||
| 499 | |||
| 500 | public function search(Request $request) |
||
| 501 | { |
||
| 502 | try { |
||
| 503 | $term = trim($request->q); |
||
| 504 | if (empty($term)) { |
||
| 505 | return \Response::json([]); |
||
| 506 | } |
||
| 507 | $users = User::where('email', 'LIKE', '%'.$term.'%') |
||
| 508 | ->orWhere('first_name', 'LIKE', '%'.$term.'%') |
||
| 509 | ->orWhere('last_name', 'LIKE', '%'.$term.'%') |
||
| 510 | ->select('id', 'email', 'profile_pic', 'first_name', 'last_name')->get(); |
||
| 511 | $formatted_tags = []; |
||
| 512 | |||
| 513 | foreach ($users as $user) { |
||
| 514 | $formatted_users[] = ['id' => $user->id, 'text' => $user->email, 'profile_pic' => $user->profile_pic, |
||
| 515 | 'first_name' => $user->first_name, 'last_name' => $user->last_name, ]; |
||
| 516 | } |
||
| 517 | |||
| 518 | return \Response::json($formatted_users); |
||
| 519 | } catch (\Exception $e) { |
||
| 520 | // returns if try fails with exception meaagse |
||
| 521 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | public function advanceSearch($name = '', $username = '', $company = '', |
||
| 541 | } |
||
| 542 | |||
| 543 | public function sendWelcomeMail($user) |
||
| 544 | { |
||
| 545 | $activate_model = new AccountActivate(); |
||
| 574 | } |
||
| 575 | } |
||
| 576 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.