Passed
Push — master ( e287ea...a2c899 )
by Adam
05:22
created

UserController::delete()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 2
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\User;
6
use App\Services\UserTableService;
7
use App\Http\Requests\ChangePasswordRequest;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Hash;
10
11
class UserController extends Controller
12
{
13
    protected $userTableService;
14
15 3
    public function __construct(UserTableService $userTableService)
16
    {
17 3
        $this->userTableService = $userTableService;
18 3
    }
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
        ];
37
38 2
        return view('list', $viewData);
39
    }
40
41 1
    public function delete($objectId)
42
    {
43 1
        $object = User::find($objectId);
44 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
45
46
        // TODO: isAdmin()
47 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...
48
            $data = ['class' => 'alert-danger', 'message' => trans('general.cannot_delete_object')];
49
        } else {
50 1
            $object->delete();
51
        }
52
53 1
        return response()->json($data);
54
    }
55
56
    public function changePassword()
57
    {
58
        return view('auth.passwords.change');
59
    }
60
61
    public function postChangePassword(ChangePasswordRequest $request)
62
    {
63
        $user = Auth::user();
64
        $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...
65
        $user->save();
66
67
        return redirect()->route($this->userTableService->getRouteName().'.change_password')
68
            ->with([
69
                'message'     => trans('general.saved'),
70
                'alert-class' => 'alert-success',
71
            ]);
72
    }
73
}
74