Completed
Pull Request — master (#183)
by Cristian
02:08
created

ChangePasswordRequest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 3
Bugs 2 Features 0
Metric Value
dl 0
loc 44
rs 10
c 3
b 2
f 0
wmc 4
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 5 1
A rules() 0 8 1
A withValidator() 0 9 2
1
<?php
2
3
namespace Backpack\Base\app\Http\Requests;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Hash;
8
9
class ChangePasswordRequest extends FormRequest
10
{
11
    /**
12
     * Determine if the user is authorized to make this request.
13
     *
14
     * @return bool
15
     */
16
    public function authorize()
17
    {
18
        // only allow updates if the user is logged in
19
        return Auth::check();
20
    }
21
22
    /**
23
     * Get the validation rules that apply to the request.
24
     *
25
     * @return array
26
     */
27
    public function rules()
28
    {
29
        return [
30
            'old_password'     => 'required',
31
            'new_password'     => 'required|min:6',
32
            'confirm_password' => 'required|same:new_password|min:6',
33
        ];
34
    }
35
36
    /**
37
     * Configure the validator instance.
38
     *
39
     * @param \Illuminate\Validation\Validator $validator
40
     *
41
     * @return void
42
     */
43
    public function withValidator($validator)
44
    {
45
        $validator->after(function ($validator) {
46
            // check old password matches
47
            if (!Hash::check($this->input('old_password'), Auth::user()->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?

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...
48
                $validator->errors()->add('old_password', trans('backpack::base.old_password_incorrect'));
49
            }
50
        });
51
    }
52
}
53