| Total Complexity | 42 |
| Total Lines | 518 |
| 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 AdvanceSearchController |
||
| 22 | { |
||
| 23 | use PaymentsAndInvoices; |
||
|
|
|||
| 24 | |||
| 25 | public $user; |
||
| 26 | public $activate; |
||
| 27 | public $product; |
||
| 28 | |||
| 29 | public function __construct() |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Display a listing of the resource. |
||
| 45 | * |
||
| 46 | * @return \Response |
||
| 47 | */ |
||
| 48 | public function index(Request $request) |
||
| 49 | { |
||
| 50 | $name = $request->input('name'); |
||
| 51 | $username = $request->input('username'); |
||
| 52 | $company = $request->input('company'); |
||
| 53 | $mobile = $request->input('mobile'); |
||
| 54 | $email = $request->input('email'); |
||
| 55 | $country = $request->input('country'); |
||
| 56 | $industry = $request->input('industry'); |
||
| 57 | $company_type = $request->input('company_type'); |
||
| 58 | $company_size = $request->input('company_size'); |
||
| 59 | $role = $request->input('role'); |
||
| 60 | $position = $request->input('position'); |
||
| 61 | $reg_from = $request->input('reg_from'); |
||
| 62 | $reg_till = $request->input('reg_till'); |
||
| 63 | |||
| 64 | return view( |
||
| 65 | 'themes.default1.user.client.index', |
||
| 66 | compact( |
||
| 67 | 'name', |
||
| 68 | 'username', |
||
| 69 | 'company', |
||
| 70 | 'mobile', |
||
| 71 | 'email', |
||
| 72 | 'country', |
||
| 73 | 'industry', |
||
| 74 | 'company_type', |
||
| 75 | 'company_size', |
||
| 76 | 'role', |
||
| 77 | 'position', |
||
| 78 | 'reg_from', |
||
| 79 | 'reg_till' |
||
| 80 | ) |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get Clients for yajra datatable. |
||
| 86 | */ |
||
| 87 | public function getClients(Request $request) |
||
| 171 | |||
| 172 | // ->searchColumns('email', 'first_name') |
||
| 173 | // ->orderColumns('email', 'first_name', 'created_at') |
||
| 174 | // ->make(); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Show the form for creating a new resource. |
||
| 179 | * |
||
| 180 | * @return \Response |
||
| 181 | */ |
||
| 182 | public function create() |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Store a newly created resource in storage. |
||
| 207 | * |
||
| 208 | * @return \Response |
||
| 209 | */ |
||
| 210 | public function store(ClientRequest $request) |
||
| 211 | { |
||
| 212 | try { |
||
| 213 | $user = $this->user; |
||
| 214 | $str = 'demopass'; |
||
| 215 | $password = \Hash::make($str); |
||
| 216 | $user->password = $password; |
||
| 217 | $cont = new \App\Http\Controllers\Front\PageController(); |
||
| 218 | $location = $cont->getLocation(); |
||
| 219 | $user->ip = $location['ip']; |
||
| 220 | $user->fill($request->input())->save(); |
||
| 221 | $this->sendWelcomeMail($user); |
||
| 222 | $mailchimpStatus = StatusSetting::first()->value('mailchimp_status'); |
||
| 223 | if ($mailchimpStatus == 1) { |
||
| 224 | $mailchimp = new \App\Http\Controllers\Common\MailChimpController(); |
||
| 225 | $r = $mailchimp->addSubscriber($user->email); |
||
| 226 | } |
||
| 227 | |||
| 228 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 229 | } catch (\Swift_TransportException $e) { |
||
| 230 | return redirect()->back()->with('warning', 'User has been created successfully |
||
| 231 | But email configuration has some problem!'); |
||
| 232 | } catch (\Exception $e) { |
||
| 233 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Display the specified resource. |
||
| 239 | * |
||
| 240 | * @param int $id |
||
| 241 | * |
||
| 242 | * @return \Response |
||
| 243 | */ |
||
| 244 | public function show($id) |
||
| 245 | { |
||
| 246 | try { |
||
| 247 | $invoice = new Invoice(); |
||
| 248 | $order = new Order(); |
||
| 249 | $invoices = $invoice->where('user_id', $id)->orderBy('created_at', 'desc')->get(); |
||
| 250 | $invoiceSum = $this->getTotalInvoice($invoices); |
||
| 251 | $amountReceived = $this->getAmountPaid($id); |
||
| 252 | $pendingAmount = $invoiceSum - $amountReceived; |
||
| 253 | // $pendingAmount = $invoiceSum - $amountReceived; |
||
| 254 | // if ($pendingAmount < 0) { |
||
| 255 | // $pendingAmount = 0; |
||
| 256 | // } |
||
| 257 | $extraAmt = $this->getExtraAmt($id); |
||
| 258 | $client = $this->user->where('id', $id)->first(); |
||
| 259 | |||
| 260 | // $client = ""; |
||
| 261 | $currency = $client->currency; |
||
| 262 | $orders = $order->where('client', $id)->get(); |
||
| 263 | $comments = $client->comments()->where('user_id', $client->id)->get(); |
||
| 264 | |||
| 265 | return view( |
||
| 266 | 'themes.default1.user.client.show', |
||
| 267 | compact( |
||
| 268 | 'id', |
||
| 269 | 'client', |
||
| 270 | 'invoices', |
||
| 271 | 'model_popup', |
||
| 272 | 'orders', |
||
| 273 | 'payments', |
||
| 274 | 'invoiceSum', |
||
| 275 | 'amountReceived', |
||
| 276 | 'pendingAmount', |
||
| 277 | 'currency', |
||
| 278 | 'extraAmt', |
||
| 279 | 'comments' |
||
| 280 | ) |
||
| 281 | ); |
||
| 282 | } catch (\Exception $ex) { |
||
| 283 | app('log')->info($ex->getMessage()); |
||
| 284 | Bugsnag::notifyException($ex); |
||
| 285 | |||
| 286 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | public function getOrderDetail($id) |
||
| 291 | { |
||
| 292 | $client = $this->user->where('id', $id)->first(); |
||
| 293 | $responseData = []; |
||
| 294 | foreach ($client->order()->orderBy('created_at', 'desc')->get() as $order) { |
||
| 295 | $date = $order->created_at; |
||
| 296 | $productName = $order->product()->first() && $order->product()->first()->name ? |
||
| 297 | $order->product()->first()->name : 'Unknown'; |
||
| 298 | $number = $order->number; |
||
| 299 | $price = $order->price_override; |
||
| 300 | $status = $order->order_status; |
||
| 301 | array_push($responseData, (['date'=> $date, 'productName'=>$productName, |
||
| 302 | 'number' => $number, 'price' =>$price, 'status'=>$status, ])); |
||
| 303 | } |
||
| 304 | |||
| 305 | return $responseData; |
||
| 306 | } |
||
| 307 | |||
| 308 | //Get Payment Details on Invoice Page |
||
| 309 | public function getPaymentDetail($id) |
||
| 310 | { |
||
| 311 | $client = $this->user->where('id', $id)->first(); |
||
| 312 | $invoice = new Invoice(); |
||
| 313 | $invoices = $invoice->where('user_id', $id)->orderBy('created_at', 'desc')->get(); |
||
| 314 | $extraAmt = $this->getExtraAmtPaid($id); |
||
| 315 | $date = ''; |
||
| 316 | $responseData = []; |
||
| 317 | if ($invoices) { |
||
| 318 | foreach ($client->payment()->orderBy('created_at', 'desc')->get() as $payment) { |
||
| 319 | $number = $payment->invoice()->first() ? $payment->invoice()->first()->number : '--'; |
||
| 320 | $date = $payment->created_at; |
||
| 321 | $date1 = new DateTime($date); |
||
| 322 | $tz = \Auth::user()->timezone()->first()->name; |
||
| 323 | $date1->setTimezone(new DateTimeZone($tz)); |
||
| 324 | $date = $date1->format('M j, Y, g:i a '); |
||
| 325 | $pay_method = $payment->payment_method; |
||
| 326 | if ($payment->invoice_id == 0) { |
||
| 327 | $amount = $extraAmt; |
||
| 328 | } else { |
||
| 329 | $amount = $payment->amount; |
||
| 330 | } |
||
| 331 | $status = ucfirst($payment->payment_status); |
||
| 332 | array_push($responseData, (['number'=>$number, 'pay_method'=>$pay_method, 'amount'=>$amount, 'status'=>$status, 'date'=>$date])); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | return $responseData; |
||
| 337 | } |
||
| 338 | |||
| 339 | public function getClientDetail($id) |
||
| 351 | } |
||
| 352 | |||
| 353 | public function getExtraAmt($userId) |
||
| 354 | { |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * Show the form for editing the specified resource. |
||
| 378 | * |
||
| 379 | * @param int $id |
||
| 380 | * |
||
| 381 | * @return \Response |
||
| 382 | */ |
||
| 383 | public function edit($id) |
||
| 384 | { |
||
| 385 | try { |
||
| 386 | $user = $this->user->where('id', $id)->first(); |
||
| 387 | $timezonesList = \App\Model\Common\Timezone::get(); |
||
| 388 | foreach ($timezonesList as $timezone) { |
||
| 389 | $location = $timezone->location; |
||
| 390 | if ($location) { |
||
| 391 | $start = strpos($location, '('); |
||
| 392 | $end = strpos($location, ')', $start + 1); |
||
| 393 | $length = $end - $start; |
||
| 394 | $result = substr($location, $start + 1, $length - 1); |
||
| 395 | $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | //for display |
||
| 399 | $timezones = array_column($display, 'name', 'id'); |
||
| 400 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state); |
||
| 401 | $managers = User::where('role', 'admin') |
||
| 402 | ->where('position', 'manager') |
||
| 403 | ->pluck('first_name', 'id')->toArray(); |
||
| 404 | $selectedCurrency = Currency::where('code', $user->currency) |
||
| 405 | ->pluck('name', 'code')->toArray(); |
||
| 406 | $selectedCompany = \DB::table('company_types')->where('short', $user->company_type) |
||
| 407 | ->pluck('name', 'short')->toArray(); |
||
| 408 | $selectedIndustry = \App\Model\Common\Bussiness::where('short', $user->bussiness) |
||
| 409 | ->pluck('name', 'short')->toArray(); |
||
| 410 | $selectedCompanySize = \DB::table('company_sizes')->where('short', $user->company_size) |
||
| 411 | ->pluck('name', 'short')->toArray(); |
||
| 412 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country); |
||
| 413 | |||
| 414 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
| 415 | |||
| 416 | return view( |
||
| 417 | 'themes.default1.user.client.edit', |
||
| 418 | compact( |
||
| 419 | 'bussinesses', |
||
| 420 | 'user', |
||
| 421 | 'timezones', |
||
| 422 | 'state', |
||
| 423 | 'states', |
||
| 424 | 'managers', |
||
| 425 | 'selectedCurrency', |
||
| 426 | 'selectedCompany', |
||
| 427 | 'selectedIndustry', |
||
| 428 | 'selectedCompanySize' |
||
| 429 | ) |
||
| 430 | ); |
||
| 431 | } catch (\Exception $ex) { |
||
| 432 | app('log')->error($ex->getMessage()); |
||
| 433 | |||
| 434 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Update the specified resource in storage. |
||
| 440 | * |
||
| 441 | * @param int $id |
||
| 442 | * |
||
| 443 | * @return \Response |
||
| 444 | */ |
||
| 445 | public function update($id, Request $request) |
||
| 446 | { |
||
| 447 | try { |
||
| 448 | $user = $this->user->where('id', $id)->first(); |
||
| 449 | $symbol = Currency::where('code', $request->input('currency'))->pluck('symbol')->first(); |
||
| 450 | $user->currency_symbol = $symbol; |
||
| 451 | $user->fill($request->input())->save(); |
||
| 452 | |||
| 453 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 454 | } catch (\Exception $ex) { |
||
| 455 | app('log')->error($ex->getMessage()); |
||
| 456 | Bugsnag::notifyException($ex); |
||
| 457 | |||
| 458 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Remove the specified resource from storage. |
||
| 464 | * |
||
| 465 | * @param int $id |
||
| 466 | * |
||
| 467 | * @return \Response |
||
| 468 | */ |
||
| 469 | public function destroy(Request $request) |
||
| 504 | </div>'; |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | public function sendWelcomeMail($user) |
||
| 541 |