Passed
Push — master ( 181c26...37f6d6 )
by Adam
05:50
created

UserController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 41.1%

Importance

Changes 0
Metric Value
dl 0
loc 141
ccs 30
cts 73
cp 0.411
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A postChangePassword() 0 10 1
A getFields() 0 47 1
A delete() 0 12 4
A index() 0 20 2
A postAdd() 0 12 1
A showAddForm() 0 14 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\ChangePasswordRequest;
6
use App\Http\Requests\UserAddRequest;
7
use App\Models\User;
8
use App\Services\UserTableService;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Hash;
11
12
class UserController extends Controller
13
{
14
    protected $userTableService;
15
16 4
    public function __construct(UserTableService $userTableService)
17
    {
18 4
        $this->userTableService = $userTableService;
19 4
    }
20
21 2
    public function index()
22
    {
23 2
        $title = trans('general.users');
24
25 2
        $dataset = User::select('id', 'name', 'email', 'created_at')
26 2
            ->paginate($this->getItemsPerPage());
27
28 2
        if ($dataset->isEmpty()) {
29 1
            $this->addFlashMessage(trans('general.no_users_in_database'), 'alert-danger');
30
        }
31
32
        $viewData = [
33 2
            'columns'       => $this->userTableService->getColumns(),
34 2
            'dataset'       => $dataset,
35 2
            'routeName'     => $this->userTableService->getRouteName(),
36 2
            'title'         => $title,
37
            'disableEdit'   => true,
38
        ];
39
40 2
        return view('list', $viewData);
41
    }
42
43
    public function showAddForm()
44
    {
45
        $dataset = new User();
46
        $title = trans('navigation.add_user');
47
48
        $viewData = [
49
            'dataset'     => $dataset,
50
            'fields'      => $this->getFields(),
51
            'title'       => $title,
52
            'submitRoute' => route($this->userTableService->getRouteName().'.postadd'),
53
            'routeName'   => $this->userTableService->getRouteName(),
54
        ];
55
56
        return view('addedit', $viewData);
57
    }
58
59
    public function postAdd(UserAddRequest $request)
60
    {
61
        $object = new User();
62
63
        $request->merge(['password' => Hash::make($request->password)]);
64
        $object->fill($request->all());
65
        $object->save();
66
67
        return redirect()->route($this->userTableService->getRouteName().'.index')
68
            ->with([
69
                'message'     => trans('general.saved'),
70
                'alert-class' => 'alert-success',
71
            ]);
72
    }
73
74 1
    public function delete($objectId)
75
    {
76 1
        $object = User::find($objectId);
77 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
78
79 1
        if (!$object || $object->id === 1 || $object->id = Auth::user()->id) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
80
            $data = ['class' => 'alert-danger', 'message' => trans('general.cannot_delete_object')];
81
        } else {
82 1
            $object->delete();
83
        }
84
85 1
        return response()->json($data);
86
    }
87
88 1
    public function changePassword()
89
    {
90 1
        return view('auth.passwords.change');
91
    }
92
93 1
    public function postChangePassword(ChangePasswordRequest $request)
94
    {
95 1
        $user = Auth::user();
96 1
        $user->password = Hash::make($request->new_password);
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
97 1
        $user->save();
98
99 1
        return redirect()->route($this->userTableService->getRouteName().'.change_password')
100 1
            ->with([
101 1
                'message'     => trans('general.saved'),
102 1
                'alert-class' => 'alert-success',
103
            ]);
104
    }
105
106
    public function getFields()
107
    {
108
        return [
109
            [
110
                'id'    => 'name',
111
                'title' => trans('general.name'),
112
                'value' => function (User $data) {
113
                    return $data->name;
114
                },
115
                'optional' => [
116
                    'required' => 'required',
117
                ],
118
            ],
119
            [
120
                'id'    => 'email',
121
                'title' => trans('auth.email'),
122
                'value' => function (User $data) {
123
                    return $data->email;
124
                },
125
                'type'     => 'email',
126
                'optional' => [
127
                    'required' => 'required',
128
                ],
129
            ],
130
            [
131
                'id'       => 'password',
132
                'title'    => trans('auth.password'),
133
                'type'     => 'password',
134
                'optional' => [
135
                    'required' => 'required',
136
                ],
137
            ],
138
            [
139
                'id'       => 'password_confirmation',
140
                'title'    => trans('auth.password_confirmation'),
141
                'type'     => 'password',
142
                'optional' => [
143
                    'required'    => 'required',
144
                ],
145
            ],
146
            [
147
                'id'    => 'is_admin',
148
                'title' => trans('general.administrator'),
149
                'value' => function () {
150
                    return true;
151
                },
152
                'type'     => 'checkbox',
153
            ],
154
        ];
155
    }
156
}
157