Completed
Push — master ( 83241a...8e03fe )
by Anthony
03:35
created

PasswordLengthValidator::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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