| Total Complexity | 43 |
| Total Lines | 297 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ArticleController 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 ArticleController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class ArticleController extends Controller |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 15 | */ |
||
| 16 | public function index() |
||
| 17 | { |
||
| 18 | $articles = Articles::orderBy('category','asc')->paginate(15); |
||
| 19 | $data = [ |
||
| 20 | 'articles' => $articles |
||
| 21 | ]; |
||
| 22 | |||
| 23 | return view('pages.article')->with('data', $data); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 28 | */ |
||
| 29 | public function create() |
||
| 30 | { |
||
| 31 | return view('pages.ext.add-article'); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param Request $request |
||
| 36 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 37 | * @throws \Illuminate\Validation\ValidationException |
||
| 38 | */ |
||
| 39 | public function store(Request $request) |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param $id |
||
| 90 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 91 | */ |
||
| 92 | public function show($id) |
||
| 93 | { |
||
| 94 | $article = Articles::find($id); |
||
| 95 | if(!$article) { |
||
| 96 | abort(404); |
||
| 97 | } |
||
| 98 | return view('pages.ext.view-article')->with('article', $article); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param $cat |
||
| 103 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 104 | */ |
||
| 105 | public function listByCat($cat) |
||
| 106 | { |
||
| 107 | $category = null; |
||
| 108 | |||
| 109 | switch ($cat) { |
||
| 110 | case "penyakit": |
||
| 111 | $category = "Penyakit"; |
||
| 112 | break; |
||
| 113 | case "obat": |
||
| 114 | $category = "Obat - obatan"; |
||
| 115 | break; |
||
| 116 | case "hidup-sehat": |
||
| 117 | $category = "Hidup Sehat"; |
||
| 118 | break; |
||
| 119 | case "keluarga": |
||
| 120 | $category = "Keluarga"; |
||
| 121 | break; |
||
| 122 | case "kesehatan": |
||
| 123 | $category = "Kesehatan"; |
||
| 124 | break; |
||
| 125 | } |
||
| 126 | |||
| 127 | $data = [ |
||
| 128 | 'articles' => Articles::where('category', $cat)->orderBy('title','asc')->get(), |
||
| 129 | 'category' => $category, |
||
| 130 | 'cat' => $cat |
||
| 131 | ]; |
||
| 132 | |||
| 133 | return view('articles')->with('data', $data); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function listByName($cat, $name) |
||
| 137 | { |
||
| 138 | $category = null; |
||
| 139 | |||
| 140 | switch ($cat) { |
||
| 141 | case "penyakit": |
||
| 142 | $category = "Penyakit"; |
||
| 143 | break; |
||
| 144 | case "obat": |
||
| 145 | $category = "Obat - obatan"; |
||
| 146 | break; |
||
| 147 | case "hidup-sehat": |
||
| 148 | $category = "Hidup Sehat"; |
||
| 149 | break; |
||
| 150 | case "keluarga": |
||
| 151 | $category = "Keluarga"; |
||
| 152 | break; |
||
| 153 | case "kesehatan": |
||
| 154 | $category = "Kesehatan"; |
||
| 155 | break; |
||
| 156 | } |
||
| 157 | $data = [ |
||
| 158 | 'articles' => Articles::where('category', $cat) |
||
| 159 | ->where('title','LIKE',$name.'%') |
||
| 160 | ->orderBy('title','asc') |
||
| 161 | ->get(), |
||
| 162 | 'category' => $category, |
||
| 163 | 'cat' => $cat |
||
| 164 | ]; |
||
| 165 | |||
| 166 | return view('articles')->with('data', $data); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function search(Request $request, $cat) |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param $id |
||
| 205 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 206 | */ |
||
| 207 | public function edit($id) |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param Request $request |
||
| 226 | * @param $id |
||
| 227 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 228 | * @throws \Illuminate\Validation\ValidationException |
||
| 229 | */ |
||
| 230 | public function update(Request $request, $id) |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $id |
||
| 277 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 278 | */ |
||
| 279 | public function destroy($id) |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get current logged in user |
||
| 303 | * |
||
| 304 | * @return mixed |
||
| 305 | */ |
||
| 306 | private function currentUser() { |
||
| 310 |