| Total Complexity | 54 |
| Total Lines | 385 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AssociationsController 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 AssociationsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class AssociationsController extends Controller |
||
| 20 | { |
||
| 21 | |||
| 22 | public function __construct(Request $request) { |
||
| 23 | $subdomain = array_first(explode('.', \Request::getHost())); |
||
| 24 | |||
| 25 | $this->association = Association::where('subdomain', $subdomain)->first(); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function view(Association $association) { |
||
| 29 | return view('association.view', ['association' => $association]); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function edit(Association $association) { |
||
| 33 | if (Bouncer::can('edit', $association)) { |
||
| 34 | return view('association.edit', [ |
||
| 35 | 'association' => $association, |
||
| 36 | 'series' => Series::where('association_id', $association->id)->get(), |
||
| 37 | 'divisions' => Division::orderBy('sequence', 'ASC')->where('association_id', $association->id)->get(), |
||
| 38 | 'venues' => Venue::orderBy('name', 'ASC')->where('association_id', $association->id)->get(), |
||
| 39 | 'current_user' => \Auth::user() |
||
| 40 | ]); |
||
| 41 | } |
||
| 42 | else { |
||
| 43 | return view('denied'); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | public function home() { |
||
| 48 | if (!empty($this->association)) { |
||
| 49 | return view('association.home', ['association' => $this->association]); |
||
| 50 | } |
||
| 51 | else { |
||
| 52 | abort(404); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | public function divisions(Association $association) { |
||
| 57 | return view('association.divisions', ['association' => $association]); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function teams(Association $association) { |
||
| 61 | return view('association.teams', ['association' => $association]); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function venues(Association $association) { |
||
| 65 | return view('association.venues', ['association' => $association]); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function series(Association $association) { |
||
| 69 | return view('association.series', ['association' => $association]); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function users(Association $association) { |
||
| 73 | return view('association.users', ['association' => $association]); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function viewUser(Association $association, User $user) { |
||
| 77 | return view('association.user.view', ['association' => $association, 'user' => $user]); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function editUser(Association $association, User $user) { |
||
| 81 | return view('association.user.edit', ['association' => $association, 'user' => $user]); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function userToken(Association $association, User $user) { |
||
| 85 | return view('association.user.token', ['association' => $association, 'user' => $user]); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function userTokenRefresh(Association $association, User $user) { |
||
| 89 | $token = Str::random(60); |
||
| 90 | |||
| 91 | $user->forceFill([ |
||
| 92 | 'api_token' => hash('sha256', $token), |
||
| 93 | ])->save(); |
||
| 94 | |||
| 95 | return view('association.user.token-refresh', ['association' => $association, 'user' => $user, 'token' => $token]); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function updateUser(Request $request, Association $association, User $user) { |
||
| 99 | |||
| 100 | if (isset($request->assoc_admin)) { |
||
| 101 | Bouncer::assign('assocadmin')->to($user); |
||
| 102 | Bouncer::allow($user)->toManage($association); |
||
| 103 | } |
||
| 104 | else { |
||
| 105 | Bouncer::disallow($user)->toManage($association); |
||
| 106 | Bouncer::retract('assocadmin')->from($user); |
||
| 107 | } |
||
| 108 | |||
| 109 | $url = $request->url; |
||
| 110 | |||
| 111 | if (!empty($url)) { |
||
| 112 | return redirect($url)->with('success', 'Data saved successfully!'); |
||
| 113 | } |
||
| 114 | |||
| 115 | return redirect()->route('user', ['id' => \Auth::user()->id]); |
||
| 116 | |||
| 117 | } |
||
| 118 | |||
| 119 | public function addUser(Association $association) { |
||
| 120 | return view('association.user.add', ['association' => $association]); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function submitScoreBegin(Request $request) { |
||
| 124 | if (!empty($this->association)) { |
||
| 125 | // get schedules with start_date < today, end_date > today |
||
| 126 | $schedules = $this->association->schedules |
||
| 127 | ->where('start_date', '<', date('Y-m-d', strtotime('today'))) |
||
| 128 | ->where('end_date', '>', date('Y-m-d', strtotime('today'))); |
||
| 129 | |||
| 130 | // get rounds with start_date < today, but greater than today - 1 week |
||
| 131 | $rounds = Round::whereIn('schedule_id', $schedules->pluck('id')) |
||
| 132 | ->where('start_date','>=', date('Y-m-d', strtotime('-1 week'))) |
||
| 133 | ->where('start_date', '<=', date('Y-m-d', strtotime("today")))->get(); |
||
| 134 | |||
| 135 | $divisions = Division::whereIn('id', $rounds->pluck('division_id'))->get(); |
||
| 136 | |||
| 137 | if (count($divisions) === 1) { |
||
| 138 | $request->division_id = $divisions[0]->id; |
||
| 139 | |||
| 140 | return $this->submitScoreStep2($request); |
||
| 141 | } |
||
| 142 | else { |
||
| 143 | return view('forms.results.choose-division', [ |
||
| 144 | 'association' => $this->association, |
||
| 145 | 'divisions' => $divisions, |
||
| 146 | ]); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | else { |
||
| 150 | abort(404); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | public function submitScoreStep2(Request $request) { |
||
| 155 | if (!empty($this->association)) { |
||
| 156 | $division = Division::find($request->division_id); |
||
| 157 | |||
| 158 | // get schedules with start_date < today, end_date > today, matching division |
||
| 159 | $schedules = $this->association->schedules |
||
| 160 | ->where('start_date', '<=', date('Y-m-d', strtotime('today'))) |
||
| 161 | ->where('end_date', '>=', date('Y-m-d', strtotime('today'))) |
||
| 162 | ->where('division_id', $division->id); |
||
| 163 | |||
| 164 | // get rounds with start_date < today, but greater than today - 1 week, not closed |
||
| 165 | $rounds = Round::whereIn('schedule_id', $schedules->pluck('id')) |
||
| 166 | ->where('start_date', '>=', date('Y-m-d', strtotime('-1 week'))) |
||
| 167 | ->where('start_date', '<=', date('Y-m-d', strtotime("today"))) |
||
| 168 | ->where(function ($query) { |
||
| 169 | $query->where('scores_closed', 0); |
||
| 170 | $query->orWhereNull('scores_closed'); |
||
| 171 | }) |
||
| 172 | ->orderBy('start_date', 'DESC') |
||
| 173 | ->get(); |
||
| 174 | |||
| 175 | return view('forms.results.choose-match', [ |
||
| 176 | 'association' => $this->association, |
||
| 177 | 'rounds' => $rounds, |
||
| 178 | ]); |
||
| 179 | } |
||
| 180 | else { |
||
| 181 | abort(404); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | public function submitScoreStep3(Request $request) { |
||
| 186 | if (!empty($this->association)) { |
||
| 187 | $match = Match::find($request->match_id); |
||
| 188 | |||
| 189 | return view('forms.results.input-scores', [ |
||
| 190 | 'association' => $this->association, |
||
| 191 | 'match' => $match, |
||
| 192 | ]); |
||
| 193 | } |
||
| 194 | else { |
||
| 195 | abort(404); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | public function submitScoreStep4(Request $request) { |
||
| 200 | if (!empty($this->association)) { |
||
| 201 | $match_id = $request->match_id; |
||
| 202 | |||
| 203 | if (!empty($match_id)) { |
||
| 204 | $home_team_id = $request->home_team_id; |
||
| 205 | $away_team_id = $request->away_team_id; |
||
| 206 | $home_team_score = $request->home_team_score; |
||
| 207 | $away_team_score = $request->away_team_score; |
||
| 208 | |||
| 209 | $submission = new ResultSubmission(); |
||
| 210 | $submission->association_id = $this->association->id; |
||
| 211 | $submission->schedule_id = Match::find($match_id)->schedule_id; |
||
| 212 | $submission->match_id = $match_id; |
||
| 213 | $submission->home_team_score = $home_team_score; |
||
| 214 | $submission->away_team_score = $away_team_score; |
||
| 215 | $submission->save(); |
||
| 216 | |||
| 217 | if ($home_team_score != $away_team_score) { |
||
| 218 | $submission->win_team_id = $home_team_score > $away_team_score ? $home_team_id : $away_team_id; |
||
| 219 | $submission->save(); |
||
| 220 | |||
| 221 | return view('forms.results.thanks', [ |
||
| 222 | 'association' => $this->association, |
||
| 223 | ]); |
||
| 224 | } |
||
| 225 | else { |
||
| 226 | return view('forms.results.choose-winner', [ |
||
| 227 | 'association' => $this->association, |
||
| 228 | 'match' => Match::find($submission->match_id), |
||
| 229 | 'submission' => $submission, |
||
| 230 | ]); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | else { |
||
| 234 | abort(404); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | else { |
||
| 238 | abort(404); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | public function submitScoreStep5(Request $request) { |
||
| 243 | if (!empty($this->association)) { |
||
| 244 | $submission_id = $request->submission_id; |
||
| 245 | |||
| 246 | if (!empty($submission_id)) { |
||
| 247 | $submission = ResultSubmission::find($submission_id); |
||
| 248 | |||
| 249 | $submission->win_team_id = $request->win_team_id; |
||
| 250 | |||
| 251 | $submission->save(); |
||
| 252 | |||
| 253 | return view('forms.results.thanks', [ |
||
| 254 | 'association' => $this->association, |
||
| 255 | ]); |
||
| 256 | } |
||
| 257 | else { |
||
| 258 | abort(404); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | else { |
||
| 262 | abort(404); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | public function standings() { |
||
| 267 | return view('association.standings', ['association' => $this->association]); |
||
| 268 | } |
||
| 269 | |||
| 270 | public function schedule() { |
||
| 271 | return view('association.schedule', ['association' => $this->association]); |
||
| 272 | } |
||
| 273 | |||
| 274 | public function css() { |
||
| 275 | $response = \Response::make('body { background-color: red; }'); |
||
| 276 | $response->header('Content-Type', 'text/css'); |
||
| 277 | return $response; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Store a new association. |
||
| 282 | * |
||
| 283 | * @param Request $request |
||
| 284 | * @return Response |
||
| 285 | */ |
||
| 286 | public function store(Request $request) { |
||
| 287 | if (Bouncer::can('create', Association::class)) { |
||
| 288 | $association = new Association; |
||
| 289 | |||
| 290 | $association->name = $request->name; |
||
| 291 | $association->user_id = $request->user_id; |
||
| 292 | |||
| 293 | $association->save(); |
||
| 294 | |||
| 295 | // TODO: Do not necessarily "onboard" for certain roles? |
||
| 296 | return redirect()->route('onboard.association', ['association' => $association]); |
||
| 297 | } |
||
| 298 | else { |
||
| 299 | return view('denied'); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | public function update(Request $request) { |
||
| 304 | |||
| 305 | $association = Association::find($request->id); |
||
| 306 | |||
| 307 | $association->name = $request->name; |
||
| 308 | $association->user_id = $request->user_id; |
||
| 309 | |||
| 310 | if (isset($request->subdomain)) { |
||
| 311 | $association->subdomain = $request->subdomain; |
||
| 312 | } |
||
| 313 | |||
| 314 | if (isset($request->home_image_file)) { |
||
| 315 | $path = $request->home_image_file->storeAs( |
||
| 316 | 'home_image_file/' . $association->subdomain, $request->home_image_file->getClientOriginalName(), 'public' |
||
| 317 | ); |
||
| 318 | |||
| 319 | $association->home_image_path = $path; |
||
| 320 | } |
||
| 321 | |||
| 322 | $association->about = $request->about; |
||
| 323 | |||
| 324 | $association->save(); |
||
| 325 | |||
| 326 | //Session::flash('message', 'Successfully updated nerd!'); |
||
| 327 | |||
| 328 | $url = $request->url; |
||
| 329 | |||
| 330 | if (!empty($url)) { |
||
| 331 | return redirect($url)->with('success', 'Data saved successfully!'); |
||
| 332 | } |
||
| 333 | |||
| 334 | return redirect()->route('user', ['id' => \Auth::user()->id]); |
||
| 335 | |||
| 336 | } |
||
| 337 | |||
| 338 | public function create() { |
||
| 339 | if (Bouncer::can('create', Association::class)) { |
||
| 340 | return view('association.create', ['current_user' => \Auth::user()]); |
||
| 341 | } |
||
| 342 | else { |
||
| 343 | return view('denied'); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | public function deleteConfirm(Association $association) { |
||
| 348 | return view('association.delete', ['association' => $association]); |
||
| 349 | } |
||
| 350 | |||
| 351 | public function delete(Association $association) { |
||
| 352 | $association->delete(); |
||
| 353 | |||
| 354 | return redirect()->route('admin')->with('success', 'Association deleted successfully.'); |
||
| 355 | } |
||
| 356 | |||
| 357 | public function undeleteConfirm(Association $association) { |
||
| 358 | return view('association.undelete', ['association' => $association]); |
||
| 359 | } |
||
| 360 | |||
| 361 | public function undelete(Association $association) { |
||
| 362 | $association->restore(); |
||
| 363 | |||
| 364 | return redirect()->route('user', ['user' => \Auth::user()])->with('success', 'Association restored successfully.'); |
||
| 365 | } |
||
| 366 | |||
| 367 | public function about() { |
||
| 368 | return view('association.about' , ['association' => $this->association]); |
||
| 369 | } |
||
| 370 | |||
| 371 | public function contact() { |
||
| 372 | return view('forms.contact', ['association' => $this->association]); |
||
| 373 | } |
||
| 374 | |||
| 375 | public function contactSubmit(Request $request) { |
||
| 376 | $validatedData = $request->validate([ |
||
| 377 | 'email' => 'required|email|max:255', |
||
| 378 | ]); |
||
| 379 | |||
| 380 | $contact = new ContactSubmission(); |
||
| 381 | |||
| 382 | $contact->email = $request->email; |
||
| 383 | $contact->reason = $request->reason; |
||
| 384 | $contact->comment = $request->comment; |
||
| 385 | $contact->association_id = $request->association_id; |
||
| 386 | |||
| 387 | $contact->save(); |
||
| 388 | |||
| 389 | return redirect()->route('contact.thanks'); |
||
| 390 | } |
||
| 391 | |||
| 392 | public function contactThanks() { |
||
| 393 | return view('contact-thanks'); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Display a listing of the resource. |
||
| 398 | * |
||
| 399 | * @return \Illuminate\Http\Response |
||
| 400 | */ |
||
| 401 | public function contactSubmissions(Association $association) |
||
| 402 | { |
||
| 403 | return view('association.contact_submissions', ['association' => $association]); |
||
| 404 | } |
||
| 407 |