Completed
Pull Request — master (#82)
by Brandon
02:20
created

UserController::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Http\Request;
7
use App\Http\Controllers\Controller;
8
use App\DataTables\UsersDataTable;
9
use App\User;
10
11
class UserController extends Controller
12
{
13
    /**
14
     * Create a new controller instance.
15
     *
16
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
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
        return $dataTable->render('user.index');
32
    }
33
34
    /**
35
     * Show create user page.
36
     *
37
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
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|string|max:255',
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
It seems like $request->input('password') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

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)
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.

This check looks from 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)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
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' => 'required|string|max:255',
115
            'email' => 'required|string|email|max:255|unique:users,email,'.$id,
116
            'password' => 'sometimes|nullable|min:8|confirmed',
117
            'role' => 'required|integer|max:3',
118
            'phone' => 'numeric|phone|nullable',
119
        ]);
120
        
121
        $query = User::findOrFail($id);
122
123
        $query->name = $request->input('name');
124
        $query->email = $request->input('email');
125
        if ($request->input('password') != '') {
126
            $query->password = bcrypt($request->input('password'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
127
        }
128
        $query->role = $request->input('role');
129
        $query->phone = $request->input('phone');
130
131
        $query->save();
132
133
        return redirect()->route('user.show', $id)
1 ignored issue
show
Documentation introduced by
$id is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
            ->with('success', 'User updated successfully');
135
    }
136
137
    /**
138
     * Deletes a user.
139
     *
140
     * @param  Request  $request
141
     * @param  string  $id
142
     * @return \Illuminate\Http\RedirectResponse
143
     */
144
    public function destroy(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
145
    {
146
        $user = User::findOrFail($id);
147
148
        if ($user->trashed()) {
149
            // if the user was already deleted then permananetly delete it
150
            User::destroy($id);
151
        } else {
152
            // soft delete the user the first time
153
            $user->delete();
154
        }
155
156
        return redirect('user.index');
157
    }
158
}
159