Completed
Branch v2.0.0 (e47d62)
by Alexander
03:40
created

UserSpecification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Domain\User\Specifications;
4
5
use Domain\User\Exceptions\UserSpecificationException;
6
use Domain\User\User;
7
8
class UserSpecification
9
{
10
    /** @var User $user */
11
    private $user;
12
13
    public function __construct(User $user)
14
    {
15
        $this->user = $user;
16
    }
17
18
    public function validate()
19
    {
20
        $this->validateIdentifier();
21
        $this->doBasicValidation();
22
    }
23
24
    public function validateNew()
25
    {
26
        $this->doBasicValidation();
27
28
        $passwordSpecification = new UserPasswordSpecification($this->user->getPassword());
0 ignored issues
show
Bug introduced by
The method getPassword() does not seem to exist on object<Domain\User\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
        $passwordSpecification->validate();
30
    }
31
32
    public function validateIdentifier()
33
    {
34
        $userID = $this->user->getUserID();
35
36
        if (!is_int($userID)) {
37
            throw new UserSpecificationException('Property `userID` must be integer.');
38
        }
39
40
        if ($userID <= 0) {
41
            throw new UserSpecificationException('Property `userID` must be more than 0.');
42
        }
43
    }
44
45
    private function doBasicValidation()
46
    {
47
        $this->validateEmail();
48
        $this->validateName();
49
    }
50
51
    private function validateEmail()
52
    {
53
        $email = $this->user->getEmail();
54
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
55
            throw new UserSpecificationException('Property `email` is not valid.');
56
        }
57
    }
58
59
    private function validateName()
60
    {
61
        $name = $this->user->getName();
62
        if (\mb_strlen($name) < 1) {
63
            throw new UserSpecificationException('`name` length should be more than 1 character.');
64
        }
65
        if (\mb_strlen($name) > 50) {
66
            throw new UserSpecificationException('`name` length should be less than 50 characters.');
67
        }
68
    }
69
}
70