1 | <?php |
||
2 | |||
3 | namespace App\Http\Controllers; |
||
4 | |||
5 | use App\Championship; |
||
6 | use App\Http\Requests\TeamRequest; |
||
7 | use App\Team; |
||
8 | use App\Tournament; |
||
9 | use Illuminate\Auth\Access\AuthorizationException; |
||
10 | use Illuminate\Database\QueryException; |
||
11 | use Illuminate\Http\JsonResponse; |
||
12 | use Illuminate\Http\Request; |
||
13 | use Illuminate\Support\Collection; |
||
14 | use Illuminate\Support\Facades\Auth; |
||
15 | use Illuminate\Support\Facades\Lang; |
||
16 | use Illuminate\Support\Facades\Response; |
||
17 | use Illuminate\Support\Facades\View; |
||
18 | |||
19 | class TeamController extends Controller |
||
20 | { |
||
21 | /** |
||
22 | * Display a listing of the teams for a tournament. |
||
23 | * |
||
24 | * @param Tournament $tournament |
||
25 | * @return View |
||
26 | */ |
||
27 | public function index(Tournament $tournament) |
||
28 | { |
||
29 | $tournament = Tournament::with(['championships' => function ($query) { |
||
30 | $query->with('teams') |
||
31 | ->whereHas('category', function ($subquery) { |
||
32 | $subquery->where('isTeam', '=', 1); |
||
33 | }); |
||
34 | }])->withCount('competitors', 'teams') |
||
35 | ->find($tournament->id); |
||
36 | |||
37 | |||
38 | $arrChampionshipsWithTeamsAndCompetitors = $tournament->championships->map(function ($championship) { |
||
39 | $competitors = $championship->competitors->load('user')->map(function ($competitor) { |
||
40 | return ["id" => $competitor->id, "name" => $competitor->user->name]; |
||
41 | })->toArray(); |
||
42 | $teams = $championship->teams()->with('competitors.user')->select('id','name')->get()->toArray(); |
||
43 | $tempAssignCompatitors = new Collection(); |
||
44 | $assignedCompetitors = $this->getAssignedCompetitors($championship, $tempAssignCompatitors); |
||
45 | $freeCompetitors = $championship->competitors; |
||
46 | if ($assignedCompetitors != null) { |
||
47 | $freeCompetitors = $freeCompetitors->diff($assignedCompetitors); |
||
48 | } |
||
49 | |||
50 | return [ |
||
51 | 'championship' => $championship->id, |
||
52 | 'competitors' => $competitors, |
||
53 | 'assignedCompetitors' => $assignedCompetitors, |
||
54 | 'freeCompetitors' => $freeCompetitors, |
||
55 | 'teams' => $teams |
||
56 | ]; |
||
57 | })->toArray(); |
||
58 | return view("teams.index", compact('tournament', 'arrChampionshipsWithTeamsAndCompetitors')); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
59 | |||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param $championship |
||
64 | * @param $tempAssignCompatitors |
||
65 | * @return mixed |
||
66 | */ |
||
67 | private function getAssignedCompetitors($championship, $tempAssignCompatitors) |
||
68 | { |
||
69 | return $championship->teams->reduce(function ($acc, $team) use ($tempAssignCompatitors) { |
||
70 | return $tempAssignCompatitors->push($team->competitors()->with('user')->get())->collapse(); |
||
71 | }); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Show the form for creating a new competitor. |
||
76 | * |
||
77 | * @param Tournament $tournament |
||
78 | * @return View |
||
79 | * @throws AuthorizationException |
||
80 | */ |
||
81 | public function create(Tournament $tournament) |
||
82 | { |
||
83 | $team = new Team; |
||
84 | $this->authorize('create', [Team::class, $tournament, Auth::user()]); |
||
85 | return view("teams.form", compact('tournament', 'team', 'cts')); |
||
0 ignored issues
–
show
|
|||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Store a newly created resource in storage. |
||
90 | * |
||
91 | * @param Request $request |
||
92 | * @param Championship $championship |
||
93 | * @return View |
||
94 | * @throws AuthorizationException |
||
95 | */ |
||
96 | public function store(Request $request, Championship $championship) |
||
97 | { |
||
98 | $this->authorize('store', [Team::class, $championship->tournament, Auth::user()]); |
||
99 | try { |
||
100 | $team = Team::where('championship_id', $championship->id)->orderBy('id', 'desc')->first(); |
||
101 | $short_id = 1; |
||
102 | if ($team != null) { |
||
103 | $short_id = $team->short_id + 1; |
||
0 ignored issues
–
show
|
|||
104 | } |
||
105 | $request->request->add(['short_id' => $short_id]); |
||
106 | $team = Team::create($request->all()); |
||
107 | flash()->success(trans('msg.team_create_successful', ['name' => $team->name])); |
||
0 ignored issues
–
show
Are you sure the usage of
flash() is correct as it seems to always return null .
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
108 | } catch (QueryException $e) { |
||
109 | flash()->error(trans('msg.team_create_error_already_exists', ['name' => $request->name])); |
||
0 ignored issues
–
show
Are you sure the usage of
flash() is correct as it seems to always return null .
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
110 | } |
||
111 | |||
112 | return back()->with('activeTab', $request->activeTab); |
||
0 ignored issues
–
show
|
|||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Show the form for creating a new competitor. |
||
117 | * |
||
118 | * @param Tournament $tournament |
||
119 | * @param $teamId |
||
120 | * @return View |
||
121 | * @throws AuthorizationException |
||
122 | */ |
||
123 | public function edit(Tournament $tournament, $teamId) |
||
124 | { |
||
125 | $team = Team::findOrFail($teamId); |
||
126 | $this->authorize('edit', [Team::class, $tournament, Auth::user()]); |
||
127 | |||
128 | $cts = $tournament->buildCategoryList(); |
||
129 | return view("teams.form", compact('tournament', 'team', 'cts')); |
||
0 ignored issues
–
show
|
|||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Update a newly created resource in storage. |
||
134 | * |
||
135 | * @param TeamRequest $request |
||
136 | * @param Tournament $tournament |
||
137 | * @param $teamId |
||
138 | * @return Response |
||
139 | * @throws AuthorizationException |
||
140 | */ |
||
141 | public function update(TeamRequest $request, Tournament $tournament, $teamId) |
||
142 | { |
||
143 | $team = Team::findOrFail($teamId); |
||
144 | $this->authorize('update', [Team::class, $tournament, Auth::user()]); |
||
145 | |||
146 | $team->update($request->all()); |
||
147 | flash()->success(trans('msg.team_edit_successful', ['name' => $team->name])); |
||
0 ignored issues
–
show
Are you sure the usage of
flash() is correct as it seems to always return null .
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
148 | return redirect()->route('teams.index', $tournament->slug); |
||
0 ignored issues
–
show
|
|||
149 | |||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Remove the Team from storage. |
||
154 | * |
||
155 | * @param Team $team |
||
156 | * @return JsonResponse |
||
157 | * @throws AuthorizationException |
||
158 | */ |
||
159 | public function destroy(Team $team) |
||
160 | { |
||
161 | $tournament = $team->championship->tournament; |
||
0 ignored issues
–
show
|
|||
162 | $this->authorize('delete', [Team::class, $tournament, Auth::user()]); |
||
163 | |||
164 | if ($team->forceDelete()) { |
||
165 | return Response::json(['msg' => Lang::get('msg.team_delete_successful', ['name' => $team->name]), 'status' => 'success']); |
||
166 | } |
||
167 | return Response::json(['msg' => Lang::get('msg.team_delete_error', ['name' => $team->name]), 'status' => 'error']); |
||
168 | } |
||
169 | } |
||
170 |