Completed
Push — master ( f40498...e3ae3a )
by ARCANEDEV
03:17
created

PermissionsController::detachRole()   B

Complexity

Conditions 2
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
ccs 0
cts 22
cp 0
rs 8.8571
cc 2
eloc 16
nc 4
nop 2
crap 6
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Foundation;
2
3
use Arcanesoft\Auth\Bases\FoundationController;
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\Foundation
13
 * @author   ARCANEDEV <[email protected]>
14
 *
15
 * @todo: Adding the authorization checks
16
 */
17
class PermissionsController extends FoundationController
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Properties
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    /** @var  \Arcanesoft\Contracts\Auth\Models\Permission  */
24
    protected $permission;
25
26
    /** @var int */
27
    protected $perPage = 30;
28
29
    /* ------------------------------------------------------------------------------------------------
30
     |  Constructor
31
     | ------------------------------------------------------------------------------------------------
32
     */
33
    /**
34
     * Instantiate the controller.
35
     *
36
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission  $permission
37
     */
38
    public function __construct(Permission $permission)
39
    {
40
        parent::__construct();
41
42
        $this->permission = $permission;
43
44
        $this->setCurrentPage('auth-permissions');
45
        $this->addBreadcrumbRoute('Permissions', 'auth::foundation.permissions.index');
46
    }
47
48
    /* ------------------------------------------------------------------------------------------------
49
     |  Main Functions
50
     | ------------------------------------------------------------------------------------------------
51
     */
52
    public function index()
53
    {
54
        $permissions = $this->permission->with('group', 'roles')
55
            ->orderBy('group_id')
56
            ->paginate($this->perPage);
57
58
        $title = 'List of permissions';
59
        $this->setTitle($title);
60
        $this->addBreadcrumb($title);
61
62
        return $this->view('foundation.permissions.list', compact('permissions'));
63
    }
64
65
    public function group(PermissionsGroup $group)
66
    {
67
        $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...
68
69
        $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...
70
            ->with('group', 'roles')
71
            ->paginate($this->perPage);
72
73
        $this->addBreadcrumbRoute('List of permissions', 'auth::foundation.permissions.index');
74
75
        $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...
76
        $title     = "List of permissions - $groupName";
77
        $this->setTitle($title);
78
        $this->addBreadcrumb($groupName);
79
80
        return $this->view('foundation.permissions.list', compact('permissions'));
81
    }
82
83
    public function show(Permission $permission)
84
    {
85
        $permission->load(['roles', 'roles.users']);
86
87
        $title = 'Permission details';
88
        $this->setTitle($title);
89
        $this->addBreadcrumb($title);
90
91
        return $this->view('foundation.permissions.show', compact('permission'));
92
    }
93
94
    public function detachRole(Permission $permission, Role $role)
95
    {
96
        self::onlyAjax();
97
98
        try {
99
            $permission->detachRole($role, false);
100
101
            $title   = 'Role detached !';
102
            $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...
103
104
            Log::info($message, compact('permission', 'role'));
105
            $this->notifySuccess($message, $title);
106
107
            $ajax = [
108
                'status'  => 'success',
109
                'message' => $message,
110
            ];
111
        }
112
        catch(\Exception $e) {
113
            $ajax = [
114
                'status'  => 'error',
115
                'message' => $e->getMessage(),
116
            ];
117
        }
118
119
        return response()->json($ajax);
120
    }
121
}
122