Issues (232)

app/Http/Controllers/AssociationController.php (14 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Association;
6
use App\Federation;
7
use App\Http\Requests\AssociationRequest;
8
use App\User;
9
use Illuminate\Auth\Access\AuthorizationException;
10
use Illuminate\Database\QueryException;
11
use Illuminate\Http\JsonResponse;
12
use Illuminate\Http\RedirectResponse;
13
use Illuminate\Support\Collection;
14
use Illuminate\Support\Facades\Auth;
15
use Illuminate\Support\Facades\Request;
16
use Illuminate\Support\Facades\Response;
17
use Illuminate\Support\Facades\View;
18
19
class AssociationController extends Controller
20
{
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @return Collection|View
25
     */
26 3
    public function index()
27
    {
28 3
        $associations = Association::forUser(auth()->user());
29 3
        if (Request::ajax()) {
30
            return $associations->pluck('name', 'id')->prepend('-', 0);
31
        }
32 3
        $associations = $associations->where('id', '>', 1)->get();
33 3
        return view('associations.index', compact('associations'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('association...ompact('associations')) 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...
34
35
    }
36
37
38
    /**
39
     * Show the form for creating a new resource.
40
     * @return View
41
     * @throws AuthorizationException
42
     */
43
    public function create()
44
    {
45
        $association = new Association;
46
        $federation = new Collection;
47
48
        $users = User::forUser(Auth::user())->pluck('name', 'id')->prepend('-', 0);
49
        $federations = Federation::forUser(Auth::user())->pluck('name', 'id');
50
        $submitButton = trans('core.addModel', ['currentModelName' => trans_choice('structures.association', 1)]);
51
        return view('associations.form', compact('association', 'users', 'federation', 'federations', 'submitButton')); //
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('association...ions', 'submitButton')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
52
    }
53
54
    /**
55
     * Store a newly created resource in storage.
56
     *
57
     * @param AssociationRequest $request
58
     * @return Response|RedirectResponse
59
     * @throws AuthorizationException
60
     */
61
    public function store(AssociationRequest $request)
62
    {
63
        try {
64
            if ($request->president_id == 0)
65
                $request->merge(['president_id' => null]);
66
67
            $association = Association::create($request->all());
68
            if (Request::ajax()) {
69
                return Response::json(['msg' => trans('msg.association_create_successful', ['name' => $association->name]), 'status' => 'success', 'data' => $association], 200);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Illuminate\Suppor... => $association), 200) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Redirect...upport\Facades\Response.
Loading history...
70
            } else {
71
                $msg = trans('msg.association_create_successful', ['name' => $association->name]);
72
                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...
73
                return redirect(route("associations.index"));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('associations.index')) also could return the type Illuminate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\Redirect...upport\Facades\Response.
Loading history...
74
            }
75
        } catch (QueryException $e) {
76
            if (Request::ajax()) {
77
                return Response::json(['msg' => $e], 500);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Illuminate\Suppor...rray('msg' => $e), 500) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Redirect...upport\Facades\Response.
Loading history...
78
            } else {
79
                $user = User::find($request->president_id);
80
                $msg = trans('msg.association_president_already_exists', ['user' => $user->name]);
81
                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...
82
                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\Redirect...upport\Facades\Response.
Loading history...
83
            }
84
        }
85
    }
86
87
88
    /**
89
     * Display the specified resource.
90
     *
91
     * @param  int $id
92
     * @return View
93
     */
94
    public function show($id)
95
    {
96
        $association = Association::findOrFail($id);
97
        return view('associations.show', compact('association'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('association...compact('association')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
98
    }
99
100
    /**
101
     * Show the form for editing the specified resource.
102
     *
103
     * @param  int $id
104
     * @return View
105
     * @throws AuthorizationException
106
     */
107 4
    public function edit($id)
108
    {
109 4
        $association = Association::findOrFail($id);
110 4
        $federation = $association->federation;
111
112 4
        if (Auth::user()->cannot('edit', $association)) {
113 3
            throw new AuthorizationException();
114
        }
115
116 1
        $users = User::forUser(Auth::user())->pluck('name', 'id')->prepend('-', 0);
117 1
        $federations = Federation::forUser(Auth::user())->pluck('name', 'id');
118
119 1
        return view('associations.form', compact('association', 'users', 'federations', 'federation'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('association...ations', 'federation')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
120
    }
121
122
    /**
123
     * Update the specified resource in storage.
124
     *
125
     * @param AssociationRequest|Request $request
126
     * @param  int $id
127
     * @return Response|RedirectResponse
128
     * @throws AuthorizationException
129
     */
130 1
    public function update(AssociationRequest $request, $id)
131
    {
132 1
        $association = Association::findOrFail($id);
133
134 1
        if (Auth::user()->cannot('update', $association)) {
135
            throw new AuthorizationException();
136
        }
137
        try {
138 1
            if ($request->president_id == 0)
139 1
                $request->merge(['president_id' => null]);
140
141 1
            $association->update($request->all());
142 1
            $msg = trans('msg.association_edit_successful', ['name' => $association->name]);
143 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...
144 1
            return redirect(route('associations.index'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('associations.index')) also could return the type Illuminate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\Redirect...upport\Facades\Response.
Loading history...
145
146
        } catch (QueryException $e) {
147
            $user = User::find($request->president_id);
148
            $msg = trans('msg.association_president_already_exists', ['user' => $user->name]);
149
            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...
150
            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\Redirect...upport\Facades\Response.
Loading history...
151
152
        }
153
154
    }
155
156
    /**
157
     * Remove the specified resource from storage.
158
     *
159
     * @param $associationId
160
     * @return JsonResponse
161
     */
162
    public function destroy($associationId)
163
    {
164
        $association = Association::find($associationId);
165
166
        if ($association->delete()) {
167
            return Response::json(['msg' => trans('msg.association_delete_successful', ['name' => $association->name]), 'status' => 'success']);
168
        } else {
169
            return Response::json(['msg' => trans('msg.association_delete_error', ['name' => $association->name]), 'status' => 'error']);
170
        }
171
    }
172
173
    /**
174
     * @param $id
175
     * @return JsonResponse
176
     */
177
    public function restore($id)
178
    {
179
        $association = Association::withTrashed()->find($id);
180
        if ($association->restore()) {
181
            return Response::json(['msg' => trans('msg.association_restored_successful', ['name' => $association->name]), 'status' => 'success']);
182
        } else {
183
            return Response::json(['msg' => trans('msg.association_restored_error', ['name' => $association->name]), 'status' => 'error']);
184
        }
185
    }
186
187
188
    public function changePresident()
189
    {
190
        // Open Transaction
191
        // Get the current president
192
        // Set the new president
193
        // Save
194
        // Close transaction
195
    }
196
}
197