Passed
Push — master ( b8147c...403ced )
by Curtis
11:47 queued 05:39
created

PasswordValidator::handle()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 15
c 1
b 0
f 1
nc 16
nop 0
dl 0
loc 25
rs 9.4555
1
<?php
2
3
namespace App\Http\Requests\enso\Core;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
use Illuminate\Validation\Validator;
9
use App\Models\User;
10
11
class PasswordValidator
12
{
13
    private Request $request;
14
    private Validator $validator;
15
    private User $user;
16
17
    public function __construct(Request $request, Validator $validator, User $user)
18
    {
19
        $this->request = $request;
20
        $this->validator = $validator;
21
        $this->user = $user;
22
    }
23
24
    public function handle()
25
    {
26
        if ($this->user->currentPasswordIs($this->request->get('password'))) {
0 ignored issues
show
Bug introduced by
It seems like $this->request->get('password') can also be of type null; however, parameter $password of App\Models\User::currentPasswordIs() 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

26
        if ($this->user->currentPasswordIs(/** @scrutinizer ignore-type */ $this->request->get('password'))) {
Loading history...
27
            $this->validator->errors()
28
                ->add('password', __('You cannot use the existing password'));
0 ignored issues
show
Bug introduced by
It seems like __('You cannot use the existing password') can also be of type array and array; however, parameter $message of Illuminate\Support\MessageBag::add() 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

28
                ->add('password', /** @scrutinizer ignore-type */ __('You cannot use the existing password'));
Loading history...
29
        }
30
31
        if (! $this->hasMinUppercase()) {
32
            $this->validator->errors()
33
                ->add('password', __('Minimum upper case letters count is :number', [
34
                    'number' => config('enso.auth.password.minUpperCase'),
35
                ]));
36
        }
37
38
        if (! $this->hasMinNumeric()) {
39
            $this->validator->errors()
40
                ->add('password', __('Minimum numeric characters count is :number', [
41
                    'number' => config('enso.auth.password.minNumeric'),
42
                ]));
43
        }
44
45
        if (! $this->hasMinSpecial()) {
46
            $this->validator->errors()
47
                ->add('password', __('Minimum special characters count is :number', [
48
                    'number' => config('enso.auth.password.minSpecial'),
49
                ]));
50
        }
51
    }
52
53
    private function hasMinUppercase()
54
    {
55
        if (! config('enso.auth.password.minUpperCase')) {
56
            return true;
57
        }
58
59
        preg_match_all('/[A-Z]+/', $this->request->get('password'), $matches);
60
61
        return $this->length($matches) >= config('enso.auth.password.minUpperCase');
62
    }
63
64
    private function hasMinNumeric()
65
    {
66
        if (! config('enso.auth.password.minNumeric')) {
67
            return true;
68
        }
69
70
        preg_match_all('/[0-9]+/', $this->request->get('password'), $matches);
71
72
        return $this->length($matches) >= config('enso.auth.password.minNumeric');
73
    }
74
75
    private function hasMinSpecial()
76
    {
77
        if (! config('enso.auth.password.minSpecial')) {
78
            return true;
79
        }
80
81
        preg_match_all('/[^\da-zA-Z0-9]+/', $this->request->get('password'), $matches);
82
83
        return $this->length($matches) >= config('enso.auth.password.minSpecial');
84
    }
85
86
    private function length($matches)
87
    {
88
        return Str::length((new Collection($matches))->flatten()->implode(''));
89
    }
90
}
91