Completed
Push — master ( 7d4954...7d4080 )
by Faysal
05:08
created

UserController::restore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Devfaysal\LaravelAdmin\Http\Controllers;
4
5
use App\User;
6
use Illuminate\Http\Request;
7
use Yajra\DataTables\DataTables;
8
use Spatie\Permission\Models\Role;
9
use App\Http\Controllers\Controller;
10
use Illuminate\Support\Facades\Hash;
11
use Illuminate\Support\Facades\Session;
12
use Spatie\Permission\Models\Permission;
13
14
class UserController extends Controller
15
{
16
    public function index()
17
    {
18
        $users = auth()->user()->hasPermissionTo('manage_trashed_users') ? User::withTrashed()->get() : User::all();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
19
20
        return view('laravel-admin::users.index', [
21
22
            'users' => $users
23
24
        ]);
25
    }
26
27
    public function show(User $user)
28
    {
29
        return view('laravel-admin::users.show', [
30
31
            'user' => $user
32
33
        ]);
34
    }
35
36
    public function create()
37
    {
38
        $permissions = Permission::all();
39
        $roles = Role::all();
40
        return view('laravel-admin::users.create', [
41
            'roles' => $roles,
42
            'permissions' => $permissions
43
        ]);
44
    }
45
46
    public function store(Request $request)
47
    {
48
        $request->validate([
49
            'name' => 'required',
50
            'email' => 'required|email|unique:users',
51
            'password' => 'required',
52
        ]);
53
54
        $user = User::create([
55
            'name' => $request->name,
56
            'email' => $request->email,
57
            'password' => Hash::make($request->password),
58
        ]);
59
        
60
        if($request->roles){
61
            $user->assignRole($request->roles);
62
        }
63
64
        if($request->permissions){
65
            $user->givePermissionTo($request->permissions);
66
        }
67
        
68
69
        Session::flash('message', 'User created Successfully!!'); 
70
        Session::flash('alert-class', 'alert-success');
71
72
        return redirect('/admin/users');
73
    }
74
75
    public function edit(User $user)
76
    {
77
        $permissions = Permission::all();
78
        $roles = Role::all();
79
        return view('laravel-admin::users.edit', [
80
            'user' => $user,
81
            'roles' => $roles,
82
            'permissions' => $permissions
83
        ]);
84
    }
85
86
    public function update(Request $request, User $user)
87
    {
88
        $request->validate([
89
            'name' => 'required',
90
            'email' => 'required|email|unique:users,email,' . $user->id
91
        ]);
92
93
        $user->update([
94
            'name' => $request->name,
95
            'email' => $request->email,
96
        ]);
97
98
        if($request->roles){
99
            $user->syncRoles($request->roles);
100
        }
101
        if($request->permissions){
102
            $user->syncPermissions($request->permissions);
103
        }
104
        
105
        Session::flash('message', 'User updated Successfully!!'); 
106
        Session::flash('alert-class', 'alert-success');
107
108
        return redirect('/admin/users');
109
    }
110
111
    public function destroy(User $user)
112
    {
113
        $user->delete();
114
115
        Session::flash('message', 'User deleted Successfully!!'); 
116
        Session::flash('alert-class', 'alert-success');
117
118
        return redirect('/admin/users');
119
    }
120
121
    public function restore($id)
122
    {
123
        $user = User::withTrashed()->findOrFail($id);
124
        $user->restore();
125
126
        Session::flash('message', 'User restored Successfully!!'); 
127
        Session::flash('alert-class', 'alert-success');
128
129
        return redirect('/admin/users');
130
    }
131
132
    public function datatable()
133
    {
134
        $users = auth()->user()->hasPermissionTo('manage_trashed_users') ? User::withTrashed()->get() : User::all();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
135
136
        return DataTables::of($users)
137
            ->addColumn('action', function($user) {
138
                $string = '';
139
                if($user->trashed()){
140
                    $string .= '<a class="btn btn-sm btn-oval btn-warning" href="'. route('users.restore', $user->id) .'">Restore</a> ';
141
                }
142
                $string .= '<a class="btn btn-sm btn-oval btn-info" href="'. route('users.edit', $user->id) .'">Edit</a>';
143
                $string .= ' <a class="btn btn-sm btn-oval btn-primary" href="'. route('users.show', $user->id) .'">Show</a>';
144
                return $string;
145
            })
146
            ->addColumn('roles', function($user) {
147
                $string = '';
148
                foreach ($user->roles as $role){
149
                    $string .= '<span class="badge badge-success">'. $role->name .'</span> ';
150
                }
151
                return $string;
152
            })
153
            ->rawColumns(['action', 'roles'])
154
            ->make(true);
155
    }
156
}
157