Test Setup Failed
Pull Request — master (#26)
by
unknown
04:33
created

RoundsController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 58
c 2
b 0
f 1
dl 0
loc 153
rs 10
wmc 13
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Match;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_MATCH, expecting T_STRING or '{' on line 5 at column 8
Loading history...
6
use App\Round;
7
use App\Schedule;
8
use Illuminate\Http\Request;
9
10
class RoundsController extends Controller
11
{
12
    /**
13
     * Display a listing of the resource.
14
     *
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function index()
18
    {
19
        //
20
    }
21
22
    /**
23
     * Show the form for creating a new resource.
24
     *
25
     * @return \Illuminate\Http\Response
26
     */
27
    public function create(Schedule $schedule)
28
    {
29
        return view('round.create', [
30
            'schedule' => $schedule,
31
        ]);
32
    }
33
34
    /**
35
     * Store a newly created resource in storage.
36
     *
37
     * @param  \Illuminate\Http\Request  $request
38
     * @return \Illuminate\Http\Response
39
     */
40
    public function store(Request $request, Schedule $schedule)
41
    {
42
        $validatedData = $request->validate([
43
            'name' => 'required|max:255',
44
            'start_date' => 'required|date',
45
        ]);
46
47
        $round = new Round();
48
49
        $round->name = $request->name;
50
        $round->series_id = $schedule->series->id;
51
        $round->schedule_id = $schedule->id;
52
        $round->start_date = $request->start_date;
53
        $round->end_date = $request->end_date;
54
55
        $round->save();
56
57
        $association = $schedule->association;
58
        $venues = $association->venues;
59
60
        foreach ($venues as $venue) {
61
            $match = new Match;
62
63
            $match->name = $venue->name . ' – ' . $round->start_date->format('m-d-Y');
64
            $match->association_id = $association->id;
65
66
            if (!empty($schedule->series)) {
67
                $match->series_id = $schedule->series->id;
68
69
                if (!empty($schedule->series->division)) {
70
                    $match->division_id = $schedule->series->division->id;
71
                }
72
            }
73
74
            // Unique key fields:
75
            $match->schedule_id = $schedule->id;
76
            $match->round_id = $round->id;
77
            $match->venue_id = $venue->id;
78
            $match->sequence = 1;
79
80
            $match->start_date = $round->start_date;
81
            $match->end_date = $round->end_date;
82
83
            $match->save();
84
        }
85
86
        return redirect()->route('schedule.rounds', ['schedule' => $schedule]);
87
    }
88
89
    /**
90
     * Display the specified resource.
91
     *
92
     * @param  int  $id
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function show($id)
96
    {
97
        //
98
    }
99
100
    public function edit(Schedule $schedule, Round $round)
101
    {
102
        return view('round.edit', [
103
            'schedule' => $schedule,
104
            'round' => $round,
105
        ]);
106
    }
107
108
    /**
109
     * Update the specified resource in storage.
110
     *
111
     * @param  \Illuminate\Http\Request  $request
112
     * @param \App\Schedule $schedule
113
     * @param  int  $id
114
     * @return \Illuminate\Http\Response
115
     */
116
    public function update(Request $request, $schedule, $id)
117
    {
118
        $validatedData = $request->validate([
119
            'name' => 'required|max:255',
120
            'start_date' => 'required|date',
121
        ]);
122
123
        $round = Round::find($id);
124
125
        $round->name = $request->name;
126
        $round->start_date = $request->start_date;
127
        $round->end_date = $request->end_date;
128
        $round->scores_closed = isset($request->scores_closed);
129
130
        $round->save();
131
132
        foreach ($round->matches as $match) {
133
            $home_team_id = $request->{'match_' . $match->id . '__home_team_id'};
134
            $away_team_id = $request->{'match_' . $match->id . '__away_team_id'};
135
136
            $match->home_team_id = $home_team_id;
137
            $match->away_team_id = $away_team_id;
138
139
            $match->save();
140
        }
141
142
        $request->session()->flash('message', __('Successfully updated round'));
143
144
        return redirect()->route('schedule.rounds', ['schedule' => $schedule]);
145
    }
146
147
    public function deleteConfirm(Schedule $schedule, Round $round) {
148
        return view('round.delete-confirm', [
149
            'schedule' => $schedule,
150
            'round' => $round,
151
        ]);
152
    }
153
154
    public function destroy(Schedule $schedule, Round $round)
155
    {
156
        $round = Round::find($round->id);
157
158
        if (!empty($round)) {
159
            $round->delete();
160
        }
161
162
        return redirect()->route('schedule.rounds', ['schedule' => $schedule]);
163
    }
164
}
165