Completed
Push — master ( 01af8d...4b411f )
by ARCANEDEV
02:55
created

UpdateUserRequest::getRoleIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Auth\Http\Requests\Backend\Users;
2
3
use Arcanesoft\Auth\Bases\FormRequest;
4
use Illuminate\Support\Facades\Cache;
5
6
/**
7
 * Class     UpdateUserRequest
8
 *
9
 * @package  Arcanesoft\Auth\Http\Requests\Backend\Users
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class UpdateUserRequest extends FormRequest
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * Role validation rules.
20
     *
21
     * @var array
22
     */
23
    protected $rules = [
24
        'username'              => 'required|min:3',
25
        'email'                 => 'required|email',
26
        'first_name'            => 'required|min:2',
27
        'last_name'             => 'required|min:2',
28
        'password'              => 'required_with:password_confirmation|min:8|confirmed',
29
        'password_confirmation' => 'required_with:password|min:8',
30
        'roles'                 => 'required|array|min:1',
31
    ];
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Main Functions
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * Determine if the user is authorized to make this request.
39
     *
40
     * @return bool
41
     */
42
    public function authorize()
43
    {
44
        return true;
45
    }
46
47
    /**
48
     * Get the validation rules that apply to the request.
49
     *
50
     * @return array
51
     */
52
    public function rules()
53
    {
54
        /**  @var \Arcanesoft\Contracts\Auth\Models\User  $user */
55
        $user  = $this->route('user_id');
56
        $rules = $this->rules;
57
58
        $rules['username'] .= '|unique:users,username,' . $user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
59
        $rules['email']    .= '|unique:users,email,' . $user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
60
        $rules['roles']    .= '|in:' . $this->getRoleIds()->implode(',');
61
62
        return $rules;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function all()
69
    {
70
        return array_merge(parent::all(), [
71
            'username' => str_slug($this->get('username'))
72
        ]);
73
    }
74
75
    /* ------------------------------------------------------------------------------------------------
76
     |  Other Functions
77
     | ------------------------------------------------------------------------------------------------
78
     */
79
    /**
80
     * Get the role ids.
81
     *
82
     * @return \Illuminate\Database\Eloquent\Collection
83
     */
84
    private function getRoleIds()
85
    {
86
        return Cache::remember('auth.roles.ids', 5, function () {
87
            return app(\Arcanesoft\Contracts\Auth\Models\Role::class)->lists('id');
88
        });
89
    }
90
}
91