Password::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 17
rs 10
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Skrill\ValueObject;
6
7
use Skrill\Exception\InvalidPasswordException;
8
use Skrill\ValueObject\Traits\ValueToStringTrait;
9
10
/**
11
 * Value object for Skrill API/MQI password.
12
 *
13
 * - At least 8 characters long
14
 * - At least 1 alphabetic character (A-Z)
15
 * - At least 1 non-alphabetic character (0-9, ., +, etc.)
16
 * - Cannot be the same as your email address
17
 *
18
 * @see https://www.skrill.com/fileadmin/content/pdf/Skrill_Quick_Checkout_Guide.pdf
19
 */
20
final class Password
21
{
22
    use ValueToStringTrait;
23
24
    public const MIN_LENGTH = 8;
25
26
    /**
27
     * @param string $value
28
     *
29
     * @throws InvalidPasswordException
30
     */
31
    public function __construct(string $value)
32
    {
33
        $value = trim($value);
34
35
        if (strlen($value) < self::MIN_LENGTH) {
36
            throw InvalidPasswordException::invalidMinLength();
37
        }
38
39
        if (!preg_match('/\pL/u', $value)) {
40
            throw InvalidPasswordException::missingLetters();
41
        }
42
43
        if (!preg_match('/\pN/u', $value)) {
44
            throw InvalidPasswordException::missingNumbers();
45
        }
46
47
        $this->value = $value;
48
    }
49
}
50