PermissionController::store()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 2
nop 1
dl 0
loc 21
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Controllers\Back\Authorization;
4
5
use Illuminate\Http\Request;
6
use Thinktomorrow\Chief\Admin\Authorization\Permission;
7
use Thinktomorrow\Chief\Admin\Authorization\Role;
8
use Thinktomorrow\Chief\App\Http\Controllers\Controller;
9
10
class PermissionController extends Controller
11
{
12
    /**
13
     * Display a listing of the resource.
14
     *
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function index()
18
    {
19
        $permissions = Permission::all();
20
21
        return view('chief::admin.permissions.index')->with('permissions', $permissions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('chief::admi...issions', $permissions) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
22
    }
23
    /**
24
     * Show the form for creating a new resource.
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28
    public function create()
29
    {
30
        $roles = Role::get();
31
32
        return view('chief::admin.permissions.create')->with('roles', $roles);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('chief::admi...->with('roles', $roles) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
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)
41
    {
42
        $this->validate($request, [
43
            'name' => 'required|max:40',
44
        ]);
45
        $name = $request['name'];
46
        $permission = new Permission();
47
        $permission->name = $name;
48
        $roles = $request['roles'];
49
50
        $permission->save();
51
        if (! empty($request['roles'])) {
52
            foreach ($roles as $role) {
53
                $r = Role::where('id', '=', $role)->firstOrFail(); //Match input role to db record
54
                $permission = Permission::where('name', '=', $name)->first();
55
                $r->givePermissionTo($permission);
56
            }
57
        }
58
59
        return redirect()->route('chief.back.permissions.index')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...sion->name . ' added!') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
60
            ->with('flash_message',     'Permission' . $permission->name . ' added!');
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
61
    }
62
    /**
63
     * Display the specified resource.
64
     *
65
     * @param int  $id
66
     *
67
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
68
     */
69
    public function show($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

69
    public function show(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71
        return redirect('permissions');
72
    }
73
    /**
74
     * Show the form for editing the specified resource.
75
     *
76
     * @param int  $id
77
     *
78
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
79
     */
80
    public function edit($id)
81
    {
82
        $permission = Permission::find($id);
83
84
        return view('chief::admin.permissions.edit', compact('permission'));
85
    }
86
    /**
87
     * Update the specified resource in storage.
88
     *
89
     * @param  \Illuminate\Http\Request  $request
90
     * @param  int  $id
91
     * @return \Illuminate\Http\Response
92
     */
93
    public function update(Request $request, $id)
94
    {
95
        $permission = Permission::findOrFail($id);
96
        $this->validate($request, [
97
            'name' => 'required|max:40',
98
        ]);
99
100
        $input = $request->all();
101
        $permission->fill($input)->save();
102
103
        return redirect()->route('chief.back.permissions.index')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...on->name . ' updated!') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
104
            ->with('flash_message',     'Permission' . $permission->name . ' updated!');
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
105
    }
106
    /**
107
     * Remove the specified resource from storage.
108
     *
109
     * @param  int  $id
110
     * @return \Illuminate\Http\Response
111
     */
112
    public function destroy($id)
113
    {
114
        $permission = Permission::findOrFail($id);
115
116
        if ($permission->name == "Administer roles & permissions") {
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
117
            return redirect()->route('chief.back.permissions.index')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...lete this Permission!') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
118
                ->with('flash_message',         'Cannot delete this Permission!');
119
        }
120
121
        $permission->delete();
122
123
        return redirect()->route('chief.back.permissions.index')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route... 'Permission deleted!') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
124
            ->with('flash_message',     'Permission deleted!');
125
    }
126
}
127