Passed
Push — develop ( 0fc46b...6b7c8d )
by Tony
04:19
created

UserController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 9.4285
c 0
b 0
f 0
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\Routing\Helpers;
12
use Illuminate\Http\Request;
13
14
class UserController extends Controller
15
{
16
    use Helpers;
17
18
    /**
19
     * Display a listing of the resource.
20
     *
21
     * @param UserDataTable $dataTable
22
     * @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...
23
     */
24 1
    public function index(UserDataTable $dataTable)
25
    {
26 1
        if (Auth::user()->isAdmin()) {
27 1
            return $dataTable->render('users.manage');
28
        }
29
        return redirect('preferences');
30
    }
31
32
    /**
33
     * Show the form for creating a new resource.
34
     *
35
     * @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...
36
     */
37
    public function create()
38
    {
39
        return view('users.create');
40
    }
41
42
    /**
43
     * Store a newly created resource in storage.
44
     *
45
     * @param CreateUserRequest $request
46
     * @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...
47
     */
48
    public function store(CreateUserRequest $request)
49
    {
50
        $user = User::create($request->all());
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Models\User. Did you maybe mean whereCreatedAt()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
51
52
        return response()->json(['message' => trans('user.text.created', ['username' => $user->username])]);
53
    }
54
55
    /**
56
     * Display the specified resource.
57
     *
58
     * @param  int $user_id
59
     * @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...
60
     */
61
    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...
62
    {
63
        // show read only view of user info here
64
    }
65
66
    /**
67
     * Show the form for editing the specified resource.
68
     *
69
     * @param  int $user_id
70
     * @return \Illuminate\Http\Response
71
     */
72 1
    public function edit($user_id)
73
    {
74 1
        $user = User::with('devices', 'ports')->findOrFail($user_id);
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
75
76 1
        if (Auth::user()->isAdmin()) {
77 1
            return view('users.edit')->withUser($user);
78
        }
79
80
        return redirect('preferences');
81
    }
82
83
    /**
84
     * Show the user's preference page
85
     *
86
     * @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...
87
     */
88 4
    public function preferences()
89
    {
90 4
        $user = Auth::user();
91
92 4
        $device_count = $user->devices()->count();
93 4
        $port_count = $user->ports()->count();
94
95 4
        return view('users.preferences', compact('device_count', 'port_count'));
96
    }
97
98
    /**
99
     * Update the specified resource in storage.
100
     *
101
     * @param UpdateUserRequest|Request $request
102
     * @param $user_id
103
     * @return \Illuminate\Http\Response
104
     */
105
    public function update(UpdateUserRequest $request, $user_id)
106
    {
107
        $user = User::find($user_id);
108
        $user->update($request->all());
109
        if ($request->input('update') == 'password') {
110
            $message = trans('user.text.pwdupdated');
111
        } else {
112
            $message = trans('user.text.updated', ['username' => $user->username]);
113
        }
114
115
        return redirect()->back()->with(['type' => 'success', 'message' => $message]);
116
    }
117
118
    /**
119
     * Remove the specified resource from storage.
120
     *
121
     * @param  int $user_id
122
     * @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...
123
     */
124
    public function destroy(DeleteUserRequest $request, $user_id)
125
    {
126
        $user = User::find($user_id);
127
        $user->delete();
128
129
        return response()->json(['message' => trans('user.text.deleted', ['username' => $user->username])]);
130
    }
131
}
132