UserController::update()   B
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 33
Code Lines 24

Duplication

Lines 5
Ratio 15.15 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 24
c 1
b 0
f 0
nc 10
nop 2
dl 5
loc 33
ccs 0
cts 23
cp 0
crap 30
rs 8.439
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
use App\DataTables\UsersDataTable;
8
use App\User;
9
use Illuminate\Support\Facades\Auth;
10
11
class UserController extends Controller
12
{
13
    /**
14
     * Create a new controller instance.
15
     *
16
     * @return void
17
     */
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
    }
22
23
    /**
24
     * Display index page and process dataTable ajax request.
25
     *
26
     * @param \App\DataTables\UsersDataTable $dataTable
27
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
28
     */
29
    public function index(UsersDataTable $dataTable)
30
    {
31
        $this->authorize('index', User::class);
32
        
33
        $trashed = User::onlyTrashed()->get();
34
        return $dataTable->render('user.index', compact('trashed'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $dataTable->rende...x', compact('trashed')) also could return the type callable which is incompatible with the documented return type Illuminate\Http\JsonResponse|Illuminate\View\View.
Loading history...
35
    }
36
37
    /**
38
     * Show create user page.
39
     *
40
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
0 ignored issues
show
Bug introduced by
The type BladeView was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
     */
42
    public function create()
43
    {
44
        $this->authorize('create', User::class);
45
        
46
        return view('user.create');
47
    }
48
49
    /**
50
     * Store a new user.
51
     *
52
     * @param  Request  $request
53
     * @return \Illuminate\Http\RedirectResponse
54
     */
55
    public function store(Request $request)
56
    {
57
        $this->authorize('store', User::class);
58
        
59
        request()->validate([
60
            'name' => 'required|min:2|max:190|full_name',
61
            'email' => 'required|string|email|max:255|unique:users',
62
            'password' => 'required|string|min:8|confirmed',
63
            'role' => 'required|integer|max:3',
64
            'phone' => 'numeric|phone|nullable',
65
        ]);
66
    
67
        $user = User::create([ 'name' => $request->name,
68
            'email' => $request->input('email'),
69
            'password' => bcrypt($request->input('password')),
0 ignored issues
show
Bug introduced by
$request->input('password') of type array is incompatible with the type string expected by parameter $value of bcrypt(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            'password' => bcrypt(/** @scrutinizer ignore-type */ $request->input('password')),
Loading history...
70
            'phone' => $request->input('phone'),
71
            'role' => $request->input('role'),]);
72
73
        return redirect()->route('user.show', $user->id)
0 ignored issues
show
Bug introduced by
$user->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        return redirect()->route('user.show', /** @scrutinizer ignore-type */ $user->id)
Loading history...
74
            ->with('success', 'User created successfully');
75
    }
76
77
    /**
78
     * Show the given user.
79
     *
80
     * @param  User  $user
81
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
82
     */
83
    public function show(User $user)
84
    {
85
        $this->authorize('show', $user);
86
        
87
        $user->password = "";
88
        
89
        return view('user.show', [ 'user' => $user ]);
90
    }
91
92
    /**
93
     * Edit the given user.
94
     *
95
     * @param  User  $user
96
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
97
     */
98
    public function edit(User $user)
99
    {
100
        $this->authorize('edit', $user);
101
        
102
        $user->password = "";
103
104
        return view('user.edit', [ 'user' => $user ]);
105
    }
106
107
    /**
108
     * Update the given user.
109
     *
110
     * @param  Request  $request
111
     * @param  User  $user
112
     * @return \Illuminate\Http\RedirectResponse
113
     */
114
    public function update(Request $request, User $user)
115
    {
116
        $this->authorize('update', $user);
117
        
118
        request()->validate([
119
            'name' => 'sometimes|nullable|min:2|max:190|full_name',
120
            'email' => 'sometimes|nullable|string|email|max:255|unique:users,email,'.$user->id,
121
            'password' => 'sometimes|nullable|min:8|confirmed',
122
            'role' => 'sometimes|nullable|integer|max:3',
123
            'phone' => 'numeric|phone|nullable',
124
            'preferred_device_id' => 'nullable|integer|digits_between:1,10|exists:devices,id',
125
        ]);
126
127
        if ($request->input('name') != null)
128
        {
129
            $user->name = $request->input('name');
130
            $user->email = $request->input('email');
131
            $user->phone = $request->input('phone');
132
            if ($request->input('password') != '')
133
                $user->password = bcrypt($request->input('password'));
0 ignored issues
show
Bug introduced by
$request->input('password') of type array is incompatible with the type string expected by parameter $value of bcrypt(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
                $user->password = bcrypt(/** @scrutinizer ignore-type */ $request->input('password'));
Loading history...
134
            if (Auth::user()->can('updateRole', $user))
135
                $user->role = $request->input('role');
136
        }
137
        else
138
            $user->preferred_device_id = $request->input('preferred_device_id');
139
140
        $user->save();
141
    
142 View Code Duplication
        if (\Request::ajax())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
            return response()->json([ 'success' => 'Preferred device updated successfully' ]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...updated successfully')) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\RedirectResponse.
Loading history...
144
        else
145
            return redirect()->route('user.show', $user->id)
0 ignored issues
show
Bug introduced by
$user->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
            return redirect()->route('user.show', /** @scrutinizer ignore-type */ $user->id)
Loading history...
146
                ->with('success', 'User updated successfully');
147
    }
148
149
    /**
150
     * Deletes a user.
151
     *
152
     * @param  string  $id
153
     * @return \Illuminate\Http\RedirectResponse
154
     */
155
    public function destroy($id)
156
    {
157
        $user = User::withTrashed()->findOrFail($id);
158
        $this->authorize('destroy', $user);
159
160
        if ($user->trashed()) {
161
            //If the user was already deleted then permanently delete it
162
            $user->forceDelete($user->id);
0 ignored issues
show
Unused Code introduced by
The call to App\Device::forceDelete() has too many arguments starting with $user->id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
            $user->/** @scrutinizer ignore-call */ 
163
                   forceDelete($user->id);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
163
        } else {
164
            //Soft delete the user the first time
165
            $user->delete();
166
        }
167
168
        return redirect()->route('user.index')
169
            ->with('success', 'User deleted successfully');
170
    }
171
    
172
    /**
173
     * Restores a user.
174
     *
175
     * @param  string  $id
176
     * @return \Illuminate\Http\RedirectResponse
177
     */
178 View Code Duplication
    public function restore($id)
179
    {
180
        $this->authorize('restore', User::class);
181
        
182
        $user = User::onlyTrashed()->findOrFail($id);
183
184
        $user->restore();
185
        
186
        return redirect()->route('user.show', $user->id)
0 ignored issues
show
Bug introduced by
$user->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
        return redirect()->route('user.show', /** @scrutinizer ignore-type */ $user->id)
Loading history...
187
            ->with('success', 'User restored successfully');
188
    }
189
}
190