Completed
Push — master ( 6e4b3b...01af8d )
by ARCANEDEV
03:09
created

UsersController::restore()   B

Complexity

Conditions 2
Paths 4

Size

Total Lines 25
Code Lines 15

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 25
ccs 0
cts 21
cp 0
rs 8.8571
cc 2
eloc 15
nc 4
nop 1
crap 6
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\Contracts\Auth\Models\Role;
6
use Arcanesoft\Contracts\Auth\Models\User;
7
use Illuminate\Support\Facades\Log;
8
9
/**
10
 * Class     UsersController
11
 *
12
 * @package  Arcanesoft\Auth\Http\Controllers\Foundation
13
 * @author   ARCANEDEV <[email protected]>
14
 *
15
 * @todo: Adding the authorization checks
16
 */
17
class UsersController extends FoundationController
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Properties
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    /** @var  \Arcanesoft\Contracts\Auth\Models\User  */
24
    protected $user;
25
26
    /* ------------------------------------------------------------------------------------------------
27
     |  Constructor
28
     | ------------------------------------------------------------------------------------------------
29
     */
30
    /**
31
     * Instantiate the controller.
32
     */
33
    public function __construct(User $user)
34
    {
35
        parent::__construct();
36
37
        $this->user = $user;
38
39
        $this->setCurrentPage('auth-users');
40
        $this->addBreadcrumbRoute('Users', 'auth::foundation.users.index');
41
    }
42
43
    /* ------------------------------------------------------------------------------------------------
44
     |  Main Functions
45
     | ------------------------------------------------------------------------------------------------
46
     */
47
    public function index($trashed = false)
48
    {
49
        $users = $this->user->with('roles');
50
        $users = $trashed
51
            ? $users->onlyTrashed()->paginate(30)
52
            : $users->paginate(30);
53
54
        $title = 'List of users' . ($trashed ? ' - Trashed' : '');
55
        $this->setTitle($title);
56
        $this->addBreadcrumb($title);
57
58
        return $this->view('foundation.users.list', compact('trashed', 'users'));
59
    }
60
61
    public function trashList()
62
    {
63
        return $this->index(true);
64
    }
65
66
    public function create(Role $role)
67
    {
68
        $roles = $role->all();
69
70
        $title = 'Create a new user';
71
        $this->setTitle($title);
72
        $this->addBreadcrumb($title);
73
74
        return $this->view('foundation.users.create', compact('roles'));
75
    }
76
77
    public function store(CreateUserRequest $request, User $user)
78
    {
79
        $data = $request->only([
80
            'username', 'email', 'first_name', 'last_name', 'password'
81
        ]);
82
        $user->fill($data);
83
        $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...
84
        $user->save();
85
        $user->roles()->sync($request->get('roles'));
86
87
        $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...
88
        Log::info($message, $user->toArray());
89
        $this->notifySuccess($message, 'User created !');
90
91
        return redirect()->route('auth::foundation.users.index');
92
    }
93
94
    public function show(User $user)
95
    {
96
        $user->load(['roles', 'roles.permissions']);
97
98
        $title = 'User details';
99
        $this->setTitle($title);
100
        $this->addBreadcrumb($title);
101
102
        return $this->view('foundation.users.show', compact('user'));
103
    }
104
105
    public function edit(User $user)
106
    {
107
        $user->load(['roles', 'roles.permissions']);
108
109
        $title = 'Edit a user';
110
        $this->setTitle($title);
111
        $this->addBreadcrumb($title);
112
113
        return $this->view('foundation.users.edit', compact('user'));
114
    }
115
116
    public function update(User $user)
117
    {
118
        //
119
    }
120
121
    public function restore(User $user)
122
    {
123
        self::onlyAjax();
124
125
        try {
126
            $user->restore();
127
128
            $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...
129
            Log::info($message, $user->toArray());
130
            $this->notifySuccess($message, 'User restored !');
131
132
            $ajax = [
133
                'status'  => 'success',
134
                'message' => $message,
135
            ];
136
        }
137
        catch (\Exception $e) {
138
            $ajax = [
139
                'status'  => 'error',
140
                'message' => $e->getMessage(),
141
            ];
142
        }
143
144
        return response()->json($ajax);
145
    }
146
147
    public function delete(User $user)
148
    {
149
        self::onlyAjax();
150
151
        try {
152
            if ($user->trashed()) {
153
                $user->forceDelete();
154
                $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...
155
                Log::info($message, $user->toArray());
156
            }
157
            else {
158
                $user->delete();
159
                $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...
160
            }
161
162
            $this->notifySuccess($message, 'User deleted !');
163
164
            $ajax = [
165
                'status'  => 'success',
166
                'message' => $message,
167
            ];
168
        }
169
        catch(\Exception $e) {
170
            $ajax = [
171
                'status'  => 'error',
172
                'message' => $e->getMessage(),
173
            ];
174
        }
175
176
        return response()->json($ajax);
177
    }
178
}
179