Test Setup Failed
Push — master ( 0a8e7c...ce45b0 )
by Sam
04:25
created

AssociationsController   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 326
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 154
dl 0
loc 326
rs 8.72
c 0
b 0
f 0
wmc 46

27 Methods

Rating   Name   Duplication   Size   Complexity  
A teams() 0 2 1
A submitScoreStep4() 0 40 5
A home() 0 6 2
A addUser() 0 2 1
A create() 0 6 2
A submitScoreStep2() 0 28 2
A submitScoreBegin() 0 28 3
A venues() 0 2 1
A users() 0 2 1
A __construct() 0 4 1
A store() 0 14 2
A updateUser() 0 18 3
A schedule() 0 2 1
A undelete() 0 4 1
A deleteConfirm() 0 2 1
A delete() 0 4 1
A series() 0 2 1
A update() 0 30 4
A standings() 0 2 1
A submitScoreStep3() 0 11 2
A divisions() 0 2 1
A editUser() 0 2 1
A css() 0 4 1
A undeleteConfirm() 0 2 1
A view() 0 2 1
A edit() 0 12 2
A submitScoreStep5() 0 21 3

How to fix   Complexity   

Complex Class

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
2
3
namespace App\Http\Controllers;
4
5
use App\Association;
6
use App\Division;
7
use App\Match;
8
use App\ResultSubmission;
9
use App\Round;
10
use App\Series;
11
use App\Schedule;
12
use App\User;
13
use App\Venue;
14
use Bouncer;
15
use Illuminate\Http\Request;
16
17
class AssociationsController extends Controller
18
{
19
20
    public function __construct(Request $request) {
21
        $subdomain = array_first(explode('.', \Request::getHost()));
0 ignored issues
show
Deprecated Code introduced by
The function array_first() has been deprecated: Arr::first() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

21
        $subdomain = /** @scrutinizer ignore-deprecated */ array_first(explode('.', \Request::getHost()));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
22
23
        $this->association = Association::where('subdomain', $subdomain)->first();
0 ignored issues
show
Bug Best Practice introduced by
The property association does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
    }
25
26
    public function view(Association $association) {
27
        return view('association.view', ['association' => $association]);
28
    }
29
30
    public function edit(Association $association) {
31
        if (Bouncer::can('edit', $association)) {
32
            return view('association.edit', [
33
                'association' => $association,
34
                'series' => Series::where('association_id', $association->id)->get(),
35
                'divisions' => Division::orderBy('sequence', 'ASC')->where('association_id', $association->id)->get(),
36
                'venues' => Venue::orderBy('name', 'ASC')->where('association_id', $association->id)->get(),
37
                'current_user' => \Auth::user()
38
            ]);
39
        }
40
        else {
41
            return view('denied');
42
        }
43
    }
44
45
    public function home() {
46
        if (!empty($this->association)) {
47
            return view('association.home', ['association' => $this->association]);
48
        }
49
        else {
50
            abort(404);
51
        }
52
    }
53
54
    public function divisions(Association $association) {
55
        return view('association.divisions', ['association' => $association]);
56
    }
57
58
    public function teams(Association $association) {
59
        return view('association.teams', ['association' => $association]);
60
    }
61
62
    public function venues(Association $association) {
63
        return view('association.venues', ['association' => $association]);
64
    }
65
66
    public function series(Association $association) {
67
        return view('association.series', ['association' => $association]);
68
    }
69
70
    public function users(Association $association) {
71
        return view('association.users', ['association' => $association]);
72
    }
73
74
    public function editUser(Association $association, User $user) {
75
        return view('association.user.edit', ['association' => $association, 'user' => $user]);
76
    }
77
78
    public function updateUser(Request $request, Association $association, User $user) {
79
80
        if (isset($request->assoc_admin)) {
81
            Bouncer::assign('assocadmin')->to($user);
82
            Bouncer::allow($user)->toManage($association);
83
        }
84
        else {
85
            Bouncer::disallow($user)->toManage($association);
86
            Bouncer::retract('assocadmin')->from($user);
87
        }
88
89
        $url = $request->url;
90
91
        if (!empty($url)) {
92
            return redirect($url)->with('success', 'Data saved successfully!');
93
        }
94
95
        return redirect()->route('user', ['id' => \Auth::user()->id]);
96
97
    }
98
99
    public function addUser(Association $association) {
100
        return view('association.user.add', ['association' => $association]);
101
    }
102
103
    public function submitScoreBegin(Request $request) {
104
        if (!empty($this->association)) {
105
            // get schedules with start_date < today, end_date > today
106
            $schedules = $this->association->schedules
107
            ->where('start_date', '<', date('Y-m-d', strtotime('today')))
108
            ->where('end_date', '>', date('Y-m-d', strtotime('today')));
109
110
            // get rounds with start_date < today, but greater than today - 1 week
111
            $rounds = Round::whereIn('schedule_id', $schedules->pluck('id'))
112
                ->where('start_date','>=', date('Y-m-d', strtotime('-1 week')))
113
                ->where('start_date', '<=', date('Y-m-d', strtotime("today")))->get();
114
115
            $divisions = Division::whereIn('id', $rounds->pluck('division_id'))->get();
116
117
            if (count($divisions) === 1) {
118
                $request->division_id = $divisions[0]->id;
0 ignored issues
show
Bug introduced by
The property division_id does not seem to exist on Illuminate\Http\Request.
Loading history...
119
120
                return $this->submitScoreStep2($request);
121
            }
122
            else {
123
                return view('forms.results.choose-division', [
124
                    'association' => $this->association,
125
                    'divisions' => $divisions,
126
                    ]);
127
            }
128
        }
129
        else {
130
            abort(404);
131
        }
132
    }
133
134
    public function submitScoreStep2(Request $request) {
135
        if (!empty($this->association)) {
136
            $division = Division::find($request->division_id);
137
138
            // get schedules with start_date < today, end_date > today, matching division
139
            $schedules = $this->association->schedules
140
                ->where('start_date', '<=', date('Y-m-d', strtotime('today')))
141
                ->where('end_date', '>=', date('Y-m-d', strtotime('today')))
142
                ->where('division_id', $division->id);
143
144
            // get rounds with start_date < today, but greater than today - 1 week, not closed
145
            $rounds = Round::whereIn('schedule_id', $schedules->pluck('id'))
146
                ->where('start_date', '>=', date('Y-m-d', strtotime('-1 week')))
147
                ->where('start_date', '<=', date('Y-m-d', strtotime("today")))
148
                ->where(function ($query) {
149
                    $query->where('scores_closed', 0);
150
                    $query->orWhereNull('scores_closed');
151
                })
152
                ->orderBy('start_date', 'DESC')
153
                ->get();
154
155
            return view('forms.results.choose-match', [
156
                'association' => $this->association,
157
                'rounds' => $rounds,
158
                ]);
159
        }
160
        else {
161
            abort(404);
162
        }
163
    }
164
165
    public function submitScoreStep3(Request $request) {
166
        if (!empty($this->association)) {
167
            $match = Match::find($request->match_id);
168
169
            return view('forms.results.input-scores', [
170
                'association' => $this->association,
171
                'match' => $match,
172
                ]);
173
        }
174
        else {
175
            abort(404);
176
        }
177
    }
178
179
    public function submitScoreStep4(Request $request) {
180
        if (!empty($this->association)) {
181
            $match_id = $request->match_id;
182
183
            if (!empty($match_id)) {
184
                $home_team_id = $request->home_team_id;
185
                $away_team_id = $request->away_team_id;
186
                $home_team_score = $request->home_team_score;
187
                $away_team_score = $request->away_team_score;
188
189
                $submission = new ResultSubmission();
190
                $submission->association_id = $this->association->id;
191
                $submission->schedule_id = Match::find($match_id)->schedule_id;
192
                $submission->match_id = $match_id;
193
                $submission->home_team_score = $home_team_score;
194
                $submission->away_team_score = $away_team_score;
195
                $submission->save();
196
197
                if ($home_team_score != $away_team_score) {
198
                    $submission->win_team_id = $home_team_score > $away_team_score ? $home_team_id : $away_team_id;
199
                    $submission->save();
200
201
                    return view('forms.results.thanks', [
202
                        'association' => $this->association,
203
                        ]);
204
                }
205
                else {
206
                    return view('forms.results.choose-winner', [
207
                        'association' => $this->association,
208
                        'match' => Match::find($submission->match_id),
209
                        'submission' => $submission,
210
                        ]);
211
                }
212
            }
213
            else {
214
                abort(404);
215
            }
216
        }
217
        else {
218
            abort(404);
219
        }
220
    }
221
222
    public function submitScoreStep5(Request $request) {
223
        if (!empty($this->association)) {
224
            $submission_id = $request->submission_id;
225
226
            if (!empty($submission_id)) {
227
                $submission = ResultSubmission::find($submission_id);
228
229
                $submission->win_team_id = $request->win_team_id;
230
231
                $submission->save();
232
233
                return view('forms.results.thanks', [
234
                    'association' => $this->association,
235
                    ]);
236
            }
237
            else {
238
                abort(404);
239
            }
240
        }
241
        else {
242
            abort(404);
243
        }
244
    }
245
246
    public function standings() {
247
        return view('association.standings', ['association' => $this->association]);
248
    }
249
250
    public function schedule() {
251
        return view('association.schedule', ['association' => $this->association]);
252
    }
253
254
    public function css() {
255
        $response = \Response::make('body { background-color: red; }');
256
        $response->header('Content-Type', 'text/css');
257
        return $response;
258
    }
259
260
    /**
261
     * Store a new association.
262
     *
263
     * @param  Request  $request
264
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
265
     */
266
    public function store(Request $request) {
267
        if (Bouncer::can('create', Association::class)) {
268
            $association = new Association;
269
270
            $association->name = $request->name;
271
            $association->user_id = $request->user_id;
272
273
            $association->save();
274
275
            // TODO: Do not necessarily "onboard" for certain roles?
276
            return redirect()->route('onboard.association', ['association' => $association]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...tion' => $association)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
277
        }
278
        else {
279
            return view('denied');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('denied') returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
280
        }
281
    }
282
283
    public function update(Request $request) {
284
285
        $association = Association::find($request->id);
286
287
        $association->name = $request->name;
288
        $association->user_id = $request->user_id;
289
290
        if (isset($request->subdomain)) {
291
            $association->subdomain = $request->subdomain;
292
        }
293
294
        if (isset($request->home_image_file)) {
295
            $path = $request->home_image_file->storeAs(
296
                'home_image_file/' . $association->subdomain, $request->home_image_file->getClientOriginalName(), 'public'
297
            );
298
299
            $association->home_image_path = $path;
300
        }
301
302
        $association->save();
303
304
        //Session::flash('message', 'Successfully updated nerd!');
305
306
        $url = $request->url;
307
308
        if (!empty($url)) {
309
            return redirect($url)->with('success', 'Data saved successfully!');
310
        }
311
312
        return redirect()->route('user', ['id' => \Auth::user()->id]);
313
314
    }
315
316
    public function create() {
317
        if (Bouncer::can('create', Association::class)) {
318
            return view('association.create', ['current_user' => \Auth::user()]);
319
        }
320
        else {
321
            return view('denied');
322
        }
323
    }
324
325
    public function deleteConfirm(Association $association) {
326
        return view('association.delete', ['association' => $association]);
327
    }
328
329
    public function delete(Association $association) {
330
        $association->delete();
331
332
        return redirect()->route('admin')->with('success', 'Association deleted successfully.');
333
    }
334
335
    public function undeleteConfirm(Association $association) {
336
        return view('association.undelete', ['association' => $association]);
337
    }
338
339
    public function undelete(Association $association) {
340
        $association->restore();
341
342
        return redirect()->route('user', ['user' => \Auth::user()])->with('success', 'Association restored successfully.');
343
    }
344
345
346
347
}
348