Issues (69)

src/Http/Controllers/AbilitiesController.php (11 issues)

1
<?php
2
3
namespace Thinkstudeo\Rakshak\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Thinkstudeo\Rakshak\Ability;
7
8
class AbilitiesController extends Controller
9
{
10
    /**
11
     * Validation rules.
12
     *
13
     * @param Ability $ability
14
     * @return mixed
15
     */
16
    protected function rules($ability)
17
    {
18
        return [
19
            'name'        => ['required', 'string', 'unique:abilities,name,'.$ability->id ?: null, 'min:3', 'max:255'],
20
            'label'       => ['required', 'string'],
21
            'description' => ['required', 'string', 'max:255'],
22
            'active'      => ['nullable', 'boolean'],
23
        ];
24
    }
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function index()
32
    {
33
        $this->authorize('index', Ability::class);
34
35
        $abilities = Ability::all();
36
37
        return view('rakshak::abilities.index', compact('abilities'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('rakshak::ab..., compact('abilities')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
38
    }
39
40
    /**
41
     * Show the form for creating a new resource.
42
     *
43
     * @return \Illuminate\Http\Response
44
     */
45
    public function create()
46
    {
47
        $ability = new Ability();
48
49
        return view('rakshak::abilities.create', compact('ability'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('rakshak::ab...e', compact('ability')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
50
    }
51
52
    /**
53
     * Store a newly created resource in storage.
54
     *
55
     * @param  \Illuminate\Http\Request  request
56
     * @return \Illuminate\Http\Response
57
     */
58
    public function store(Request $request)
59
    {
60
        $this->authorize('create', Ability::class);
61
62
        $validated = $request->validate($this->rules($request));
0 ignored issues
show
$request of type Illuminate\Http\Request is incompatible with the type Thinkstudeo\Rakshak\Ability expected by parameter $ability of Thinkstudeo\Rakshak\Http...tiesController::rules(). ( Ignorable by Annotation )

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

62
        $validated = $request->validate($this->rules(/** @scrutinizer ignore-type */ $request));
Loading history...
63
64
        $ability = Ability::create($validated);
65
66
        if ($request->expectsJson()) {
67
            return response()->json(['message' => "New ability {$ability->name} has been created.", 'record' => $ability], 201);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...ord' => $ability), 201) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
68
        }
69
70
        return redirect(route('rakshak.abilities.index'))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('r...e.' has been created.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
71
            ->with('status', 'success')
72
            ->with('message', "Ability {$ability->name} has been created.");
73
    }
74
75
    /**
76
     * Show the form for editing the specified resource.
77
     *
78
     * @param  Ability $ability
79
     * @return \Illuminate\Http\Response
80
     */
81
    public function edit(Ability $ability)
82
    {
83
        $this->authorize('update', $ability);
84
85
        return view('rakshak::abilities.edit', compact('ability'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('rakshak::ab...t', compact('ability')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
86
    }
87
88
    /**
89
     * Update the specified resource in storage.
90
     *
91
     * @param  \Illuminate\Http\Request  $request
92
     * @param  Thinkstudeo\Rakshak\Ability $ability
0 ignored issues
show
The type Thinkstudeo\Rakshak\Http...kstudeo\Rakshak\Ability was not found. Did you mean Thinkstudeo\Rakshak\Ability? If so, make sure to prefix the type with \.
Loading history...
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function update(Request $request, Ability $ability)
96
    {
97
        $this->authorize('update', $ability);
98
99
        $validated = $request->validate($this->rules($ability));
100
101
        $ability = tap($ability)->update($validated);
102
103
        if ($request->expectsJson()) {
104
            return response()->json(['message' => "The ability {$ability->name} has been updated.", 'record' => $ability], 200);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...ord' => $ability), 200) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
105
        }
106
107
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...updated successfully.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
108
            ->back()
109
            ->with('status', 'success')
110
            ->with('message', "Ability {$ability->name} updated successfully.");
111
    }
112
113
    /**
114
     * Remove the specified resource from storage.
115
     *
116
     * @param  Thinkstudeo\Rakshak\Ability $ability
117
     * @return \Illuminate\Http\Response
118
     */
119
    public function destroy(Ability $ability)
120
    {
121
        $ability->delete();
122
123
        if (request()->expectsJson()) {
124
            return response()->json(['message' => "The ability {$ability->name} has been deleted."]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(....' has been deleted.')) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
125
        }
126
127
        return redirect(route('rakshak.abilities.index'))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('r...e.' has been deleted.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
128
            ->with('status', 'success')
129
            ->with('message', "Ability {$ability->name} has been deleted.");
130
    }
131
}
132