| Total Complexity | 64 |
| Total Lines | 450 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PageController 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 PageController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class PageController extends Controller |
||
| 10 | { |
||
| 11 | public $page; |
||
| 12 | |||
| 13 | public function __construct() |
||
| 14 | { |
||
| 15 | $this->middleware('auth'); |
||
| 16 | $this->middleware('admin'); |
||
| 17 | |||
| 18 | $page = new FrontendPage(); |
||
| 19 | $this->page = $page; |
||
| 20 | } |
||
| 21 | |||
| 22 | public function index() |
||
| 23 | { |
||
| 24 | try { |
||
| 25 | return view('themes.default1.front.page.index'); |
||
| 26 | } catch (\Exception $ex) { |
||
| 27 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | public function getPages() |
||
| 32 | { |
||
| 33 | return \DataTables::of($this->page->get()) |
||
| 34 | ->addColumn('checkbox', function ($model) { |
||
| 35 | return "<input type='checkbox' class='page_checkbox' value=".$model->id.' name=select[] id=check>'; |
||
| 36 | }) |
||
| 37 | ->addColumn('name', function ($model) { |
||
| 38 | return ucfirst($model->name); |
||
| 39 | }) |
||
| 40 | ->addColumn('url', function ($model) { |
||
| 41 | return $model->url; |
||
| 42 | }) |
||
| 43 | ->addColumn('created_at', function ($model) { |
||
| 44 | return $model->created_at; |
||
| 45 | }) |
||
| 46 | |||
| 47 | ->addColumn('content', function ($model) { |
||
| 48 | return str_limit($model->content, 10, '...'); |
||
| 49 | }) |
||
| 50 | ->addColumn('action', function ($model) { |
||
| 51 | return '<a href='.url('pages/'.$model->id.'/edit')." class='btn btn-sm btn-primary'>Edit</a>"; |
||
| 52 | }) |
||
| 53 | |||
| 54 | ->rawColumns(['checkbox', 'name', 'url', 'created_at', 'content', 'action']) |
||
| 55 | ->make(true); |
||
| 56 | // ->searchColumns('name', 'content') |
||
| 57 | // ->orderColumns('name') |
||
| 58 | // ->make(); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function create() |
||
| 62 | { |
||
| 63 | try { |
||
| 64 | $parents = $this->page->pluck('name', 'id')->toArray(); |
||
| 65 | |||
| 66 | return view('themes.default1.front.page.create', compact('parents')); |
||
| 67 | } catch (\Exception $ex) { |
||
| 68 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | public function edit($id) |
||
| 73 | { |
||
| 74 | try { |
||
| 75 | $page = $this->page->where('id', $id)->first(); |
||
| 76 | $parents = $this->page->where('id', '!=', $id)->pluck('name', 'id')->toArray(); |
||
| 77 | |||
| 78 | return view('themes.default1.front.page.edit', compact('parents', 'page')); |
||
| 79 | } catch (\Exception $ex) { |
||
| 80 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | public function store(Request $request) |
||
| 85 | { |
||
| 86 | $this->validate($request, [ |
||
| 87 | 'name' => 'required', |
||
| 88 | 'publish' => 'required', |
||
| 89 | 'slug' => 'required', |
||
| 90 | 'url' => 'required', |
||
| 91 | 'content' => 'required', |
||
| 92 | ]); |
||
| 93 | |||
| 94 | try { |
||
| 95 | $this->page->fill($request->input())->save(); |
||
| 96 | |||
| 97 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 98 | } catch (\Exception $ex) { |
||
| 99 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | public function update($id, Request $request) |
||
| 104 | { |
||
| 105 | $this->validate($request, [ |
||
| 106 | 'name' => 'required', |
||
| 107 | 'publish' => 'required', |
||
| 108 | 'slug' => 'required', |
||
| 109 | 'url' => 'required', |
||
| 110 | 'content' => 'required', |
||
| 111 | ]); |
||
| 112 | |||
| 113 | try { |
||
| 114 | $page = $this->page->where('id', $id)->first(); |
||
| 115 | $page->fill($request->input())->save(); |
||
| 116 | |||
| 117 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 118 | } catch (\Exception $ex) { |
||
| 119 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | public function getPageUrl($slug) |
||
| 124 | { |
||
| 125 | $productController = new \App\Http\Controllers\Product\ProductController(); |
||
| 126 | $url = $productController->GetMyUrl(); |
||
| 127 | $segment = $this->addSegment(['public/pages']); |
||
| 128 | $url = $url.$segment; |
||
| 129 | |||
| 130 | $slug = str_slug($slug, '-'); |
||
| 131 | echo $url.'/'.$slug; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getSlug($slug) |
||
| 138 | } |
||
| 139 | |||
| 140 | public function addSegment($segments = []) |
||
| 141 | { |
||
| 142 | $segment = ''; |
||
| 143 | foreach ($segments as $seg) { |
||
| 144 | $segment .= '/'.$seg; |
||
| 145 | } |
||
| 146 | |||
| 147 | return $segment; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function generate(Request $request) |
||
| 151 | { |
||
| 152 | // dd($request->all()); |
||
| 153 | if ($request->has('slug')) { |
||
| 154 | $slug = $request->input('slug'); |
||
| 155 | |||
| 156 | return $this->getSlug($slug); |
||
|
|
|||
| 157 | } |
||
| 158 | if ($request->has('url')) { |
||
| 159 | $slug = $request->input('url'); |
||
| 160 | |||
| 161 | return $this->getPageUrl($slug); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | public function show($slug) |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Remove the specified resource from storage. |
||
| 181 | * |
||
| 182 | * @param int $id |
||
| 183 | * |
||
| 184 | * @return \Response |
||
| 185 | */ |
||
| 186 | public function destroy(Request $request) |
||
| 187 | { |
||
| 188 | try { |
||
| 189 | $ids = $request->input('select'); |
||
| 190 | if (!empty($ids)) { |
||
| 191 | foreach ($ids as $id) { |
||
| 192 | $page = $this->page->where('id', $id)->first(); |
||
| 193 | if ($page) { |
||
| 194 | // dd($page); |
||
| 195 | $page->delete(); |
||
| 196 | } else { |
||
| 197 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 198 | <i class='fa fa-ban'></i> |
||
| 199 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */ |
||
| 200 | \Lang::get('message.failed').' |
||
| 201 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 202 | './* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
||
| 203 | </div>'; |
||
| 204 | //echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 208 | <i class='fa fa-ban'></i> |
||
| 209 | |||
| 210 | <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */ |
||
| 211 | \Lang::get('message.success').' |
||
| 212 | |||
| 213 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 214 | './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
||
| 215 | </div>'; |
||
| 216 | } else { |
||
| 217 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 218 | <i class='fa fa-ban'></i> |
||
| 219 | <b>".\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 220 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 221 | './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
||
| 222 | </div>'; |
||
| 223 | //echo \Lang::get('message.select-a-row'); |
||
| 224 | } |
||
| 225 | } catch (\Exception $e) { |
||
| 226 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 227 | <i class='fa fa-ban'></i> |
||
| 228 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 229 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 230 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 231 | '.$e->getMessage().' |
||
| 232 | </div>'; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | public function search(Request $request) |
||
| 237 | { |
||
| 238 | try { |
||
| 239 | $search = $request->input('q'); |
||
| 240 | $model = $this->result($search, $this->page); |
||
| 241 | |||
| 242 | return view('themes.default1.front.page.search', compact('model')); |
||
| 243 | } catch (\Exception $ex) { |
||
| 244 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | public function result($search, $model) |
||
| 249 | { |
||
| 250 | try { |
||
| 251 | $model = $model->where('name', 'like', '%'.$search.'%')->orWhere('content', 'like', '%'.$search.'%')->paginate(10); |
||
| 252 | |||
| 253 | return $model->setPath('search'); |
||
| 254 | } catch (\Exception $ex) { |
||
| 255 | //dd($ex); |
||
| 256 | throw new \Exception('Can not get the search result'); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | public function transform($type, $data, $trasform = []) |
||
| 276 | } |
||
| 277 | |||
| 278 | public function checkString($data, $string) |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | |||
| 286 | public function getLocation() |
||
| 287 | { |
||
| 288 | if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet |
||
| 289 | $ip = $_SERVER['HTTP_CLIENT_IP']; |
||
| 290 | } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy |
||
| 291 | $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
||
| 292 | } else { |
||
| 293 | $ip = $_SERVER['REMOTE_ADDR']; |
||
| 294 | } |
||
| 295 | |||
| 296 | if ($ip != '::1') { |
||
| 297 | $location = json_decode(file_get_contents('http://ip-api.com/json/'.$ip), true); |
||
| 298 | } else { |
||
| 299 | $location = json_decode(file_get_contents('http://ip-api.com/json'), true); |
||
| 300 | } |
||
| 301 | return $location; |
||
| 302 | } |
||
| 303 | |||
| 304 | |||
| 305 | public function getCurrency($location) |
||
| 306 | { |
||
| 307 | if ($location['country'] == 'India') { |
||
| 308 | $currency = 'INR'; |
||
| 309 | } else { |
||
| 310 | $currency = 'USD'; |
||
| 311 | } |
||
| 312 | if (\Auth::user()) { |
||
| 313 | $currency = 'INR'; |
||
| 314 | $user_currency = \Auth::user()->currency; |
||
| 315 | if ($user_currency == 1 || $user_currency == 'USD') { |
||
| 316 | $currency = 'USD'; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | return $currency; |
||
| 320 | } |
||
| 321 | |||
| 322 | public function cart() |
||
| 323 | { |
||
| 324 | $location = $this->getLocation(); |
||
| 325 | $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']); |
||
| 326 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']); |
||
| 327 | $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray(); |
||
| 328 | $state_code = $location['countryCode'].'-'.$location['region']; |
||
| 329 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 330 | $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']); |
||
| 331 | $currency=$this->getCurrency($location); |
||
| 332 | |||
| 333 | \Session::put('currency', $currency); |
||
| 334 | if (!\Session::has('currency')) { |
||
| 335 | \Session::put('currency', 'INR'); |
||
| 336 | } |
||
| 337 | $pages = $this->page->find(1); |
||
| 338 | $data = $pages->content; |
||
| 339 | |||
| 340 | $product = new \App\Model\Product\Product(); |
||
| 341 | $helpdesk_products = $product->where('id', '!=', 1)->where('category', '=', 'helpdesk')->get()->toArray(); |
||
| 342 | $temp_controller = new \App\Http\Controllers\Common\TemplateController(); |
||
| 343 | $trasform = []; |
||
| 344 | $template = $this->getHelpdeskTemplate($helpdesk_products,$data,$trasform); |
||
| 345 | $sevice_desk_products = $product->where('id', '!=', 1)->where('category', '=', 'servicedesk')->get()->toArray(); |
||
| 346 | $trasform1 = []; |
||
| 347 | $servicedesk_template =$this->getServiceDeskdeskTemplate($sevice_desk_products,$data,$trasform1); |
||
| 348 | |||
| 349 | $service = $product->where('id', '!=', 1)->where('category', '=', 'service')->get()->toArray(); |
||
| 350 | $trasform2 = []; |
||
| 351 | $service_template = $this->getServiceTemplate($service,$data,$trasform2); |
||
| 352 | |||
| 353 | |||
| 354 | |||
| 355 | return view('themes.default1.common.template.shoppingcart', compact('template', 'trasform', 'servicedesk_template', 'trasform1', 'service_template', 'trasform2')); |
||
| 356 | } |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * Get Template For Helpdsk Products |
||
| 361 | */ |
||
| 362 | public function getHelpdeskTemplate($helpdesk_products,$data,$trasform) |
||
| 363 | { |
||
| 364 | $temp_controller = new \App\Http\Controllers\Common\TemplateController(); |
||
| 365 | if (count($helpdesk_products) > 0) { |
||
| 366 | foreach ($helpdesk_products as $key => $value) { |
||
| 367 | $trasform[$value['id']]['price'] = $temp_controller->leastAmount($value['id']); |
||
| 368 | $trasform[$value['id']]['name'] = $value['name']; |
||
| 369 | $trasform[$value['id']]['feature'] = $value['description']; |
||
| 370 | $trasform[$value['id']]['subscription'] = $temp_controller->plans($value['shoping_cart_link'], $value['id']); |
||
| 371 | $trasform[$value['id']]['url'] = "<input type='submit' value='Buy' class='btn btn-primary'></form>"; |
||
| 372 | } |
||
| 373 | $template = $this->transform('cart', $data, $trasform); |
||
| 374 | }else{ |
||
| 375 | $template = ''; |
||
| 376 | } |
||
| 377 | return $template; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get Template For Service Desk Products |
||
| 382 | */ |
||
| 383 | public function getServiceDeskdeskTemplate($sevice_desk_products,$data,$trasform1) |
||
| 384 | { |
||
| 385 | $temp_controller = new \App\Http\Controllers\Common\TemplateController(); |
||
| 386 | if (count($sevice_desk_products) > 0) { |
||
| 387 | foreach ($sevice_desk_products as $key => $value) { |
||
| 388 | $trasform1[$value['id']]['price'] = $temp_controller->leastAmount($value['id']); |
||
| 389 | $trasform1[$value['id']]['name'] = $value['name']; |
||
| 390 | $trasform1[$value['id']]['feature'] = $value['description']; |
||
| 391 | $trasform1[$value['id']]['subscription'] = $temp_controller->plans($value['shoping_cart_link'], $value['id']); |
||
| 392 | |||
| 393 | $trasform1[$value['id']]['url'] = "<input type='submit' value='Buy' class='btn btn-primary'></form>"; |
||
| 394 | } |
||
| 395 | $servicedesk_template = $this->transform('cart', $data, $trasform1); |
||
| 396 | } else{ |
||
| 397 | $servicedesk_template = ''; |
||
| 398 | } |
||
| 399 | return $servicedesk_template; |
||
| 400 | } |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * Get Template For Services |
||
| 405 | */ |
||
| 406 | public function getServiceTemplate($service,$data,$trasform2) |
||
| 407 | { |
||
| 408 | $temp_controller = new \App\Http\Controllers\Common\TemplateController(); |
||
| 409 | if (count($service) > 0) { |
||
| 410 | foreach ($service as $key => $value) { |
||
| 411 | $trasform2[$value['id']]['price'] = $temp_controller->leastAmountService($value['id']); |
||
| 412 | $trasform2[$value['id']]['name'] = $value['name']; |
||
| 413 | $trasform2[$value['id']]['feature'] = $value['description']; |
||
| 414 | $trasform2[$value['id']]['subscription'] = $temp_controller->plans($value['shoping_cart_link'], $value['id']); |
||
| 415 | |||
| 416 | $trasform2[$value['id']]['url'] = "<input type='submit' value='Buy' class='btn btn-primary'></form>"; |
||
| 417 | } |
||
| 418 | $service_template = $this->transform('cart', $data, $trasform2); |
||
| 419 | } |
||
| 420 | else{ |
||
| 421 | $service_template = ''; |
||
| 422 | } |
||
| 423 | return $service_template; |
||
| 424 | } |
||
| 425 | |||
| 426 | public function checkConfigKey($config, $transform) |
||
| 439 | } |
||
| 440 | |||
| 441 | public function keyArray($array) |
||
| 449 | } |
||
| 450 | |||
| 451 | public function valueArray($array) |
||
| 452 | { |
||
| 453 | $result = []; |
||
| 459 | } |
||
| 460 | } |
||
| 461 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.