PermissionsController::transNotification()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Admin;
2
3
use Arcanedev\LaravelApiHelper\Traits\JsonResponses;
4
use Arcanesoft\Auth\Policies\PermissionsPolicy;
5
use Arcanesoft\Contracts\Auth\Models\Permission;
6
use Arcanesoft\Contracts\Auth\Models\PermissionsGroup;
7
use Arcanesoft\Contracts\Auth\Models\Role;
8
use Log;
9
10
/**
11
 * Class     PermissionsController
12
 *
13
 * @package  Arcanesoft\Auth\Http\Controllers\Admin
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class PermissionsController extends Controller
17
{
18
    /* -----------------------------------------------------------------
19
     |  Traits
20
     | -----------------------------------------------------------------
21
     */
22
23
    use JsonResponses;
24
25
    /* -----------------------------------------------------------------
26
     |  Properties
27
     | -----------------------------------------------------------------
28
     */
29
30
    /** @var  \Arcanesoft\Contracts\Auth\Models\Permission|\Arcanesoft\Auth\Models\Permission  */
31
    protected $permission;
32
33
    /** @var int */
34
    protected $perPage = 30;
35
36
    /* -----------------------------------------------------------------
37
     |  Constructor
38
     | -----------------------------------------------------------------
39
     */
40
41
    /**
42
     * Instantiate the controller.
43
     *
44
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission  $permission
45
     */
46
    public function __construct(Permission $permission)
47
    {
48
        parent::__construct();
49
50
        $this->permission = $permission;
51
52
        $this->setCurrentPage('auth-permissions');
53
        $this->addBreadcrumbRoute(trans('auth::permissions.titles.permissions'), 'admin::auth.permissions.index');
54
    }
55
56
    /* -----------------------------------------------------------------
57
     |  Main Methods
58
     | -----------------------------------------------------------------
59
     */
60
61
    public function index()
62
    {
63
        $this->authorize(PermissionsPolicy::PERMISSION_LIST);
64
65
        $permissions = $this->permission->with(['group'])
0 ignored issues
show
Bug introduced by
The method with does only exist in Arcanesoft\Auth\Models\Permission, but not in Arcanesoft\Contracts\Auth\Models\Permission.

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...
66
            ->withCount(['roles'])
67
            ->orderBy('group_id')
68
            ->paginate($this->perPage);
69
70
        $this->setTitle($title = trans('auth::permissions.titles.permissions-list'));
71
        $this->addBreadcrumb($title);
72
73
        return $this->view('admin.permissions.index', compact('permissions'));
74
    }
75
76
    public function group(PermissionsGroup $group)
77
    {
78
        $this->authorize(PermissionsPolicy::PERMISSION_LIST);
79
80
        $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...
81
82
        $permissions = $this->permission->where('group_id', $groupId)
83
            ->with('group', 'roles')
84
            ->paginate($this->perPage);
85
86
        $this->addBreadcrumbRoute(trans('auth::permissions.titles.permissions-list'), 'admin::auth.permissions.index');
87
88
        $groupName = $groupId == 0 ? trans('auth::permission-groups.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...
89
        $this->setTitle($title = trans('auth::permissions.titles.permissions-list')." - $groupName");
90
        $this->addBreadcrumb($groupName);
91
92
        return $this->view('admin.permissions.index', compact('permissions'));
93
    }
94
95
    public function show(Permission $permission)
96
    {
97
        $this->authorize(PermissionsPolicy::PERMISSION_SHOW);
98
99
        $permission->load(['roles', 'roles.users']);
100
101
        $this->setTitle($title = 'Permission details');
102
        $this->addBreadcrumb($title);
103
104
        return $this->view('admin.permissions.show', compact('permission'));
105
    }
106
107
    public function detachRole(Permission $permission, Role $role)
108
    {
109
        $this->authorize(PermissionsPolicy::PERMISSION_UPDATE);
110
111
        try {
112
            $permission->detachRole($role, false);
113
114
            $message = $this->transNotification(
115
                'detached',
116
                ['role' => $role->name,      'permission' => $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...
117
                ['role' => $role->toArray(), 'permissions' => $permission->toArray()]
118
            );
119
120
            return $this->jsonResponseSuccess(compact('message'));
121
        }
122
        catch(\Exception $e) {
123
            return $this->jsonResponseError(['message' => $e->getMessage()], 500);
124
        }
125
    }
126
127
    /* -----------------------------------------------------------------
128
     |  Other Methods
129
     | -----------------------------------------------------------------
130
     */
131
132
    /**
133
     * Notify with translation.
134
     *
135
     * @param  string  $action
136
     * @param  array   $replace
137
     * @param  array   $context
138
     *
139
     * @return string
140
     */
141
    protected function transNotification($action, array $replace = [], array $context = [])
142
    {
143
        $title   = trans("auth::permissions.messages.{$action}.title");
144
        $message = trans("auth::permissions.messages.{$action}.message", $replace);
145
146
        Log::info($message, $context);
147
        $this->notifySuccess($message, $title);
148
149
        return $message;
150
    }
151
}
152