Passed
Push — master ( ccc374...0fc05e )
by Adam
05:56
created

UserController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 19 2
A delete() 0 12 4
A postChangePassword() 0 10 1
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
        ];
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 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...
47
            $data = ['class' => 'alert-danger', 'message' => trans('general.cannot_delete_object')];
48
        } else {
49 1
            $object->delete();
50
        }
51
52 1
        return response()->json($data);
53
    }
54
55 1
    public function changePassword()
56
    {
57 1
        return view('auth.passwords.change');
58
    }
59
60 1
    public function postChangePassword(ChangePasswordRequest $request)
61
    {
62 1
        $user = Auth::user();
63 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...
64 1
        $user->save();
65
66 1
        return redirect()->route($this->userTableService->getRouteName().'.change_password')
67 1
            ->with([
68 1
                'message'     => trans('general.saved'),
69 1
                'alert-class' => 'alert-success',
70
            ]);
71
    }
72
}
73