UserValidationService   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 70
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validateUsername() 0 12 3
A validatePassword() 0 12 3
A validateEmail() 0 12 3
A validateMobile() 0 6 1
A validateFullName() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Core\Component\User\Application\Validation;
16
17
use Acme\App\Core\Port\Validation\PhoneNumber\PhoneNumberException;
18
use Acme\App\Core\Port\Validation\PhoneNumber\PhoneNumberValidatorInterface;
19
use Acme\App\Core\SharedKernel\Exception\InvalidArgumentException;
20
21
/**
22
 * This class is used to provide an example of integrating simple classes as
23
 * services into a Symfony application.
24
 *
25
 * @author Javier Eguiluz <[email protected]>
26
 * @author Herberto Graca <[email protected]>
27
 */
28
class UserValidationService
29
{
30
    /**
31
     * @var PhoneNumberValidatorInterface
32
     */
33
    private $phoneNumberValidator;
34
35
    public function __construct(PhoneNumberValidatorInterface $phoneNumberValidator)
36
    {
37
        $this->phoneNumberValidator = $phoneNumberValidator;
38
    }
39
40
    public function validateUsername(?string $username): string
41
    {
42
        if (empty($username)) {
43
            throw new InvalidArgumentException('The username can not be empty.');
44
        }
45
46
        if (preg_match('/^[a-z_]+$/', $username) !== 1) {
47
            throw new InvalidArgumentException('The username must contain only lowercase latin characters and underscores.');
48
        }
49
50
        return $username;
51
    }
52
53
    public function validatePassword(?string $plainPassword): string
54
    {
55
        if (empty($plainPassword)) {
56
            throw new InvalidArgumentException('The password can not be empty.');
57
        }
58
59
        if (mb_strlen(trim($plainPassword)) < 6) {
60
            throw new InvalidArgumentException('The password must be at least 6 characters long.');
61
        }
62
63
        return $plainPassword;
64
    }
65
66
    public function validateEmail(?string $email): string
67
    {
68
        if (empty($email)) {
69
            throw new InvalidArgumentException('The email can not be empty.');
70
        }
71
72
        if (mb_strpos($email, '@') === false) {
73
            throw new InvalidArgumentException('The email should look like a real email.');
74
        }
75
76
        return $email;
77
    }
78
79
    /**
80
     * @throws PhoneNumberException
81
     */
82
    public function validateMobile(?string $mobile): string
83
    {
84
        $this->phoneNumberValidator->validate($mobile);
85
86
        return $mobile;
87
    }
88
89
    public function validateFullName(?string $fullName): string
90
    {
91
        if (empty($fullName)) {
92
            throw new InvalidArgumentException('The full name can not be empty.');
93
        }
94
95
        return $fullName;
96
    }
97
}
98