UserController::index()   A
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 3
b 0
f 0
nc 32
nop 0
dl 0
loc 30
rs 9.2222
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Entities\User;
6
use App\Repositories\Criteria\Like;
7
use App\Repositories\Criteria\Where;
8
use App\Repositories\UserRepository;
9
use Illuminate\Database\Eloquent\Collection;
10
use Illuminate\Database\Eloquent\MassAssignmentException;
11
12
class UserController extends DataController
13
{
14
    public function index()
15
    {
16
        if (request()->filled('id')) {
17
            $this->repository->pushCriteria(new Where('id', request('id')));
18
        }
19
20
        if (request()->filled('name')) {
21
            $this->repository->pushCriteria(new Like('username', request('name')));
22
        }
23
24
        if (request()->filled('email')) {
25
            $this->repository->pushCriteria(new Like('email', request('email')));
26
        }
27
28
        if (request()->filled('status')) {
29
            $this->repository->pushCriteria(new Where('status', request('status')));
30
        }
31
32
        /** @var Collection $models */
33
        $models = parent::index();
34
        $models->load('roles');
35
36
        foreach ($models as $model) {
37
            $model->roles_edit = $model->roles->map(function ($role) {
38
                return $role->id;
39
            });
40
            $model->addHidden('roles');
41
        }
42
43
        return $models;
44
    }
45
46
    public function update($id)
47
    {
48
        /** @var User $model */
49
        $model = $this->repository->findOrFail($id);
50
51
        try {
52
            $model->fill(request()->all());
53
            if (request('password')) {
54
                $model->password = app('hash')->make(request('password'));
0 ignored issues
show
Bug introduced by
It seems like request('password') can also be of type array; however, parameter $value of Illuminate\Hashing\HashManager::make() 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

54
                $model->password = app('hash')->make(/** @scrutinizer ignore-type */ request('password'));
Loading history...
55
            }
56
            $model->save();
57
        } catch (MassAssignmentException $e) {
58
            app('log')->error($e->getMessage());
59
        }
60
61
        $model->roles()->sync(request('roles_edit'));
62
63
        return $model;
64
    }
65
66
    protected function getRepository()
67
    {
68
        return UserRepository::class;
69
    }
70
}
71