Completed
Push — master ( 52caec...ac6145 )
by Şəhriyar
17:22
created

UsersController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 52.94%

Importance

Changes 14
Bugs 8 Features 5
Metric Value
wmc 20
c 14
b 8
f 5
lcom 1
cbo 8
dl 0
loc 134
ccs 18
cts 34
cp 0.5294
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 8 3
A store() 0 10 3
A show() 0 6 1
A edit() 0 11 3
A update() 0 9 3
A destroy() 0 10 3
A redirectPath() 0 8 3
1
<?php namespace App\Http\Controllers;
2
3
use App\Contracts\Registrar;
4
use App\Exceptions\Common\NotImplementedException;
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Response as IlluminateResponse;
7
8
class UsersController extends Controller
9
{
10
    /**
11
     * Create a new authentication controller instance.
12
     */
13 10
    public function __construct()
14
    {
15 10
        $this->middleware('guest', ['except' => ['show', 'edit', 'update', 'destroy']]);
16 10
    }
17
18
    /**
19
     * Show the form for creating a new resource.
20
     *
21
     * @param \Illuminate\Http\Request $request
22
     *
23
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
24
     */
25 6
    public function create(Request $request)
26
    {
27 6
        if ($request->ajax() || $request->wantsJson()) {
28 1
            throw new NotImplementedException;
29
        }
30
31 5
        return view('auth/register');
32
    }
33
34
    /**
35
     * Store a newly created user.
36
     *
37
     * @param \Illuminate\Http\Request $request *
38
     * @param \App\Contracts\Registrar $registrar
39
     *
40
     * @return \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
41
     */
42 4
    public function store(Request $request, Registrar $registrar)
43
    {
44 4
        $user = $registrar->register();
45
46 2
        if ($request->ajax() || $request->wantsJson()) {
47 1
            return response()->json($user)->setStatusCode(IlluminateResponse::HTTP_CREATED);
0 ignored issues
show
Documentation introduced by
$user is of type object<Cartalyst\Sentinel\Users\UserInterface>, but the function expects a string|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...
48
        }
49
50 1
        return redirect($this->redirectPath())->with('message', 'Created');
51
    }
52
53
    /**
54
     * Display the specified user information.
55
     *
56
     * @param \App\Contracts\Registrar $registrar
57
     * @param int                      $id
58
     *
59
     * @return \Illuminate\Http\JsonResponse
60
     */
61 2
    public function show(Registrar $registrar, $id)
62
    {
63 2
        $user = $registrar->get($id);
64
65 1
        return response()->json($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<Cartalyst\Sentinel\Users\UserInterface>, but the function expects a string|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...
66
    }
67
68
    /**
69
     * Show the form for editing user information.
70
     *
71
     * @param \Illuminate\Http\Request $request
72
     * @param \App\Contracts\Registrar $registrar
73
     * @param int                      $id
74
     *
75
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
76
     */
77
    public function edit(Request $request, Registrar $registrar, $id)
78
    {
79
        /** @var \App\Models\User $user */
80
        $user = $registrar->get($id);
81
82
        if ($request->ajax() || $request->wantsJson()) {
83
            return response()->json(['message' => 'Ready', 'data' => $user]);
84
        }
85
86
        return view('auth/edit', ['user' => $user]);
87
    }
88
89
    /**
90
     * Update the specified user information.
91
     *
92
     * @param \Illuminate\Http\Request $request
93
     * @param \App\Contracts\Registrar $registrar
94
     * @param int                      $id
95
     *
96
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
97
     */
98
    public function update(Request $request, Registrar $registrar, $id)
99
    {
100
        $registrar->update($id);
101
        if ($request->ajax() || $request->wantsJson()) {
102
            return response()->json(['message' => 'Updated']);
103
        }
104
105
        return redirect()->back()->with('message', 'Updated');
106
    }
107
108
    /**
109
     * Remove the specified user record from storage.
110
     *
111
     * @param \Illuminate\Http\Request $request
112
     * @param \App\Contracts\Registrar $registrar
113
     * @param int                      $id
114
     *
115
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
116
     */
117
    public function destroy(Request $request, Registrar $registrar, $id)
118
    {
119
        $registrar->delete($id);
120
121
        if ($request->ajax() || $request->wantsJson()) {
122
            return response()->json()->setStatusCode(IlluminateResponse::HTTP_NO_CONTENT);
123
        }
124
125
        return redirect($this->redirectPath())->with('message', 'Deleted');
126
    }
127
128
    /**
129
     * Get the post register / login redirect path.
130
     *
131
     * @return string
132
     */
133 1
    private function redirectPath()
134
    {
135 1
        if (isset($this->redirectPath)) {
136
            return $this->redirectPath;
0 ignored issues
show
Bug introduced by
The property redirectPath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
137
        }
138
139 1
        return isset($this->redirectTo) ? $this->redirectTo : '/';
0 ignored issues
show
Bug introduced by
The property redirectTo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
140
    }
141
}
142