Issues (232)

app/Http/Controllers/ClubController.php (16 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Association;
6
use App\Club;
7
use App\Federation;
8
use App\Http\Requests\ClubRequest;
9
use App\User;
10
use Illuminate\Auth\Access\AuthorizationException;
11
use Illuminate\Database\QueryException;
12
use Illuminate\Http\JsonResponse;
13
use Illuminate\Http\RedirectResponse;
14
use Illuminate\Support\Collection;
15
use Illuminate\Support\Facades\Auth;
16
use Illuminate\Support\Facades\Request;
17
use Illuminate\Support\Facades\View;
18
use Response;
19
20
class ClubController extends Controller
21
{
22
    // Only Super Admin and Club President can manage Clubs
23
24
    /**
25
     * Display a listing of the resource.
26
     *
27
     * @return Collection|View
28
     */
29 1
    public function index()
30
    {
31
32 1
        if (Request::ajax()) {
33
            return Club::fillSelect(Auth::user()->federation_id, Auth::user()->association_id);
34
        } else {
35 1
            $clubs = Club::with('president', 'association.federation')
36 1
                ->forUser(Auth::user())
37 1
                ->where('id', '>', 1)
38 1
                ->get();
39
40 1
            return view('clubs.index', compact('clubs'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('clubs.index', compact('clubs')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Colle...te\Support\Facades\View.
Loading history...
41
        }
42
43
    }
44
45
46
    /**
47
     * Show the form for creating a new resource.
48
     * @return View
49
     * @throws AuthorizationException
50
     */
51 1
    public function create()
52
    {
53
54
55 1
        $club = new Club;
56
        // Authorization
57
58 1
        $federations = Federation::fillSelect();
59 1
        $associations = Association::forUser(auth()->user())->pluck('name', 'id')->prepend('-', 0);
60 1
        $users = Auth::user()->fillSelect();
61
62
63 1
        $defaultLng = Auth::user()->latitude ?? geoip()->lat;
0 ignored issues
show
The property lat does not seem to exist on Torann\GeoIP\GeoIP.
Loading history...
64 1
        $defaultLat = Auth::user()->longitude ?? geoip()->lon;
0 ignored issues
show
The property lon does not seem to exist on Torann\GeoIP\GeoIP.
Loading history...
65
66
67 1
        return view('clubs.form', compact('club', 'users', 'federations', 'associations', 'defaultLng', 'defaultLat')); //
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('clubs.form'...ultLng', 'defaultLat')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
68
    }
69
70
    /**
71
     * Store a newly created resource in storage.
72
     *
73
     * @param ClubRequest $request
74
     * @return JsonResponse|RedirectResponse
75
     */
76 1
    public function store(ClubRequest $request)
77
    {
78
79
        try {
80 1
            if ($request->president_id == 0) $request->merge(['president_id' => null]);
81 1
            if ($request->federation_id == 0) $request->merge(['federation_id' => null]);
82 1
            if ($request->association_id == 0) $request->merge(['association_id' => null]);
83 1
            $club = Club::create($request->all());
84
85 1
            if (Request::ajax()) {
86
                return Response::json(['msg' => trans('msg.club_create_successful', ['name' => $club->name]), 'status' => 'success', 'data' => $club], 200);
87
            } else {
88 1
                $msg = trans('msg.club_edit_successful', ['name' => $club->name]);
89 1
                flash()->success($msg);
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 getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
90
91 1
                return redirect("clubs");
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('clubs') also could return the type Illuminate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\JsonResp...e\Http\RedirectResponse.
Loading history...
92
93
            }
94
        } catch (QueryException $e) {
95
            $user = User::find($request->president_id);
96
            $msg = trans('msg.club_president_already_exists', ['user' => $user->name]);
97
            flash()->error($msg);
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 getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
98
            return redirect()->back();
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back() also could return the type Illuminate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\JsonResp...e\Http\RedirectResponse.
Loading history...
99
        }
100
101
    }
102
103
104
    /**
105
     * Display the specified resource.
106
     *
107
     * @param  int $id
108
     * @return View
109
     */
110
    public function show($id)
111
    {
112
        $club = Club::findOrFail($id);
113
        return view('clubs.show', compact('club'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('clubs.show', compact('club')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
114
    }
115
116
    /**
117
     * Show the form for editing the specified resource.
118
     *
119
     * @param  int $id
120
     * @return View
121
     * @throws AuthorizationException
122
     */
123 2
    public function edit($id)
124
    {
125
126 2
        $defaultLng = Auth::user()->latitude ?? geoip()->lat;
0 ignored issues
show
The property lat does not seem to exist on Torann\GeoIP\GeoIP.
Loading history...
127 2
        $defaultLat = Auth::user()->longitude ?? geoip()->lon;
0 ignored issues
show
The property lon does not seem to exist on Torann\GeoIP\GeoIP.
Loading history...
128
129 2
        $club = Club::findOrFail($id);
130 2
        $this->authorize('edit', [$club, Auth::user()]);
131
132 1
        $federations = Federation::fillSelect();
133 1
        $associations = Association::forUser(auth()->user())->pluck('name', 'id')->prepend('-', 0);
134 1
        $users = Auth::user()->fillSelect();
135
136 1
        return view('clubs.form', compact('club', 'users', 'associations', 'federations', 'defaultLng', 'defaultLat')); //
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('clubs.form'...ultLng', 'defaultLat')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
137
    }
138
139
    /**
140
     * Update the specified resource in storage.
141
     *
142
     * @param ClubRequest|Request $request
143
     * @param  int $id
144
     * @return \Illuminate\Http\Response
145
     * @throws AuthorizationException
146
     */
147
    public function update(ClubRequest $request, $id)
148
    {
149
        $club = Club::findOrFail($id);
150
151
        $this->authorize('update', [$club, Auth::user()]);
152
153
        try {
154
            if ($request->president_id == 0) $request->merge(['president_id' => null]);
155
            if ($request->association_id == 0) $request->merge(['association_id' => null]);
156
157
            $club->update($request->all());
158
            $msg = trans('msg.club_edit_successful', ['name' => $club->name]);
159
            flash()->success($msg);
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 getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
160
            return redirect("clubs");
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('clubs') returns the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
161
        } catch (QueryException $e) {
162
            // Already
163
            $msg = "User" . $request->id . "is already president of another club";
164
            flash()->error($msg);
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 getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
165
            return redirect()->back();
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back() returns the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
166
        }
167
    }
168
169
    /**
170
     * Remove the specified resource from storage.
171
     *
172
     * @param $clubId
173
     * @return JsonResponse
174
     * @internal param Club $club
175
     */
176
    public function destroy($clubId)
177
    {
178
        $club = Club::find($clubId);
179
        if ($club->delete()) {
180
            return Response::json(['msg' => trans('msg.club_delete_successful', ['name' => $club->name]), 'status' => 'success']);
181
        } else {
182
            return Response::json(['msg' => trans('msg.club_delete_error', ['name' => $club->name]), 'status' => 'error']);
183
        }
184
    }
185
186
    /**
187
     * @param $id
188
     * @return \Illuminate\Http\JsonResponse
189
     */
190
    public function restore($id)
191
192
    {
193
        $club = Club::withTrashed()->find($id);
194
        if ($club->restore()) {
195
            return Response::json(['msg' => trans('msg.club_restored_successful', ['name' => $club->name]), 'status' => 'success']);
196
        } else {
197
            return Response::json(['msg' => trans('msg.club_restored_error', ['name' => $club->name]), 'status' => 'error']);
198
        }
199
    }
200
}
201