Passed
Push — master ( 26b15f...82e84f )
by Adam
05:41
created

UserController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 0
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\ChangePasswordRequest;
6
use App\Models\User;
7
use App\Services\UserTableService;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Hash;
10
11
class UserController extends Controller
12
{
13
    protected $userTableService;
14
15 4
    public function __construct(UserTableService $userTableService)
16
    {
17 4
        $this->userTableService = $userTableService;
18 4
    }
19
20 2
    public function index()
21
    {
22 2
        $title = trans('general.users');
23
24 2
        $dataset = User::select('id', 'name', 'email', 'created_at')
25 2
            ->paginate($this->getItemsPerPage());
26
27 2
        if ($dataset->isEmpty()) {
28 1
            $this->addFlashMessage(trans('general.no_users_in_database'), 'alert-danger');
29
        }
30
31
        $viewData = [
32 2
            'columns'       => $this->userTableService->getColumns(),
33 2
            'dataset'       => $dataset,
34 2
            'routeName'     => $this->userTableService->getRouteName(),
35 2
            'title'         => $title,
36
            'disableEdit'   => true,
37
        ];
38
39 2
        return view('list', $viewData);
40
    }
41
42
    public function showAddForm()
43
    {
44
        $dataset = new User();
45
        $title = trans('navigation.add_user');
46
47
        $viewData = [
48
            'dataset'     => $dataset,
49
            'fields'      => $this->getFields(),
50
            'title'       => $title,
51
            'submitRoute' => route($this->userTableService->getRouteName().'.postadd'),
52
            'routeName'   => $this->userTableService->getRouteName(),
53
        ];
54
55
        return view('addedit', $viewData);
56
    }
57
58 1
    public function delete($objectId)
59
    {
60 1
        $object = User::find($objectId);
61 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
62
63 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...
64
            $data = ['class' => 'alert-danger', 'message' => trans('general.cannot_delete_object')];
65
        } else {
66 1
            $object->delete();
67
        }
68
69 1
        return response()->json($data);
70
    }
71
72 1
    public function changePassword()
73
    {
74 1
        return view('auth.passwords.change');
75
    }
76
77 1
    public function postChangePassword(ChangePasswordRequest $request)
78
    {
79 1
        $user = Auth::user();
80 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...
81 1
        $user->save();
82
83 1
        return redirect()->route($this->userTableService->getRouteName().'.change_password')
84 1
            ->with([
85 1
                'message'     => trans('general.saved'),
86 1
                'alert-class' => 'alert-success',
87
            ]);
88
    }
89
90
    public function getFields()
91
    {
92
        return [
93
            [
94
                'id'    => 'name',
95
                'title' => trans('general.name'),
96
                'value' => function (User $data) {
97
                    return $data->name;
98
                },
99
                'optional' => [
100
                    'required' => 'required',
101
                ],
102
            ],
103
            [
104
                'id'    => 'email',
105
                'title' => trans('auth.email'),
106
                'value' => function (User $data) {
107
                    return $data->email;
108
                },
109
                'type'     => 'email',
110
                'optional' => [
111
                    'required' => 'required',
112
                ],
113
            ],
114
            [
115
                'id'    => 'password',
116
                'title' => trans('auth.password'),
117
                'type'     => 'password',
118
                'optional' => [
119
                    'required' => 'required',
120
                ],
121
            ],
122
            [
123
                'id'    => 'password-confirm',
124
                'title' => trans('auth.password_confirmation'),
125
                'type'     => 'password',
126
                'optional' => [
127
                    'required'    => 'required',
128
                ],
129
            ],
130
            [
131
                'id'    => 'is-admin',
132
                'title' => trans('general.administrator'),
133
                'value' => function () {
134
                    return true;
135
                },
136
                'type'     => 'checkbox',
137
            ],
138
        ];
139
    }
140
}
141