Issues (78)

src/Rules/CurrentPin.php (4 issues)

1
<?php
2
3
namespace Ikechukwukalu\Requirepin\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Hash;
8
9
class CurrentPin implements Rule
10
{
11
12
    private bool $defaultPin = false;
13
    private bool $allowDefaultPin = false;
14
    /**
15
     * Create a new rule instance.
16
     *
17
     * @return void
18
     */
19
    public function __construct(bool $allowDefaultPin = false)
20
    {
21
        //
22
        $this->allowDefaultPin = $allowDefaultPin;
23
    }
24
25
    /**
26
     * Determine if the validation rule passes.
27
     *
28
     * @param  string  $attribute
29
     * @param  mixed  $value
30
     * @return bool
31
     */
32
    public function passes($attribute, $value)
33
    {
34
        if (Auth::guard(config('requirepin.auth_guard', 'web'))->user()->default_pin && !$this->allowDefaultPin) {
0 ignored issues
show
Accessing default_pin on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
35
            $this->defaultPin = true;
36
37
            return false;
38
        }
39
40
        return Hash::check($value, Auth::guard(config('requirepin.auth_guard', 'web'))->user()->pin);
0 ignored issues
show
Accessing pin on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
41
    }
42
43
    /**
44
     * Get the validation error message.
45
     *
46
     * @return string
47
     */
48
    public function message()
49
    {
50
        if ($this->defaultPin) {
51
            return trans('requirepin::pin.default');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('requirepin::pin.default') also could return the type Illuminate\Contracts\Translation\Translator|array which is incompatible with the documented return type string.
Loading history...
52
        }
53
54
        return trans('requirepin::pin.wrong');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('requirepin::pin.wrong') also could return the type Illuminate\Contracts\Translation\Translator|array which is incompatible with the documented return type string.
Loading history...
55
    }
56
}
57