Completed
Push — master ( 6e8adf...c24137 )
by
unknown
27s
created

UserController::update()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 23

Duplication

Lines 5
Ratio 15.63 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 23
c 2
b 0
f 0
nc 6
nop 2
dl 5
loc 32
ccs 0
cts 22
cp 0
crap 20
rs 8.5806
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
10
class UserController extends Controller
11
{
12
    /**
13
     * Create a new controller instance.
14
     *
15
     * @return void
16
     */
17
    public function __construct()
18
    {
19
        $this->middleware('auth');
20
    }
21
22
    /**
23
     * Display index page and process dataTable ajax request.
24
     *
25
     * @param \App\DataTables\UsersDataTable $dataTable
26
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
27
     */
28
    public function index(UsersDataTable $dataTable)
29
    {
30
        $trashed = User::onlyTrashed()->get();
31
        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...
32
    }
33
34
    /**
35
     * Show create user page.
36
     *
37
     * @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...
38
     */
39
    public function create()
40
    {
41
        return view('user.create');
42
    }
43
44
    /**
45
     * Store a new user.
46
     *
47
     * @param  Request  $request
48
     * @return \Illuminate\Http\RedirectResponse
49
     */
50
    public function store(Request $request)
51
    {
52
        request()->validate([
53
            'name' => 'required|min:2|max:190|full_name',
54
            'email' => 'required|string|email|max:255|unique:users',
55
            'password' => 'required|string|min:8|confirmed',
56
            'role' => 'required|integer|max:3',
57
            'phone' => 'numeric|phone|nullable',
58
        ]);
59
60
        $user = new User;
61
62
        $user->name = $request->input('name');
63
        $user->email = $request->input('email');
64
        $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

64
        $user->password = bcrypt(/** @scrutinizer ignore-type */ $request->input('password'));
Loading history...
65
        $user->role = $request->input('role');
66
        $user->phone = $request->input('phone');
67
68
        $user->save();
69
70
        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

70
        return redirect()->route('user.show', /** @scrutinizer ignore-type */ $user->id)
Loading history...
71
            ->with('success', 'User created successfully');
72
    }
73
74
    /**
75
     * Show the given user.
76
     *
77
     * @param  Request  $request
78
     * @param  string  $id
79
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
80
     */
81 View Code Duplication
    public function show(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

81
    public function show(/** @scrutinizer ignore-unused */ Request $request, $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        $user = User::findOrFail($id);
84
        $user->password = "";
85
        
86
        return view('user.show', [ 'user' => $user ]);
87
    }
88
89
    /**
90
     * Edit the given user.
91
     *
92
     * @param  Request  $request
93
     * @param  string  $id
94
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
95
     */
96 View Code Duplication
    public function edit(Request $request, $id)
97
    {
98
        $user = User::findOrFail($id);
99
        $user->password = "";
100
101
        return view('user.edit', [ 'user' => $user ]);
102
    }
103
104
    /**
105
     * Update the given user.
106
     *
107
     * @param  Request  $request
108
     * @param  string  $id
109
     * @return \Illuminate\Http\RedirectResponse
110
     */
111
    public function update(Request $request, $id)
112
    {
113
        request()->validate([
114
            'name' => 'sometimes|nullable|min:2|max:190|full_name',
115
            'email' => 'sometimes|nullable|string|email|max:255|unique:users,email,'.$id,
116
            'password' => 'sometimes|nullable|min:8|confirmed',
117
            'role' => 'sometimes|nullable|integer|max:3',
118
            'phone' => 'numeric|phone|nullable',
119
            'preferred_device_id' => 'nullable|integer|digits_between:1,10|exists:devices,id',
120
        ]);
121
        
122
        $query = User::findOrFail($id);
123
124
        if ($request->input('name') != null)
125
        {
126
            $query->name = $request->input('name');
127
            $query->email = $request->input('email');
128
            $query->role = $request->input('role');
129
            $query->phone = $request->input('phone');
130
            if ($request->input('password') != '')
131
                $query->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

131
                $query->password = bcrypt(/** @scrutinizer ignore-type */ $request->input('password'));
Loading history...
132
        }
133
        else
134
            $query->preferred_device_id = $request->input('preferred_device_id');
135
136
        $query->save();
137
    
138 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...
139
            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...
140
        else
141
            return redirect()->route('user.show', $id)
0 ignored issues
show
Bug introduced by
$id of type string 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

141
            return redirect()->route('user.show', /** @scrutinizer ignore-type */ $id)
Loading history...
142
                ->with('success', 'User updated successfully');
143
    }
144
145
    /**
146
     * Deletes a user.
147
     *
148
     * @param  string  $id
149
     * @return \Illuminate\Http\RedirectResponse
150
     */
151 View Code Duplication
    public function destroy($id)
152
    {
153
        $user = User::withTrashed()->findOrFail($id);
154
155
        if ($user->trashed()) {
156
            //If the user was already deleted then permanently delete it
157
            $user->forceDelete($user->id);
0 ignored issues
show
Unused Code introduced by
The call to App\User::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

157
            $user->/** @scrutinizer ignore-call */ 
158
                   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...
158
        } else {
159
            //Soft delete the user the first time
160
            $user->delete();
161
        }
162
163
        return redirect()->route('user.index')
164
            ->with('success', 'User deleted successfully');
165
    }
166
    
167
    /**
168
     * Restores a user.
169
     *
170
     * @param  string  $id
171
     * @return \Illuminate\Http\RedirectResponse
172
     */
173 View Code Duplication
    public function restore($id)
174
    {
175
        $user = User::onlyTrashed()->findOrFail($id);
176
177
        $user->restore();
178
        
179
        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

179
        return redirect()->route('user.show', /** @scrutinizer ignore-type */ $user->id)
Loading history...
180
            ->with('success', 'User restored successfully');
181
    }
182
}
183