Completed
Push — master ( 6f780f...49672a )
by ARCANEDEV
04:44
created

RolesController::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\Http\Requests\Admin\Roles\CreateRoleRequest;
5
use Arcanesoft\Auth\Http\Requests\Admin\Roles\UpdateRoleRequest;
6
use Arcanesoft\Auth\Policies\RolesPolicy;
7
use Arcanesoft\Contracts\Auth\Models\Role;
8
use Log;
9
10
/**
11
 * Class     RolesController
12
 *
13
 * @package  Arcanesoft\Auth\Http\Controllers\Admin
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class RolesController extends Controller
17
{
18
    /* -----------------------------------------------------------------
19
     |  Traits
20
     | -----------------------------------------------------------------
21
     */
22
    use JsonResponses;
23
24
    /* -----------------------------------------------------------------
25
     |  Properties
26
     | -----------------------------------------------------------------
27
     */
28
    /**
29
     * The Role model.
30
     *
31
     * @var \Arcanesoft\Contracts\Auth\Models\Role
32
     */
33
    protected $role;
34
35
    /* -----------------------------------------------------------------
36
     |  Constructor
37
     | -----------------------------------------------------------------
38
     */
39
    /**
40
     * Instantiate the controller.
41
     *
42
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
43
     */
44
    public function __construct(Role $role)
45
    {
46
        parent::__construct();
47
48
        $this->role = $role;
49
50
        $this->setCurrentPage('auth-roles');
51
        $this->addBreadcrumbRoute(trans('auth::roles.titles.roles'), 'admin::auth.roles.index');
52
    }
53
54
    /* -----------------------------------------------------------------
55
     |  Main Methods
56
     | -----------------------------------------------------------------
57
     */
58
    public function index()
59
    {
60
        $this->authorize(RolesPolicy::PERMISSION_LIST);
61
62
        $roles = $this->role->with('users', 'permissions')->paginate(30);
0 ignored issues
show
Bug introduced by
The method with() 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...
63
64
        $this->setTitle($title = trans('auth::roles.titles.roles-list'));
65
        $this->addBreadcrumb($title);
66
67
        return $this->view('admin.roles.list', compact('roles'));
68
    }
69
70
    public function create()
71
    {
72
        $this->authorize(RolesPolicy::PERMISSION_CREATE);
73
74
        $this->setTitle($title = trans('auth::roles.titles.create-role'));
75
        $this->addBreadcrumb($title);
76
77
        return $this->view('admin.roles.create');
78
    }
79
80
    public function store(CreateRoleRequest $request)
81
    {
82
        $this->authorize(RolesPolicy::PERMISSION_CREATE);
83
84
        $this->role->fill($request->only('name', 'slug', 'description'));
0 ignored issues
show
Bug introduced by
The method fill() 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...
85
        $this->role->save();
86
        $this->role->permissions()->attach($request->get('permissions'));
87
88
        $this->transNotification('created', ['name' => $this->role->name], $this->role->toArray());
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
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...
89
90
        return redirect()
91
            ->route('admin::auth.roles.index');
92
    }
93
94
    public function show(Role $role)
95
    {
96
        $this->authorize(RolesPolicy::PERMISSION_SHOW);
97
98
        /** @var  \Arcanesoft\Auth\Models\Role  $role */
99
        $role->load(['users', 'permissions', 'permissions.group']);
100
101
        $this->setTitle($title = trans('auth::roles.titles.role-details'));
102
        $this->addBreadcrumb($title);
103
104
        return $this->view('admin.roles.show', compact('role'));
105
    }
106
107
    public function edit(Role $role)
108
    {
109
        $this->authorize(RolesPolicy::PERMISSION_UPDATE);
110
111
        /** @var  \Arcanesoft\Auth\Models\Role  $role */
112
        $role->load(['users', 'permissions']);
113
114
        $this->setTitle($title = trans('auth::roles.titles.edit-role'));
115
        $this->addBreadcrumb($title);
116
117
        return $this->view('admin.roles.edit', compact('role'));
118
    }
119
120
    public function update(UpdateRoleRequest $request, Role $role)
121
    {
122
        $this->authorize(RolesPolicy::PERMISSION_UPDATE);
123
124
        /** @var  \Arcanesoft\Auth\Models\Role  $role */
125
        $role->fill($request->only('name', 'slug', 'description'));
126
        $role->save();
127
        $role->permissions()->sync($request->get('permissions'));
128
129
        $this->transNotification('updated', ['name' => $role->name], $role->toArray());
130
131
        return redirect()
132
            ->route('admin::auth.roles.show', [$role->hashed_id]);
133
    }
134
135
    public function activate(Role $role)
136
    {
137
        $this->authorize(RolesPolicy::PERMISSION_UPDATE);
138
139
        try {
140
            /** @var  \Arcanesoft\Auth\Models\Role  $role */
141
            ($active = $role->isActive()) ? $role->deactivate() : $role->activate();
142
143
            return $this->jsonResponseSuccess(
144
                $this->transNotification($active ? 'disabled' : 'enabled', ['name' => $role->name], $role->toArray())
145
            );
146
        }
147
        catch(\Exception $e) {
148
            return $this->jsonResponseError($e->getMessage(), 500);
149
        }
150
    }
151
152
    public function delete(Role $role)
153
    {
154
        /** @var  \Arcanesoft\Auth\Models\Role  $role */
155
        $this->authorize(RolesPolicy::PERMISSION_DELETE);
156
157
        try {
158
            $role->delete();
159
160
            return $this->jsonResponseSuccess(
161
                $this->transNotification('deleted', ['name' => $role->name], $role->toArray())
162
            );
163
        }
164
        catch(\Exception $e) {
165
            return $this->jsonResponseError($e->getMessage(), 500);
166
        }
167
    }
168
169
    /* -----------------------------------------------------------------
170
     |  Other Methods
171
     | -----------------------------------------------------------------
172
     */
173
    /**
174
     * Notify with translation.
175
     *
176
     * @param  string  $action
177
     * @param  array   $replace
178
     * @param  array   $context
179
     *
180
     * @return string
181
     */
182
    protected function transNotification($action, array $replace = [], array $context = [])
183
    {
184
        $title   = trans("auth::roles.messages.{$action}.title");
185
        $message = trans("auth::roles.messages.{$action}.message", $replace);
186
187
        Log::info($message, $context);
188
        $this->notifySuccess($message, $title);
189
190
        return $message;
191
    }
192
}
193