Completed
Push — master ( 8de0b5...8c3012 )
by Faysal
01:14
created

AdminController::restore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Devfaysal\LaravelAdmin\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Yajra\DataTables\DataTables;
7
use Spatie\Permission\Models\Role;
8
use Illuminate\Support\Facades\Hash;
9
use Illuminate\Support\Facades\Session;
10
use Devfaysal\LaravelAdmin\Models\Admin;
11
use Spatie\Permission\Models\Permission;
12
use Illuminate\Foundation\Bus\DispatchesJobs;
13
use Illuminate\Routing\Controller as BaseController;
14
use Illuminate\Foundation\Validation\ValidatesRequests;
15
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
16
17
class AdminController extends BaseController
18
{
19
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
20
21
    public function index()
22
    {
23
        $admins = auth()->user()->hasPermissionTo('manage_trashed_admins', 'admin') ? Admin::withTrashed()->get() : Admin::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...
Bug introduced by
The method get does only exist in Illuminate\Database\Eloq...\Database\Query\Builder, but not in Illuminate\Database\Eloquent\SoftDeletes.

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...
24
25
        return view('laravel-admin::admins.index', [
26
27
            'admins' => $admins
28
29
        ]);
30
    }
31
32
    public function show(Admin $admin)
33
    {
34
        return view('laravel-admin::admins.show', [
35
36
            'admin' => $admin
37
38
        ]);
39
    }
40
41 View Code Duplication
    public function create()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $permissions = Permission::where('guard_name', 'admin')->get();
44
        $roles = Role::where('guard_name', 'admin')->get();
45
        return view('laravel-admin::admins.create', [
46
            'roles' => $roles->pluck('name'),
47
            'permissions' => $permissions->pluck('name')
48
        ]);
49
    }
50
51
    public function store(Request $request)
52
    {
53
        $request->validate([
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
54
            'name' => 'required',
55
            'email' => 'required|email|unique:admins',
56
            'password' => 'required',
57
        ]);
58
59
        $admin = Admin::create([
60
            'name' => $request->name,
61
            'email' => $request->email,
62
            'password' => Hash::make($request->password),
63
        ]);
64
        
65
        if($request->roles){
66
            $admin->assignRole($request->roles);
67
        }
68
69
        if($request->permissions){
70
            $admin->givePermissionTo($request->permissions);
71
        }
72
        
73
74
        Session::flash('message', 'Admin created Successfully!!'); 
75
        Session::flash('alert-class', 'alert-success');
76
77
        return redirect('/admin/admins');
78
    }
79
80 View Code Duplication
    public function edit(Admin $admin)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $permissions = Permission::where('guard_name', 'admin')->get();
83
        $roles = Role::where('guard_name', 'admin')->get();
84
        return view('laravel-admin::admins.edit', [
85
            'admin' => $admin,
86
            'roles' => $roles->pluck('name'),
87
            'permissions' => $permissions->pluck('name')
88
        ]);
89
    }
90
91
    public function update(Request $request, Admin $admin)
92
    {
93
        $attributes = $request->validate([
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
94
            'name' => 'required',
95
            'email' => 'required|email|unique:admins,email,' . $admin->id
96
        ]);
97
98
        if($request->password){
99
            $attributes['password'] = Hash::make($request->password);
100
        }
101
102
        $admin->update($attributes);
103
104
        if($request->roles){
105
            $admin->syncRoles($request->roles);
106
        }
107
        if($request->permissions){
108
            $admin->syncPermissions($request->permissions);
109
        }
110
        
111
        Session::flash('message', 'Admin updated Successfully!!'); 
112
        Session::flash('alert-class', 'alert-success');
113
114
        return redirect('/admin/admins');
115
    }
116
117
    public function destroy(Admin $admin)
118
    {
119
        $admin->delete();
120
121
        Session::flash('message', 'Admin deleted Successfully!!'); 
122
        Session::flash('alert-class', 'alert-success');
123
124
        return redirect('/admin/admins');
125
    }
126
127
    public function restore($id)
128
    {
129
        $admin = Admin::withTrashed()->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloq...\Database\Query\Builder.

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...
130
        $admin->restore();
131
132
        Session::flash('message', 'Admin restored Successfully!!'); 
133
        Session::flash('alert-class', 'alert-success');
134
135
        return redirect('/admin/admins');
136
    }
137
138
    public function datatable()
139
    {
140
        $admins = auth()->user()->hasPermissionTo('manage_trashed_admins', 'admin') ? Admin::withTrashed()->get() : Admin::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...
Bug introduced by
The method get does only exist in Illuminate\Database\Eloq...\Database\Query\Builder, but not in Illuminate\Database\Eloquent\SoftDeletes.

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...
141
142
        return DataTables::of($admins)
143
            ->addColumn('action', function($admin) {
144
                $string = '';
145
                if($admin->trashed()){
146
                    $string .= '<a class="btn btn-sm btn-oval btn-warning" href="'. route('admins.restore', $admin->id) .'">Restore</a> ';
147
                }
148
                $string .= '<a class="btn btn-sm btn-oval btn-info" href="'. route('admins.edit', $admin->id) .'">Edit</a>';
149
                $string .= ' <a class="btn btn-sm btn-oval btn-primary" href="'. route('admins.show', $admin->id) .'">Show</a>';
150
                return $string;
151
            })
152
            ->addColumn('roles', function($admin) {
153
                $string = '';
154
                foreach ($admin->roles as $role){
155
                    $string .= '<span class="badge badge-success">'. $role->name .'</span> ';
156
                }
157
                return $string;
158
            })
159
            ->rawColumns(['action', 'roles'])
160
            ->make(true);
161
    }
162
}
163