Completed
Push — master ( 9fa9ff...52caec )
by Şəhriyar
20:38 queued 15:34
created

UsersController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 88.24%

Importance

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