DisallowOldPassword   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A message() 0 3 1
A __construct() 0 11 3
A getOldPasswords() 0 12 2
A passes() 0 15 4
1
<?php
2
3
namespace Ikechukwukalu\Sanctumauthstarter\Rules;
4
5
use Ikechukwukalu\Sanctumauthstarter\Models\OldPassword;
6
use Illuminate\Contracts\Validation\Rule;
7
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Hash;
10
11
class DisallowOldPassword implements Rule
0 ignored issues
show
Deprecated Code introduced by
The interface Illuminate\Contracts\Validation\Rule has been deprecated: see ValidationRule ( Ignorable by Annotation )

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

11
class DisallowOldPassword implements /** @scrutinizer ignore-deprecated */ Rule

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
12
{
13
    /**
14
     * Create a new rule instance.
15
     *
16
     * @return void
17
     */
18
19
    private int|bool $checkAll;
20
    private int $number;
21
    private $user;
22
23
    public function __construct($checkAll = true, $number = 4)
24
    {
25
        //
26
        $this->checkAll = $checkAll;
27
        $this->number = $number;
28
29
        if (is_int($this->checkAll) && !empty($this->checkAll)) {
30
            $this->number = $checkAll;
31
        }
32
33
        $this->user = Auth::user();
34
    }
35
36
    /**
37
     * Determine if the validation rule passes.
38
     *
39
     * @param  string  $attribute
40
     * @param  mixed  $value
41
     * @return bool
42
     */
43
    public function passes($attribute, $value)
44
    {
45
        $oldpasswords = $this->getOldPasswords();
46
47
        if ($oldpasswords->count() === 0) {
48
            return !Hash::check($value, $this->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?
Loading history...
49
        }
50
51
        foreach ($oldpasswords as $oldpassword) {
52
            if (Hash::check($value, $oldpassword->password)) {
53
                return false;
54
            }
55
        }
56
57
        return true;
58
    }
59
60
    /**
61
     * Get the validation error message.
62
     *
63
     * @return string
64
     */
65
    public function message()
66
    {
67
        return trans_choice('sanctumauthstarter::passwords.exists', intval(is_int($this->checkAll)), ['number' => $this->number]);
68
    }
69
70
    /**
71
     * Get OldPin Model.
72
     *
73
     * @return \Illuminate\Database\Eloquent\Collection
74
     */
75
    private function getOldPasswords(): EloquentCollection
76
    {
77
        if ($this->checkAll === true) {
78
            return OldPassword::where('user_id', $this->user->id)
0 ignored issues
show
Bug Best Practice introduced by
The expression return Ikechukwukalu\San...ted_at', 'desc')->get() could return the type Illuminate\Database\Eloq...Relations\HasOneThrough which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Collection. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
79
                    ->orderBy('created_at', 'desc')
80
                    ->get();
81
        }
82
83
        return OldPassword::where('user_id', $this->user->id)
0 ignored issues
show
Bug Best Practice introduced by
The expression return Ikechukwukalu\San...e($this->number)->get() could return the type Illuminate\Database\Eloq...Relations\HasOneThrough which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Collection. Consider adding an additional type-check to rule them out.
Loading history...
84
                ->orderBy('created_at', 'desc')
85
                ->take($this->number)
86
                ->get();
87
    }
88
}
89