Passed
Push — master ( f6e9eb...632a43 )
by Adam
05:07
created

UserController::delete()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 9.4285
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
8
class UserController extends Controller
9
{
10
    protected $userTableService;
11
12 3
    public function __construct(UserTableService $userTableService)
13
    {
14 3
        $this->userTableService = $userTableService;
15 3
    }
16
17 2
    public function index()
18
    {
19 2
        $title = trans('general.users');
20
21 2
        $dataset = User::select('id', 'name', 'email', 'created_at')
22 2
            ->paginate($this->getItemsPerPage());
23
24 2
        if ($dataset->isEmpty()) {
25 1
            $this->addFlashMessage(trans('general.no_users_in_database'), 'alert-danger');
26
        }
27
28
        $viewData = [
29 2
            'columns'       => $this->userTableService->getColumns(),
30 2
            'dataset'       => $dataset,
31 2
            'routeName'     => $this->userTableService->getRouteName(),
32 2
            'title'         => $title,
33 2
            'deleteMessage' => mb_strtolower(trans('general.user')).' '.mb_strtolower(trans('general.number')),
0 ignored issues
show
Bug introduced by
It seems like trans('general.user') can also be of type array; however, parameter $str of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
            'deleteMessage' => mb_strtolower(/** @scrutinizer ignore-type */ trans('general.user')).' '.mb_strtolower(trans('general.number')),
Loading history...
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->isAdmin()*/) {
0 ignored issues
show
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