Password::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.6111
cc 5
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the G.L.S.R. Apps package.
7
 *
8
 * (c) Dev-Int Création <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace User\Domain\Model\VO;
15
16
use User\Domain\Exception\PasswordMustHaveDigitException;
17
use User\Domain\Exception\PasswordMustHaveSpecialCharactersException;
18
use User\Domain\Exception\PasswordMustHaveUpperException;
19
use User\Domain\Exception\PasswordMustReach8CharactersException;
20
21
final class Password
22
{
23
    private string $password;
24
25
    public function __construct(string $password)
26
    {
27
        if (\strlen($password) < 8) {
28
            throw new PasswordMustReach8CharactersException();
29
        }
30
        if (!\preg_match('/([A-Z])/', $password)) {
31
            throw new PasswordMustHaveUpperException();
32
        }
33
        if (!\preg_match('/([\d])/', $password)) {
34
            throw new PasswordMustHaveDigitException();
35
        }
36
        if (!\preg_match('/([\-#$+_!()@])/', $password)) {
37
            throw new PasswordMustHaveSpecialCharactersException();
38
        }
39
40
        $this->password = $password;
41
    }
42
43
    public static function fromString(string $password): self
44
    {
45
        return new self($password);
46
    }
47
48
    public function value(): string
49
    {
50
        return $this->password;
51
    }
52
}
53