| Total Complexity | 40 |
| Total Lines | 509 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 3 | Features | 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 |
||
| 19 | class ClientController extends AdvanceSearchController |
||
| 20 | { |
||
| 21 | use PaymentsAndInvoices; |
||
|
|
|||
| 22 | |||
| 23 | public $user; |
||
| 24 | public $activate; |
||
| 25 | public $product; |
||
| 26 | |||
| 27 | public function __construct() |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Display a listing of the resource. |
||
| 43 | * |
||
| 44 | * @param Request $request |
||
| 45 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 46 | */ |
||
| 47 | public function index(Request $request) |
||
| 48 | { |
||
| 49 | $validator = \Validator::make($request->all(), [ |
||
| 50 | 'reg_from' => 'nullable', |
||
| 51 | 'reg_till' => 'nullable|after:reg_from', |
||
| 52 | |||
| 53 | ]); |
||
| 54 | if ($validator->fails()) { |
||
| 55 | $request->reg_from = ''; |
||
| 56 | $request->reg_till = ''; |
||
| 57 | |||
| 58 | return redirect('clients')->with('fails', 'Registered from should be before registered till date'); |
||
| 59 | } |
||
| 60 | |||
| 61 | return view('themes.default1.user.client.index', compact('request')); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get Clients for yajra datatable. |
||
| 66 | * @param Request $request |
||
| 67 | * @return |
||
| 68 | * @throws \Exception |
||
| 69 | */ |
||
| 70 | public function getClients(Request $request) |
||
| 71 | { |
||
| 72 | $baseQuery = $this->getBaseQueryForUserSearch($request); |
||
| 73 | |||
| 74 | return\ DataTables::of($baseQuery) |
||
| 75 | ->addColumn('checkbox', function ($model) { |
||
| 76 | $isAccountManager = User::where('account_manager', $model->id)->get(); |
||
| 77 | $isSalesManager = User::where('manager', $model->id)->get(); |
||
| 78 | if (count($isSalesManager)) { |
||
| 79 | return "<input type='checkbox' disabled> |
||
| 80 | <i class='fa fa-info-circle' style='cursor: help; font-size: small; color: rgb(60, 141, 188);' ".'<label data-toggle="tooltip" style="font-weight:500;" data-placement="top" title="This user cannot be deleted as he/she is existing sales manager for certain clients. Please replace Sales Manager from settings and then try deleting."> |
||
| 81 | </label>'.'</i>'; |
||
| 82 | } elseif (count($isAccountManager)) { |
||
| 83 | // dd("<input type='checkbox' ".tooltip('dsf')."'disabled'"); |
||
| 84 | return "<input type='checkbox' disabled> |
||
| 85 | <i class='fa fa-info-circle' style='cursor: help; font-size: small; color: rgb(60, 141, 188);' ".'<label data-toggle="tooltip" style="font-weight:500;" data-placement="top" title="This user cannot be deleted as he/she is existing account manager for certain clients. Please replace Account Manager from settings and then try deleting."> |
||
| 86 | </label>'.'</i>'; |
||
| 87 | } else { |
||
| 88 | return "<input type='checkbox' class='user_checkbox' value=".$model->id.' name=select[] id=check>'; |
||
| 89 | } |
||
| 90 | }) |
||
| 91 | ->addColumn('name', function ($model) { |
||
| 92 | return '<a href='.url('clients/'.$model->id).'>'.ucfirst($model->name).'</a>'; |
||
| 93 | }) |
||
| 94 | ->addColumn('email', function ($model) { |
||
| 95 | return $model->email; |
||
| 96 | }) |
||
| 97 | ->addColumn('mobile', function ($model) { |
||
| 98 | return $model->mobile; |
||
| 99 | }) |
||
| 100 | ->addColumn('country', function ($model) { |
||
| 101 | return ucfirst(strtolower($model->country)); |
||
| 102 | }) |
||
| 103 | ->addColumn('company', function ($model) { |
||
| 104 | return $model->company; |
||
| 105 | }) |
||
| 106 | ->addColumn('created_at', function ($model) { |
||
| 107 | return getDateHtml($model->created_at); |
||
| 108 | }) |
||
| 109 | ->addColumn('active', function ($model) { |
||
| 110 | return $this->getActiveLabel($model->mobile_verified, $model->active, $model->is_2fa_enabled); |
||
| 111 | }) |
||
| 112 | ->addColumn('action', function ($model) { |
||
| 113 | return '<a href='.url('clients/'.$model->id.'/edit') |
||
| 114 | ." class='btn btn-sm btn-secondary btn-xs'".tooltip('Edit')." |
||
| 115 | <i class='fa fa-edit' style='color:white;'> </i></a>" |
||
| 116 | .' <a href='.url('clients/'.$model->id) |
||
| 117 | ." class='btn btn-sm btn-secondary btn-xs'".tooltip('View')." |
||
| 118 | <i class='fa fa-eye' style='color:white;'> </i></a>"; |
||
| 119 | }) |
||
| 120 | |||
| 121 | ->filterColumn('name', function ($model, $keyword) { |
||
| 122 | // removing all white spaces so that it can be searched irrespective of number of spaces |
||
| 123 | $model->whereRaw("CONCAT(first_name, ' ',last_name) like ?", ["%$keyword%"]); |
||
| 124 | }) |
||
| 125 | ->filterColumn('email', function ($model, $keyword) { |
||
| 126 | $model->whereRaw('email like ?', ["%$keyword%"]); |
||
| 127 | }) |
||
| 128 | ->filterColumn('mobile', function ($model, $keyword) { |
||
| 129 | // removing all white spaces so that it can be searched in a single query |
||
| 130 | $searchQuery = str_replace(' ', '', $keyword); |
||
| 131 | $model->whereRaw("CONCAT('+', mobile_code, mobile) like ?", ["%$searchQuery%"]); |
||
| 132 | }) |
||
| 133 | ->filterColumn('country', function ($model, $keyword) { |
||
| 134 | // removing all white spaces so that it can be searched in a single query |
||
| 135 | $searchQuery = str_replace(' ', '', $keyword); |
||
| 136 | $model->whereRaw('country_name like ?', ["%$searchQuery%"]); |
||
| 137 | }) |
||
| 138 | ->orderColumn('name', 'name $1') |
||
| 139 | ->orderColumn('email', 'email $1') |
||
| 140 | ->orderColumn('mobile', 'mobile $1') |
||
| 141 | ->orderColumn('country', 'country $1') |
||
| 142 | ->orderColumn('created_at', 'created_at $1') |
||
| 143 | |||
| 144 | ->rawColumns(['checkbox', 'name', 'email', 'created_at', 'active', 'action']) |
||
| 145 | ->make(true); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function getActiveLabel($mobileActive, $emailActive, $twoFaActive) |
||
| 149 | { |
||
| 150 | $emailLabel = "<i class='fas fa-envelope' style='color:red' <label data-toggle='tooltip' style='font-weight:500;' data-placement='top' title='Unverified email'> </label></i>"; |
||
| 151 | $mobileLabel = "<i class='fas fa-phone' style='color:red' <label data-toggle='tooltip' style='font-weight:500;' data-placement='top' title='Unverified mobile'> </label></i>"; |
||
| 152 | $twoFalabel = "<i class='fas fa-qrcode' style='color:red' <label data-toggle='tooltip' style='font-weight:500;' data-placement='top' title='2FA not enabled'> </label></i>"; |
||
| 153 | if ($mobileActive) { |
||
| 154 | $mobileLabel = "<i class='fas fa-phone' style='color:green' <label data-toggle='tooltip' style='font-weight:500;' data-placement='top' title='Mobile verified'></label></i>"; |
||
| 155 | } |
||
| 156 | if ($emailActive) { |
||
| 157 | $emailLabel = "<i class='fas fa-envelope' style='color:green' <label data-toggle='tooltip' style='font-weight:500;' data-placement='top' title='Email verified'> </label></i>"; |
||
| 158 | } |
||
| 159 | if ($twoFaActive) { |
||
| 160 | $twoFalabel = "<i class='fas fa-qrcode' style='color:green' <label data-toggle='tooltip' style='font-weight:500;' data-placement='top' title='2FA Enabled'> </label></i>"; |
||
| 161 | } |
||
| 162 | |||
| 163 | return $emailLabel.' '.$mobileLabel.' '.$twoFalabel; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Show the form for creating a new resource. |
||
| 168 | * |
||
| 169 | * @return \Response |
||
| 170 | */ |
||
| 171 | public function create() |
||
| 172 | { |
||
| 173 | $timezones = new \App\Model\Common\Timezone(); |
||
| 174 | $timezones = $timezones->pluck('name', 'id')->toArray(); |
||
| 175 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
| 176 | $managers = User::where('role', 'admin')->where('position', 'manager') |
||
| 177 | ->pluck('first_name', 'id')->toArray(); |
||
| 178 | $accountManager = User::where('role', 'admin')->where('position', 'account_manager') |
||
| 179 | ->pluck('first_name', 'id')->toArray(); |
||
| 180 | $timezonesList = \App\Model\Common\Timezone::get(); |
||
| 181 | foreach ($timezonesList as $timezone) { |
||
| 182 | $location = $timezone->location; |
||
| 183 | if ($location) { |
||
| 184 | $start = strpos($location, '('); |
||
| 185 | $end = strpos($location, ')', $start + 1); |
||
| 186 | $length = $end - $start; |
||
| 187 | $result = substr($location, $start + 1, $length - 1); |
||
| 188 | $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | $timezones = array_column($display, 'name', 'id'); |
||
| 192 | |||
| 193 | return view('themes.default1.user.client.create', compact('timezones', 'bussinesses', 'managers', 'accountManager')); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Store a newly created resource in storage. |
||
| 198 | * |
||
| 199 | * @return \Response |
||
| 200 | */ |
||
| 201 | public function store(ClientRequest $request) |
||
| 202 | { |
||
| 203 | try { |
||
| 204 | $user = $this->user; |
||
| 205 | $str = 'demopass'; |
||
| 206 | $password = \Hash::make($str); |
||
| 207 | $user->password = $password; |
||
| 208 | if ($request->input('mobile_code') == '') { |
||
| 209 | $country = new Country(); |
||
| 210 | $mobile_code = $country->where('country_code_char2', $request->input('country'))->pluck('phonecode')->first(); |
||
| 211 | } else { |
||
| 212 | $mobile_code = str_replace('+', '', $request->input('mobile_code')); |
||
| 213 | } |
||
| 214 | $currency_symbol = Currency::where('code', $request->input('currency'))->pluck('symbol')->first(); |
||
| 215 | $location = getLocation(); |
||
| 216 | $user->user_name = $request->input('user_name'); |
||
| 217 | $user->first_name = $request->input('first_name'); |
||
| 218 | $user->last_name = $request->input('last_name'); |
||
| 219 | $user->email = $request->input('email'); |
||
| 220 | $user->password = $password; |
||
| 221 | $user->company = $request->input('company'); |
||
| 222 | $user->bussiness = $request->input('bussiness'); |
||
| 223 | $user->active = $request->input('active'); |
||
| 224 | $user->mobile_verified = $request->input('mobile_verified'); |
||
| 225 | $user->role = $request->input('role'); |
||
| 226 | $user->position = $request->input('position'); |
||
| 227 | $user->company_type = $request->input('company_type'); |
||
| 228 | $user->company_size = $request->input('company_size'); |
||
| 229 | $user->address = $request->input('address'); |
||
| 230 | $user->town = $request->input('town'); |
||
| 231 | $user->country = $request->input('country'); |
||
| 232 | $user->state = $request->input('state'); |
||
| 233 | $user->zip = $request->input('zip'); |
||
| 234 | $user->timezone_id = $request->input('timezone_id'); |
||
| 235 | $user->currency = $request->input('currency'); |
||
| 236 | $user->mobile_code = $mobile_code; |
||
| 237 | $user->mobile = $request->input('mobile'); |
||
| 238 | $user->skype = $request->input('skype'); |
||
| 239 | $user->manager = $request->input('manager'); |
||
| 240 | $user->account_manager = $request->input('account_manager'); |
||
| 241 | $user->currency_symbol = $currency_symbol; |
||
| 242 | $user->ip = $location['ip']; |
||
| 243 | |||
| 244 | $user->save(); |
||
| 245 | if (emailSendingStatus() && ! $user->active) { |
||
| 246 | $this->sendWelcomeMail($user); |
||
| 247 | } |
||
| 248 | $mailchimpStatus = StatusSetting::first()->value('mailchimp_status'); |
||
| 249 | if ($mailchimpStatus == 1) { |
||
| 250 | $mailchimp = new \App\Http\Controllers\Common\MailChimpController(); |
||
| 251 | $r = $mailchimp->addSubscriber($user->email); |
||
| 252 | } |
||
| 253 | |||
| 254 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 255 | } catch (\Swift_TransportException $e) { |
||
| 256 | return redirect()->back()->with('warning', 'User has been created successfully |
||
| 257 | But email configuration has some problem!'); |
||
| 258 | } catch (\Exception $e) { |
||
| 259 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Display the specified resource. |
||
| 265 | * |
||
| 266 | * @param int $id |
||
| 267 | * |
||
| 268 | * @return \Response |
||
| 269 | */ |
||
| 270 | public function show($id) |
||
| 271 | { |
||
| 272 | try { |
||
| 273 | if (User::onlyTrashed()->find($id)) { |
||
| 274 | throw new \Exception('This user is suspended from system. Restore the user to view details.'); |
||
| 275 | } |
||
| 276 | $invoice = new Invoice(); |
||
| 277 | $order = new Order(); |
||
| 278 | $invoices = $invoice->where('user_id', $id)->orderBy('created_at', 'desc')->get(); |
||
| 279 | $invoiceSum = $this->getTotalInvoice($invoices); |
||
| 280 | $amountReceived = $this->getAmountPaid($id); |
||
| 281 | $pendingAmount = $invoiceSum - $amountReceived; |
||
| 282 | // $pendingAmount = $invoiceSum - $amountReceived; |
||
| 283 | // if ($pendingAmount < 0) { |
||
| 284 | // $pendingAmount = 0; |
||
| 285 | // } |
||
| 286 | $extraAmt = $this->getExtraAmt($id); |
||
| 287 | $client = $this->user->where('id', $id)->first(); |
||
| 288 | |||
| 289 | if (User::onlyTrashed()->find($id)) { |
||
| 290 | $client = User::onlyTrashed()->find($id); |
||
| 291 | } |
||
| 292 | |||
| 293 | $is2faEnabled = $client->is_2fa_enabled; |
||
| 294 | // $client = ""; |
||
| 295 | $currency = $client->currency; |
||
| 296 | $orders = $order->where('client', $id)->get(); |
||
| 297 | $comments = Comment::where('user_id', $client->id)->get(); |
||
| 298 | |||
| 299 | return view( |
||
| 300 | 'themes.default1.user.client.show', |
||
| 301 | compact('id','client','invoices','orders','invoiceSum','amountReceived','pendingAmount','currency','extraAmt','comments', |
||
| 302 | 'is2faEnabled') |
||
| 303 | ); |
||
| 304 | } catch (\Exception $ex) { |
||
| 305 | app('log')->info($ex->getMessage()); |
||
| 306 | Bugsnag::notifyException($ex); |
||
| 307 | |||
| 308 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Show the form for editing the specified resource. |
||
| 314 | * |
||
| 315 | * @param int $id |
||
| 316 | * |
||
| 317 | * @return \Response |
||
| 318 | */ |
||
| 319 | public function edit($id) |
||
| 320 | { |
||
| 321 | try { |
||
| 322 | $user = $this->user->where('id', $id)->first(); |
||
| 323 | $timezonesList = \App\Model\Common\Timezone::get(); |
||
| 324 | foreach ($timezonesList as $timezone) { |
||
| 325 | $location = $timezone->location; |
||
| 326 | if ($location) { |
||
| 327 | $start = strpos($location, '('); |
||
| 328 | $end = strpos($location, ')', $start + 1); |
||
| 329 | $length = $end - $start; |
||
| 330 | $result = substr($location, $start + 1, $length - 1); |
||
| 331 | $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | //for display |
||
| 335 | $timezones = array_column($display, 'name', 'id'); |
||
| 336 | $state = getStateByCode($user->state); |
||
| 337 | $managers = User::where('role', 'admin') |
||
| 338 | ->where('position', 'manager') |
||
| 339 | ->pluck('first_name', 'id')->toArray(); |
||
| 340 | $acc_managers = User::where('role', 'admin') |
||
| 341 | ->where('position', 'account_manager') |
||
| 342 | ->pluck('first_name', 'id')->toArray(); |
||
| 343 | $selectedCurrency = Currency::where('code', $user->currency) |
||
| 344 | ->pluck('name', 'code')->toArray(); |
||
| 345 | $selectedCompany = \DB::table('company_types')->where('name', $user->company_type) |
||
| 346 | ->pluck('name', 'short')->toArray(); |
||
| 347 | $selectedIndustry = \App\Model\Common\Bussiness::where('name', $user->bussiness) |
||
| 348 | ->pluck('name', 'short')->toArray(); |
||
| 349 | $selectedCompanySize = \DB::table('company_sizes')->where('short', $user->company_size) |
||
| 350 | ->pluck('name', 'short')->toArray(); |
||
| 351 | $states = findStateByRegionId($user->country); |
||
| 352 | |||
| 353 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
| 354 | |||
| 355 | return view( |
||
| 356 | 'themes.default1.user.client.edit', |
||
| 357 | compact( |
||
| 358 | 'bussinesses', |
||
| 359 | 'user', |
||
| 360 | 'timezones', |
||
| 361 | 'state', |
||
| 362 | 'states', |
||
| 363 | 'managers', |
||
| 364 | 'selectedCurrency', |
||
| 365 | 'selectedCompany', |
||
| 366 | 'selectedIndustry', |
||
| 367 | 'selectedCompanySize', |
||
| 368 | 'acc_managers' |
||
| 369 | ) |
||
| 370 | ); |
||
| 371 | } catch (\Exception $ex) { |
||
| 372 | app('log')->error($ex->getMessage()); |
||
| 373 | |||
| 374 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Update the specified resource in storage. |
||
| 380 | * |
||
| 381 | * @param int $id |
||
| 382 | * |
||
| 383 | * @return \Response |
||
| 384 | */ |
||
| 385 | public function update($id, ClientRequest $request) |
||
| 386 | { |
||
| 387 | try { |
||
| 388 | $user = $this->user->where('id', $id)->first(); |
||
| 389 | $symbol = Currency::where('code', $request->input('currency'))->pluck('symbol')->first(); |
||
| 390 | $user->currency_symbol = $symbol; |
||
| 391 | $user->fill($request->input())->save(); |
||
| 392 | // \Session::put('test', 1000); |
||
| 393 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 394 | } catch (\Exception $ex) { |
||
| 395 | app('log')->error($ex->getMessage()); |
||
| 396 | Bugsnag::notifyException($ex); |
||
| 397 | |||
| 398 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Remove the specified resource from storage. |
||
| 404 | * |
||
| 405 | * @param int $id |
||
| 406 | * |
||
| 407 | * @return \Response |
||
| 408 | */ |
||
| 409 | public function destroy(Request $request) |
||
| 463 | </div>'; |
||
| 464 | } |
||
| 465 | } |
||
| 466 | |||
| 467 | public function sendWelcomeMail($user) |
||
| 468 | { |
||
| 469 | $activate_model = new AccountActivate(); |
||
| 470 | $str = str_random(40); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Gets baseQuery for user search by appending all the allowed filters. |
||
| 499 | * @param $request |
||
| 500 | * @return mixed |
||
| 501 | */ |
||
| 502 | private function getBaseQueryForUserSearch(Request $request) |
||
| 528 | } |
||
| 529 | } |
||
| 530 |