Issues (54)

src/Http/Controllers/RoleController.php (10 issues)

1
<?php
2
3
namespace PhpCollective\MenuMaker\Http\Controllers;
4
5
use Illuminate\Routing\Controller;
6
use PhpCollective\MenuMaker\Storage\Menu;
7
use PhpCollective\MenuMaker\Storage\Role;
8
use PhpCollective\MenuMaker\Jobs\RemoveUserMenuCache;
9
use PhpCollective\MenuMaker\Http\Requests\MenuRoleRequest;
10
use PhpCollective\MenuMaker\Http\Requests\RoleRequest as Request;
11
12
class RoleController extends Controller
13
{
14
    /**
15
     * Display a listing of the resource.
16
     *
17
     * @return \Illuminate\Http\Response
18
     */
19
    public function index()
20
    {
21
        $roles = Role::withoutGlobalScopes()->paginate();
22
        return view('menu-maker::roles.index', compact('roles'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('menu-maker:...dex', compact('roles')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
23
    }
24
25
    /**
26
     * Show the form for creating a new resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function create()
31
    {
32
        return view('menu-maker::roles.create');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('menu-maker::roles.create') returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
33
    }
34
35
    /**
36
     * Store a newly created resource in storage.
37
     *
38
     * @param  Request  $request
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function store(Request $request)
42
    {
43
        Role::create($request->all());
44
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...e' => $request->name))) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
45
            ->route('menu-maker::roles.index')
46
            ->withMessage(__('menu-maker::alerts.created', ['name' => $request->name]));
47
    }
48
49
    /**
50
     * Display the specified resource.
51
     *
52
     * @param  int $id
53
     * @return \Illuminate\Http\Response
54
     */
55
    public function show($id)
56
    {
57
        $role = Role::withoutGlobalScopes()->findOrFail($id);
58
        return view('menu-maker::roles.show', compact('role'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('menu-maker:...show', compact('role')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
59
    }
60
61
    /**
62
     * Show the form for editing the specified resource.
63
     *
64
     * @param  int $id
65
     * @return \Illuminate\Http\Response
66
     */
67
    public function edit($id)
68
    {
69
        $role = Role::withoutGlobalScopes()->findOrFail($id);
70
        return view('menu-maker::roles.edit', compact('role'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('menu-maker:...edit', compact('role')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
71
    }
72
73
    /**
74
     * Update the specified resource in storage.
75
     *
76
     * @param  Request  $request
77
     * @param  int $id
78
     * @return \Illuminate\Http\Response
79
     */
80
    public function update(Request $request, $id)
81
    {
82
        $role = Role::withoutGlobalScopes()->findOrFail($id);
83
        $role->update($request->all());
84
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->to($r...e' => $request->name))) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
85
            ->to($request->redirects_to)
86
            ->withMessage(__('menu-maker::alerts.updated', ['name' => $request->name]));
87
    }
88
89
    /**
90
     * Remove the specified resource from storage.
91
     *
92
     * @param  int $id
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function destroy($id)
96
    {
97
        $role = Role::withoutGlobalScopes()->findOrFail($id);
98
        $name = $role->name;
99
        $users = $role->users()->count();
100
        if ($users > 0) {
101
            return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->to(re... . ' active user(s).')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
102
                ->to(request('redirects_to'))
0 ignored issues
show
It seems like request('redirects_to') can also be of type array; however, parameter $path of Illuminate\Routing\Redirector::to() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

102
                ->to(/** @scrutinizer ignore-type */ request('redirects_to'))
Loading history...
103
                ->withErrors(__($name . ' role has ' . $users . ' active user(s).'));
104
        }
105
        
106
        $role->delete();
107
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->to(re...rray('name' => $name))) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
108
            ->to(request('redirects_to'))
109
            ->withMessage(__('menu-maker::alerts.deleted', ['name' => $name]));
110
    }
111
112
    public function menus()
113
    {
114
        $roles = Role::admin(false)->pluck('name', 'id');
115
        $sections = Menu::sections()->pluck('name', 'id');
116
        return view('menu-maker::roles.menus', compact('roles', 'sections'));
117
    }
118
119
    /**
120
     * Update the specified resource in storage.
121
     *
122
     * @param MenuRoleRequest $request
123
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
124
     */
125
    public function assign(MenuRoleRequest $request)
126
    {
127
        $role = Role::findOrFail($request->role_id);
128
        $previous_ids = $role->menus()->descendantsOf($request->section_id)->pluck('id')->toArray();
129
        if(count($previous_ids) > 0)
130
        {
131
            $role->menus()->detach($previous_ids);
132
        }
133
        $role->menus()->attach($request->menu_ids);
134
135
        $role->users->each(function ($user) {
0 ignored issues
show
The property users does not seem to exist on PhpCollective\MenuMaker\Storage\Role. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
136
            RemoveUserMenuCache::dispatch($user);
137
        });
138
139
        return redirect()
140
            ->back()
141
            ->withMessage(__('menu-maker::alerts.updated', ['name' => $role->name]));
142
    }
143
}
144