Completed
Push — master ( 1bcff9...3c7575 )
by ARCANEDEV
04:01
created

PermissionsController::transNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

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.4285
c 0
b 0
f 0
cc 1
eloc 6
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
    use JsonResponses;
23
24
    /* -----------------------------------------------------------------
25
     |  Properties
26
     | -----------------------------------------------------------------
27
     */
28
    /** @var  \Arcanesoft\Contracts\Auth\Models\Permission  */
29
    protected $permission;
30
31
    /** @var int */
32
    protected $perPage = 30;
33
34
    /* -----------------------------------------------------------------
35
     |  Constructor
36
     | -----------------------------------------------------------------
37
     */
38
    /**
39
     * Instantiate the controller.
40
     *
41
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission  $permission
42
     */
43
    public function __construct(Permission $permission)
44
    {
45
        parent::__construct();
46
47
        $this->permission = $permission;
48
49
        $this->setCurrentPage('auth-permissions');
50
        $this->addBreadcrumbRoute(trans('auth::permissions.titles.permissions'), 'admin::auth.permissions.index');
51
    }
52
53
    /* -----------------------------------------------------------------
54
     |  Main Methods
55
     | -----------------------------------------------------------------
56
     */
57
    public function index()
58
    {
59
        $this->authorize(PermissionsPolicy::PERMISSION_LIST);
60
61
        $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...
62
            ->orderBy('group_id')
63
            ->paginate($this->perPage);
64
65
        $this->setTitle($title = trans('auth::permissions.titles.permissions-list'));
66
        $this->addBreadcrumb($title);
67
68
        return $this->view('admin.permissions.list', compact('permissions'));
69
    }
70
71
    public function group(PermissionsGroup $group)
72
    {
73
        $this->authorize(PermissionsPolicy::PERMISSION_LIST);
74
75
        $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...
76
77
        $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...
78
            ->with('group', 'roles')
79
            ->paginate($this->perPage);
80
81
        $this->addBreadcrumbRoute(trans('auth::permissions.titles.permissions-list'), 'admin::auth.permissions.index');
82
83
        $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...
84
        $this->setTitle($title = trans('auth::permissions.titles.permissions-list')." - $groupName");
85
        $this->addBreadcrumb($groupName);
86
87
        return $this->view('admin.permissions.list', compact('permissions'));
88
    }
89
90
    public function show(Permission $permission)
91
    {
92
        $this->authorize(PermissionsPolicy::PERMISSION_SHOW);
93
94
        $permission->load(['roles', 'roles.users']);
95
96
        $this->setTitle($title = 'Permission details');
97
        $this->addBreadcrumb($title);
98
99
        return $this->view('admin.permissions.show', compact('permission'));
100
    }
101
102
    public function detachRole(Permission $permission, Role $role)
103
    {
104
        $this->authorize(PermissionsPolicy::PERMISSION_UPDATE);
105
106
        try {
107
            $permission->detachRole($role, false);
108
109
            $replace = ['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...
110
            $context = ['role' => $role->toArray(), 'permissions' => $permission->toArray()];
0 ignored issues
show
Bug introduced by
The method toArray() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\Role>.

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...
Bug introduced by
The method toArray() 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...
111
112
            return $this->jsonResponseSuccess(
113
                $this->transNotification('detached', $replace, $context)
114
            );
115
        }
116
        catch(\Exception $e) {
117
            return $this->jsonResponseError($e->getMessage(), 500);
118
        }
119
    }
120
121
    /* -----------------------------------------------------------------
122
     |  Other Methods
123
     | -----------------------------------------------------------------
124
     */
125
    /**
126
     * Notify with translation.
127
     *
128
     * @param  string  $action
129
     * @param  array   $replace
130
     * @param  array   $context
131
     *
132
     * @return string
133
     */
134
    protected function transNotification($action, array $replace = [], array $context = [])
135
    {
136
        $title   = trans("auth::permissions.messages.{$action}.title");
137
        $message = trans("auth::permissions.messages.{$action}.message", $replace);
138
139
        Log::info($message, $context);
140
        $this->notifySuccess($message, $title);
141
142
        return $message;
143
    }
144
}
145