UsersController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 127
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A update() 0 13 1
A edit() 0 4 1
A __construct() 0 5 1
A destroy() 0 8 1
A store() 0 16 1
A toggle() 0 10 1
A index() 0 4 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\User;
7
use Session;
8
9
class UsersController extends Controller
10
{
11
    private $user;
12
13
    /**
14
     * Create a new controller instance.
15
     *
16
     * @return void
17
     */
18
    public function __construct(User $user)
19
    {
20
        $this->middleware('auth');
21
        $this->middleware('admin');
22
        $this->user = $user;
23
    }
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index()
31
    {
32
        $users = $this->user->whereNotIn('id', [auth()->user()->id, 1])->orderBy('name', 'asc')->paginate(10);
33
        return view('admin.users.index', compact('users'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.users...dex', compact('users')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
34
    }
35
36
    /**
37
     * Show the form for creating a new resource.
38
     *
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function create()
42
    {
43
        return view('admin.users.create');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.users.create') returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
44
    }
45
46
    /**
47
     * Store a newly created resource in storage.
48
     *
49
     * @param  \Illuminate\Http\Request  $request
50
     * @return \Illuminate\Http\Response
51
     */
52
    public function store(Request $request)
53
    {
54
        $this->validate($request, [
55
            'name' => 'required|string|max:255',
56
            'email' => 'required|string|email|max:255|unique:users',
57
            'password' => 'required|string|min:6|confirmed',
58
        ]);
59
60
        $data = $request->all();
61
        $data['password'] = bcrypt($data['password']);
62
        
63
        $this->user->create($data);
64
65
        Session::flash('success', 'Usuário criado com sucesso');
66
67
        return redirect()->route('users.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('users.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
68
    }
69
70
    /**
71
     * Show the form for editing the specified resource.
72
     *
73
     * @param  int  $id
74
     * @return \Illuminate\Http\Response
75
     */
76
    public function edit($id)
77
    {
78
        $user = $this->user->whereNotIn('id', [auth()->user()->id, 1])->findOrFail($id);
79
        return view('admin.users.edit')->with('user', $user);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.users...')->with('user', $user) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
80
    }
81
82
    /**
83
     * Update the specified resource in storage.
84
     *
85
     * @param  \Illuminate\Http\Request  $request
86
     * @param  int  $id
87
     * @return \Illuminate\Http\Response
88
     */
89
    public function update(Request $request, $id)
90
    {
91
        $this->validate($request, [
92
            'name' => 'required|string|max:255',
93
            'email' => 'required|string|email|max:255|unique:users,email,'.$id
94
        ]);
95
96
        $user = $this->user->whereNotIn('id', [auth()->user()->id, 1])->findOrFail($id);
97
        $user->update($request->all());
98
                
99
        Session::flash('success', 'Usuário atualizado com sucesso');
100
101
        return redirect()->route('users.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('users.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
102
    }
103
104
    /**
105
     * Remove the specified resource from storage.
106
     *
107
     * @param  int  $id
108
     * @return \Illuminate\Http\Response
109
     */
110
    public function destroy($id)
111
    {
112
        $user = $this->user->whereNotIn('id', [auth()->user()->id, 1])->findOrFail($id);        
113
        $user->delete();
114
115
        Session::flash('success', 'Usuário deletado com sucesso');
116
117
        return redirect()->back();
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
118
    }
119
120
    /**
121
     * Update the specified resource in storage.
122
     *
123
     * @param  \Illuminate\Http\Request  $request
124
     * @return \Illuminate\Http\Response
125
     */
126
    public function toggle(Request $request)
127
    {
128
        $this->validate($request, [
129
            "id" => "exists:users,id"
130
        ]);
131
132
        $user = $this->user->whereNotIn('id', [auth()->user()->id, 1])->findOrFail($request->id);
133
        $user->update(['status' => !$user->status]);
134
                
135
        return response()->json(['status' => 'success', 'message' => 'Usuário atualizado com sucesso']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...ualizado com sucesso')) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
136
    }
137
}
138