Completed
Pull Request — develop (#124)
by Tony
03:18
created

UserController::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 13
rs 9.4285
ccs 0
cts 0
cp 0
cc 2
eloc 8
nc 2
nop 2
crap 6
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\DataTables\General\UserDataTable;
6
use App\Http\Requests\CreateUserRequest;
7
use App\Http\Requests\DeleteUserRequest;
8
use App\Http\Requests\UpdateUserRequest;
9
use App\Models\User;
10
use Auth;
11
use Dingo\Api\Http;
12
use Dingo\Api\Routing\Helpers;
13
use Illuminate\Http\Request;
14
15 4
class UserController extends Controller
16 4
{
17 4
    use Helpers;
18
19 4
    /**
20
     * Constructor
21 4
     */
22 4
    public function __construct(Request $request)
23 4
    {
24 4
        $this->middleware('auth');
25 2
    }
26 2
27 2
    /**
28
     * Display a listing of the resource.
29 2
     *
30 2
     * @param UserDataTable $dataTable
31
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...e\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
32
     */
33 4
    public function index(UserDataTable $dataTable)
34 4
    {
35
        if (Auth::user()->isAdmin()) {
36
            return $dataTable->render('users.manage');
37
        }
38
        return redirect('preferences');
39
    }
40
41
    /**
42
     * Show the form for creating a new resource.
43
     *
44
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
45
     */
46
    public function create()
47
    {
48
        return view('users.create');
49
    }
50
51 4
    /**
52
     * Store a newly created resource in storage.
53
     *
54
     * @param CreateUserRequest $request
55
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
56
     */
57
    public function store(CreateUserRequest $request)
58
    {
59
        $user = User::create($request->all());
60
61
        return response()->json(['message' => trans('user.text.created', ['username' => $user->username])]);
62
    }
63
64
    /**
65
     * Display the specified resource.
66
     *
67
     * @param  int $user_id
68
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
69
     */
70
    public function show($user_id)
0 ignored issues
show
Unused Code introduced by
The parameter $user_id 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...
71
    {
72
        // show read only view of user info here
73
    }
74
75
    /**
76
     * Show the form for editing the specified resource.
77
     *
78
     * @param  int $user_id
79
     * @return \Illuminate\Http\Response
80
     */
81
    public function edit($user_id)
82
    {
83
        $user = User::with('devices', 'ports')->findOrFail($user_id);
84
85
        if (Auth::user()->isAdmin()) {
86
            return view('users.edit')->withUser($user);
87
        }
88
    }
89
90
    /**
91
     * Show the user's preference page
92
     *
93
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
94
     */
95
    public function preferences()
96
    {
97
        $user = Auth::user();
98
99
        $device_count = $user->devices()->count();
100
        $port_count = $user->ports()->count();
101
102
        return view('users.preferences', compact('device_count', 'port_count'));
103
    }
104
105
    /**
106
     * Update the specified resource in storage.
107
     *
108
     * @param UpdateUserRequest|Request $request
109
     * @param $user_id
110
     * @param $type Type of update info|password|adddevice|removedevice|addpor|removeport
111
     * @param  int $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function update(UpdateUserRequest $request, $user_id)
115
    {
116
        $user = User::find($user_id);
117
        $user->update($request->all());
118
        if ($request->input('update') == 'password') {
119
            $message = trans('user.text.pwdupdated');
120
        }
121
        else {
122
            $message = trans('user.text.updated', ['username' => $user->username]);
123
        }
124
125
        return redirect()->back()->with(['type' => 'success', 'message' => $message]);
126
    }
127
128
    /**
129
     * Remove the specified resource from storage.
130
     *
131
     * @param  int $user_id
132
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
133
     */
134
    public function destroy(DeleteUserRequest $request, $user_id)
135
    {
136
        $user = User::find($user_id);
137
        $user->delete();
138
139
        return response()->json(['message' => trans('user.text.deleted', ['username' => $user->username])]);
140
    }
141
}
142