| Total Complexity | 44 |
| Total Lines | 371 |
| 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 |
||
| 12 | class PageController extends GetPageTemplateController |
||
| 13 | { |
||
| 14 | public $page; |
||
| 15 | |||
| 16 | public function __construct() |
||
| 17 | { |
||
| 18 | // $this->middleware('auth'); |
||
| 19 | // $this->middleware('admin'); |
||
| 20 | |||
| 21 | $page = new FrontendPage(); |
||
| 22 | $this->page = $page; |
||
| 23 | } |
||
| 24 | |||
| 25 | public function index() |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | public function getLocation() |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | public function getPages() |
||
| 82 | // ->searchColumns('name', 'content') |
||
| 83 | // ->orderColumns('name') |
||
| 84 | // ->make(); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function create() |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | public function edit($id) |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | public function store(Request $request) |
||
| 120 | { |
||
| 121 | $this->validate($request, [ |
||
| 122 | 'name' => 'required', |
||
| 123 | 'publish' => 'required', |
||
| 124 | 'slug' => 'required', |
||
| 125 | 'url' => 'required', |
||
| 126 | 'content' => 'required', |
||
| 127 | ]); |
||
| 128 | |||
| 129 | try { |
||
| 130 | $url = $request->input('url'); |
||
| 131 | if ($request->input('type') =='contactus') { |
||
| 132 | $url = url('/contact-us'); |
||
| 133 | } |
||
| 134 | $this->page->name = $request->input('name'); |
||
| 135 | $this->page->publish = $request->input('publish'); |
||
| 136 | $this->page->slug = $request->input('slug'); |
||
| 137 | $this->page->url = $url; |
||
| 138 | $this->page->parent_page_id = $request->input('parent_page_id'); |
||
| 139 | $this->page->type = $request->input('type'); |
||
| 140 | $this->page->content = $request->input('content'); |
||
| 141 | $this->page->save(); |
||
| 142 | |||
| 143 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 144 | } catch (\Exception $ex) { |
||
| 145 | app('log')->error($ex->getMessage()); |
||
| 146 | Bugsnag::notifyException($ex); |
||
| 147 | |||
| 148 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | public function update($id, Request $request) |
||
| 153 | { |
||
| 154 | $this->validate($request, [ |
||
| 155 | 'name' => 'required', |
||
| 156 | 'publish' => 'required', |
||
| 157 | 'slug' => 'required', |
||
| 158 | 'url' => 'required', |
||
| 159 | 'content' => 'required', |
||
| 160 | 'created_at' => 'required', |
||
| 161 | ]); |
||
| 162 | |||
| 163 | try { |
||
| 164 | if($request->input('default_page_id') != '') { |
||
| 165 | $page = $this->page->where('id', $id)->first(); |
||
| 166 | $page->fill($request->except('created_at'))->save(); |
||
| 167 | $date = \DateTime::createFromFormat('d/m/Y', $request->input('created_at')); |
||
| 168 | $page->created_at = $date->format('Y-m-d H:i:s'); |
||
| 169 | $page->save(); |
||
| 170 | $defaultUrl = $this->page->where('id', $request->input('default_page_id'))->pluck('url')->first(); |
||
| 171 | DefaultPage::find(1)->update(['page_id'=>$request->input('default_page_id'), 'page_url'=>$defaultUrl]); |
||
| 172 | } else { |
||
| 173 | DefaultPage::find(1)->update(['page_id'=>1, 'page_url'=>url('my-invoices')]); |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 178 | } catch (\Exception $ex) { |
||
| 179 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getPageUrl($slug) |
||
| 184 | { |
||
| 185 | $productController = new \App\Http\Controllers\Product\ProductController(); |
||
| 186 | // $url = url('/'); |
||
| 187 | // $segment = $this->addSegment(['public/pages']); |
||
| 188 | $url = url('/'); |
||
| 189 | |||
| 190 | $slug = str_slug($slug, '-'); |
||
| 191 | echo $url.'/pages'.'/'.$slug; |
||
| 192 | } |
||
| 193 | |||
| 194 | public function getSlug($slug) |
||
| 195 | { |
||
| 196 | $slug = str_slug($slug, '-'); |
||
| 197 | echo $slug; |
||
| 198 | } |
||
| 199 | |||
| 200 | public function addSegment($segments = []) |
||
| 201 | { |
||
| 202 | $segment = ''; |
||
| 203 | foreach ($segments as $seg) { |
||
| 204 | $segment .= '/'.$seg; |
||
| 205 | } |
||
| 206 | |||
| 207 | return $segment; |
||
| 208 | } |
||
| 209 | |||
| 210 | public function generate(Request $request) |
||
| 211 | { |
||
| 212 | // dd($request->all()); |
||
| 213 | if ($request->has('slug')) { |
||
| 214 | $slug = $request->input('slug'); |
||
| 215 | |||
| 216 | return $this->getSlug($slug); |
||
| 217 | } |
||
| 218 | if ($request->has('url')) { |
||
| 219 | $slug = $request->input('url'); |
||
| 220 | |||
| 221 | return $this->getPageUrl($slug); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | public function show($slug) |
||
| 226 | { |
||
| 227 | try { |
||
| 228 | $page = $this->page->where('slug', $slug)->where('publish', 1)->first(); |
||
| 229 | if ($page && $page->type == 'cart') { |
||
| 230 | return $this->cart(); |
||
| 231 | } |
||
| 232 | |||
| 233 | return view('themes.default1.front.page.show', compact('page')); |
||
| 234 | } catch (\Exception $ex) { |
||
| 235 | return redirect()->back()->with('fails', $ex->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 | $ids = $request->input('select'); |
||
| 250 | $defaultPageId = DefaultPage::pluck('page_id')->first(); |
||
| 251 | if (!empty($ids)) { |
||
| 252 | foreach ($ids as $id) { |
||
| 253 | if ($id != $defaultPageId) { |
||
| 254 | $page = $this->page->where('id', $id)->first(); |
||
| 255 | if ($page) { |
||
| 256 | // dd($page); |
||
| 257 | $page->delete(); |
||
| 258 | } else { |
||
| 259 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 260 | <i class='fa fa-ban'></i> |
||
| 261 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 262 | /* @scrutinizer ignore-type */ |
||
| 263 | \Lang::get('message.failed').' |
||
| 264 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 265 | './* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
||
| 266 | </div>'; |
||
| 267 | //echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
||
| 268 | } |
||
| 269 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 270 | <i class='fa fa-ban'></i> |
||
| 271 | |||
| 272 | <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> '. |
||
| 273 | /* @scrutinizer ignore-type */ |
||
| 274 | \Lang::get('message.success').' |
||
| 275 | |||
| 276 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 277 | './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
||
| 278 | </div>'; |
||
| 279 | } else { |
||
| 280 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 281 | <i class='fa fa-ban'></i> |
||
| 282 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 283 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 284 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 285 | './* @scrutinizer ignore-type */ \Lang::get('message.can-not-delete-default-page').' |
||
| 286 | </div>'; |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } else { |
||
| 290 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 291 | <i class='fa fa-ban'></i> |
||
| 292 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 293 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 294 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 295 | './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
||
| 296 | </div>'; |
||
| 297 | //echo \Lang::get('message.select-a-row'); |
||
| 298 | } |
||
| 299 | } catch (\Exception $e) { |
||
| 300 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 301 | <i class='fa fa-ban'></i> |
||
| 302 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 303 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 304 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 305 | '.$e->getMessage().' |
||
| 306 | </div>'; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | public function search(Request $request) |
||
| 311 | { |
||
| 312 | try { |
||
| 313 | $search = $request->input('q'); |
||
| 314 | $model = $this->result($search, $this->page); |
||
| 315 | |||
| 316 | return view('themes.default1.front.page.search', compact('model')); |
||
| 317 | } catch (\Exception $ex) { |
||
| 318 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | public function transform($type, $data, $trasform = []) |
||
| 338 | } |
||
| 339 | |||
| 340 | public function checkString($data, $string) |
||
| 341 | { |
||
| 342 | if (strpos($data, $string) !== false) { |
||
| 343 | return true; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get Page Template when Group in Store Dropdown is |
||
| 349 | * selected on the basis of Group id. |
||
| 350 | * |
||
| 351 | * @author Ashutosh Pathak <[email protected]> |
||
| 352 | * |
||
| 353 | * @date 2019-01-10T01:20:52+0530 |
||
| 354 | * |
||
| 355 | * @param int $groupid Group id |
||
| 356 | * @param int $templateid Id of the Template |
||
| 357 | * |
||
| 358 | * @return longtext The Template to be displayed |
||
| 359 | */ |
||
| 360 | public function pageTemplates(int $templateid, int $groupid) |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 |