Passed
Branch master (cdbb92)
by Adam
08:04
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 Illuminate\Support\Facades\Auth;
8
9
class UserController extends Controller
10
{
11
    protected $userTableService;
12
13 3
    public function __construct(UserTableService $userTableService)
14
    {
15 3
        $this->userTableService = $userTableService;
16 3
    }
17
18 2
    public function index()
19
    {
20 2
        $title = trans('general.users');
21
22 2
        $dataset = User::select('id', 'name', 'email', 'created_at')
23 2
            ->paginate($this->getItemsPerPage());
24
25 2
        if ($dataset->isEmpty()) {
26 1
            $this->addFlashMessage(trans('general.no_users_in_database'), 'alert-danger');
27
        }
28
29
        $viewData = [
30 2
            'columns'       => $this->userTableService->getColumns(),
31 2
            'dataset'       => $dataset,
32 2
            'routeName'     => $this->userTableService->getRouteName(),
33 2
            'title'         => $title,
34
        ];
35
36 2
        return view('list', $viewData);
37
    }
38
39 1
    public function delete($objectId)
40
    {
41 1
        $object = User::find($objectId);
42 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
43
44
        // TODO: isAdmin()
45 1
        if (!$object || $object->id === 1 || $object->id = Auth::user()->id /*|| $object->isAdmin()*/) {
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...
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
            $data = ['class' => 'alert-danger', 'message' => trans('general.cannot_delete_admins')];
47
        } else {
48 1
            User::destroy($objectId);
49
        }
50
51 1
        return response()->json($data);
52
    }
53
}
54