Completed
Push — master ( 720025...0e57e6 )
by ARCANEDEV
04:07
created

PermissionsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 109
c 0
b 0
f 0
ccs 0
cts 61
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A index() 0 13 1
A group() 0 18 3
A show() 0 11 1
B detachRole() 0 28 2
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Admin;
2
3
use Arcanesoft\Auth\Policies\PermissionsPolicy;
4
use Arcanesoft\Contracts\Auth\Models\Permission;
5
use Arcanesoft\Contracts\Auth\Models\PermissionsGroup;
6
use Arcanesoft\Contracts\Auth\Models\Role;
7
use Log;
8
9
/**
10
 * Class     PermissionsController
11
 *
12
 * @package  Arcanesoft\Auth\Http\Controllers\Admin
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class PermissionsController extends Controller
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /** @var  \Arcanesoft\Contracts\Auth\Models\Permission  */
22
    protected $permission;
23
24
    /** @var int */
25
    protected $perPage = 30;
26
27
    /* ------------------------------------------------------------------------------------------------
28
     |  Constructor
29
     | ------------------------------------------------------------------------------------------------
30
     */
31
    /**
32
     * Instantiate the controller.
33
     *
34
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission  $permission
35
     */
36
    public function __construct(Permission $permission)
37
    {
38
        parent::__construct();
39
40
        $this->permission = $permission;
41
42
        $this->setCurrentPage('auth-permissions');
43
        $this->addBreadcrumbRoute('Permissions', 'admin::auth.permissions.index');
44
    }
45
46
    /* ------------------------------------------------------------------------------------------------
47
     |  Main Functions
48
     | ------------------------------------------------------------------------------------------------
49
     */
50
    public function index()
51
    {
52
        $this->authorize(PermissionsPolicy::PERMISSION_LIST);
53
54
        $permissions = $this->permission->with('group', 'roles')
0 ignored issues
show
Bug introduced by
The method with() does not seem to exist on object<Arcanesoft\Contra...Auth\Models\Permission>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            ->orderBy('group_id')
56
            ->paginate($this->perPage);
57
58
        $this->setTitle($title = 'List of permissions');
59
        $this->addBreadcrumb($title);
60
61
        return $this->view('admin.permissions.list', compact('permissions'));
62
    }
63
64
    public function group(PermissionsGroup $group)
65
    {
66
        $this->authorize(PermissionsPolicy::PERMISSION_LIST);
67
68
        $groupId = $group->id ? $group->id : 0;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Arcanesoft\Contracts\Auth\Models\PermissionsGroup suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
69
70
        $permissions = $this->permission->where('group_id', $groupId)
0 ignored issues
show
Bug introduced by
The method where() does not seem to exist on object<Arcanesoft\Contra...Auth\Models\Permission>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            ->with('group', 'roles')
72
            ->paginate($this->perPage);
73
74
        $groupName = $groupId == 0 ? 'Custom' : $group->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface Arcanesoft\Contracts\Auth\Models\PermissionsGroup suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
75
76
        $this->setTitle($title = "List of permissions - $groupName");
77
        $this->addBreadcrumbRoute('List of permissions', 'admin::auth.permissions.index');
78
        $this->addBreadcrumb($groupName);
79
80
        return $this->view('admin.permissions.list', compact('permissions'));
81
    }
82
83
    public function show(Permission $permission)
84
    {
85
        $this->authorize(PermissionsPolicy::PERMISSION_SHOW);
86
87
        $permission->load(['roles', 'roles.users']);
88
89
        $this->setTitle($title = 'Permission details');
90
        $this->addBreadcrumb($title);
91
92
        return $this->view('admin.permissions.show', compact('permission'));
93
    }
94
95
    public function detachRole(Permission $permission, Role $role)
96
    {
97
        self::onlyAjax();
98
        $this->authorize(PermissionsPolicy::PERMISSION_UPDATE);
99
100
        try {
101
            $permission->detachRole($role, false);
102
103
            $title   = 'Role detached !';
104
            $message = "The role {$role->name} has been successfully detached from {$permission->name} !";
0 ignored issues
show
Bug introduced by
Accessing name on the interface Arcanesoft\Contracts\Auth\Models\Role suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing name on the interface Arcanesoft\Contracts\Auth\Models\Permission suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
105
106
            Log::info($message, compact('permission', 'role'));
107
            $this->notifySuccess($message, $title);
108
109
            $ajax = [
110
                'status'  => 'success',
111
                'message' => $message,
112
            ];
113
        }
114
        catch(\Exception $e) {
115
            $ajax = [
116
                'status'  => 'error',
117
                'message' => $e->getMessage(),
118
            ];
119
        }
120
121
        return response()->json($ajax);
122
    }
123
}
124