Completed
Push — master ( 39baf9...94a3ba )
by Anthony
03:56
created

PasswordMinimumNumberValidator::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Groundsix\Password\Validators;
5
6
use Groundsix\Password\PasswordException;
7
use Groundsix\Password\Validator;
8
9
class PasswordMinimumNumberValidator implements Validator
10
{
11
    /** @var int $minimum */
12
    private $minimum;
13
14
    /**
15
     * PasswordMinimumNumberValidator constructor.
16
     * @param int $minimum The minimum number of numbers that should be in the password
17
     */
18
    public function __construct(int $minimum = 1)
19
    {
20
        $this->minimum = $minimum;
21
    }
22
23
    /** {@inheritdoc} */
24
    public function validate(string $password): bool
25
    {
26
        $numbers = preg_match_all('/\d/', $password);
27
28
        if ($numbers >= $this->minimum) {
29
            return true;
30
        }
31
32
        throw new PasswordException('Password must contain at least ' . $this->minimum . ' numbers.');
33
    }
34
}