Completed
Push — master ( 4b411f...eaddd7 )
by ARCANEDEV
03:07
created

UsersController::activate()   B

Complexity

Conditions 3
Paths 9

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
ccs 0
cts 29
cp 0
rs 8.8571
cc 3
eloc 21
nc 9
nop 1
crap 12
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Foundation;
2
3
use Arcanesoft\Auth\Bases\FoundationController;
4
use Arcanesoft\Auth\Http\Requests\Backend\Users\CreateUserRequest;
5
use Arcanesoft\Auth\Http\Requests\Backend\Users\UpdateUserRequest;
6
use Arcanesoft\Contracts\Auth\Models\Role;
7
use Arcanesoft\Contracts\Auth\Models\User;
8
use Illuminate\Support\Facades\Log;
9
10
/**
11
 * Class     UsersController
12
 *
13
 * @package  Arcanesoft\Auth\Http\Controllers\Foundation
14
 * @author   ARCANEDEV <[email protected]>
15
 *
16
 * @todo: Adding the authorization checks
17
 */
18
class UsersController extends FoundationController
19
{
20
    /* ------------------------------------------------------------------------------------------------
21
     |  Properties
22
     | ------------------------------------------------------------------------------------------------
23
     */
24
    /** @var  \Arcanesoft\Contracts\Auth\Models\User  */
25
    protected $user;
26
27
    /* ------------------------------------------------------------------------------------------------
28
     |  Constructor
29
     | ------------------------------------------------------------------------------------------------
30
     */
31
    /**
32
     * Instantiate the controller.
33
     */
34
    public function __construct(User $user)
35
    {
36
        parent::__construct();
37
38
        $this->user = $user;
39
40
        $this->setCurrentPage('auth-users');
41
        $this->addBreadcrumbRoute('Users', 'auth::foundation.users.index');
42
    }
43
44
    /* ------------------------------------------------------------------------------------------------
45
     |  Main Functions
46
     | ------------------------------------------------------------------------------------------------
47
     */
48
    public function index($trashed = false)
49
    {
50
        $users = $this->user->with('roles');
51
        $users = $trashed
52
            ? $users->onlyTrashed()->paginate(30)
53
            : $users->paginate(30);
54
55
        $title = 'List of users' . ($trashed ? ' - Trashed' : '');
56
        $this->setTitle($title);
57
        $this->addBreadcrumb($title);
58
59
        return $this->view('foundation.users.list', compact('trashed', 'users'));
60
    }
61
62
    public function trashList()
63
    {
64
        return $this->index(true);
65
    }
66
67
    public function create(Role $role)
68
    {
69
        $roles = $role->all();
70
71
        $title = 'Create a new user';
72
        $this->setTitle($title);
73
        $this->addBreadcrumb($title);
74
75
        return $this->view('foundation.users.create', compact('roles'));
76
    }
77
78
    public function store(CreateUserRequest $request, User $user)
79
    {
80
        $data = $request->only([
81
            'username', 'email', 'first_name', 'last_name', 'password'
82
        ]);
83
        $user->fill($data);
84
        $user->is_active = true;
0 ignored issues
show
Bug introduced by
Accessing is_active on the interface Arcanesoft\Contracts\Auth\Models\User 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...
85
        $user->save();
86
        $user->roles()->sync($request->get('roles'));
87
88
        $message = "The user {$user->username} was created successfully !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User 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
        Log::info($message, $user->toArray());
90
        $this->notifySuccess($message, 'User created !');
91
92
        return redirect()->route('auth::foundation.users.index');
93
    }
94
95
    public function show(User $user)
96
    {
97
        $user->load(['roles', 'roles.permissions']);
98
99
        $title = 'User details';
100
        $this->setTitle($title);
101
        $this->addBreadcrumb($title);
102
103
        return $this->view('foundation.users.show', compact('user'));
104
    }
105
106
    public function edit(User $user, Role $role)
107
    {
108
        $user->load(['roles', 'roles.permissions']);
109
        $roles = $role->all();
110
111
        $title = 'Edit a user';
112
        $this->setTitle($title);
113
        $this->addBreadcrumb($title);
114
115
        return $this->view('foundation.users.edit', compact('user', 'roles'));
116
    }
117
118
    public function update(UpdateUserRequest $request, User $user)
119
    {
120
        $inputs = ['username', 'email', 'first_name', 'last_name'];
121
122
        if ($request->has('password')) {
123
            $inputs[] = 'password';
124
        }
125
126
        $user->update($request->only($inputs));
127
        $user->roles()->sync($request->get('roles'));
128
129
        $message = "The user {$user->username} was updated successfully !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User 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...
130
        Log::info($message, $user->toArray());
131
        $this->notifySuccess($message, 'User Updated !');
132
133
        return redirect()->route('auth::foundation.users.show', [
134
            $user->hashed_id
0 ignored issues
show
Bug introduced by
Accessing hashed_id on the interface Arcanesoft\Contracts\Auth\Models\User 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...
135
        ]);
136
    }
137
138
    public function activate(User $user)
139
    {
140
        self::onlyAjax();
141
142
        try {
143
            if ($user->isActive()) {
144
                $title   = 'User disabled !';
145
                $message = "The user {$user->username} has been successfully disabled !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User 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...
146
                $user->deactivate();
147
            }
148
            else {
149
                $title   = 'User activated !';
150
                $message = "The user {$user->username} has been successfully activated !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User 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...
151
                $user->activate();
152
            }
153
154
            Log::info($message, $user->toArray());
155
            $this->notifySuccess($message, $title);
156
157
            $ajax = [
158
                'status'  => 'success',
159
                'message' => $message,
160
            ];
161
        }
162
        catch (\Exception $e) {
163
            $ajax = [
164
                'status'  => 'error',
165
                'message' => $e->getMessage(),
166
            ];
167
        }
168
169
        return response()->json($ajax);
170
    }
171
172
    public function restore(User $user)
173
    {
174
        self::onlyAjax();
175
176
        try {
177
            $user->restore();
178
179
            $message = "The user {$user->username} has been successfully restored !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User 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...
180
            Log::info($message, $user->toArray());
181
            $this->notifySuccess($message, 'User restored !');
182
183
            $ajax = [
184
                'status'  => 'success',
185
                'message' => $message,
186
            ];
187
        }
188
        catch (\Exception $e) {
189
            $ajax = [
190
                'status'  => 'error',
191
                'message' => $e->getMessage(),
192
            ];
193
        }
194
195
        return response()->json($ajax);
196
    }
197
198
    public function delete(User $user)
199
    {
200
        self::onlyAjax();
201
202
        try {
203
            if ($user->trashed()) {
204
                $user->forceDelete();
205
                $message = "The user {$user->username} has been successfully deleted !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User 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...
206
                Log::info($message, $user->toArray());
207
            }
208
            else {
209
                $user->delete();
210
                $message = "The user {$user->username} was placed in trashed users !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User 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...
211
            }
212
213
            $this->notifySuccess($message, 'User deleted !');
214
215
            $ajax = [
216
                'status'  => 'success',
217
                'message' => $message,
218
            ];
219
        }
220
        catch(\Exception $e) {
221
            $ajax = [
222
                'status'  => 'error',
223
                'message' => $e->getMessage(),
224
            ];
225
        }
226
227
        return response()->json($ajax);
228
    }
229
}
230