Test Failed
Push — master ( fe954a...0e0ddf )
by Mike
02:18
created

UserValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
4
namespace Xervice\User\Business\Validator;
5
6
7
use DataProvider\UserDataProvider;
8
use Xervice\User\Business\Exception\UserException;
9
use Xervice\User\UserQueryContainerInterface;
10
11
class UserValidator implements UserValidatorInterface
12
{
13
    /**
14
     * @var \Xervice\User\UserQueryContainerInterface
15
     */
16
    private $queryContainer;
17
18
    /**
19
     * UserValidator constructor.
20
     *
21
     * @param \Xervice\User\UserQueryContainerInterface $queryContainer
22
     */
23
    public function __construct(UserQueryContainerInterface $queryContainer)
24
    {
25
        $this->queryContainer = $queryContainer;
26
    }
27
28
    /**
29
     * @param \DataProvider\UserDataProvider $userDataProvider
30
     *
31
     * @throws \Xervice\User\Business\Exception\UserException
32
     */
33
    public function validateUser(UserDataProvider $userDataProvider)
34
    {
35
        $this->userHasEmail($userDataProvider);
36
        $this->validateLoginHasCredential($userDataProvider);
37
        $this->validateWithDb(
38
            $userDataProvider,
39
            $this->queryContainer->getUserFromEmail($userDataProvider->getEmail())
40
        );
41
    }
42
43
    /**
44
     * @param \DataProvider\UserDataProvider $userDataProvider
45
     * @param \DataProvider\UserDataProvider $dbUser
46
     *
47
     * @throws \Xervice\User\Business\Exception\UserException
48
     */
49
    protected function validateWithDb(UserDataProvider $userDataProvider, UserDataProvider $dbUser): void
50
    {
51
        $this->userExist($userDataProvider, $dbUser);
52
        $this->userLoginExist($userDataProvider, $dbUser);
53
    }
54
55
    /**
56
     * @param \DataProvider\UserDataProvider $userDataProvider
57
     *
58
     * @throws \Xervice\User\Business\Exception\UserException
59
     */
60
    private function validateLoginHasCredential(UserDataProvider $userDataProvider): void
61
    {
62
        foreach ($userDataProvider->getUserLogins() as $login) {
63
            if (!$login->hasUserCredential()) {
64
                $this->throwException('Login %s has no credentials', $login->getType());
65
            }
66
        }
67
    }
68
69
    /**
70
     * @param \DataProvider\UserDataProvider $userDataProvider
71
     * @param \DataProvider\UserDataProvider $dbUser
72
     *
73
     * @throws \Xervice\User\Business\Exception\UserException
74
     */
75
    private function userLoginExist(UserDataProvider $userDataProvider, UserDataProvider $dbUser): void
76
    {
77
        if ($userDataProvider->hasUserLogins()) {
78
            foreach ($userDataProvider->getUserLogins() as $login) {
79
                foreach ($dbUser->getUserLogins() as $dbLogin) {
80
                    if (
81
                        $dbLogin->getType() === $login->getType()
82
                        && $dbLogin->getUserLoginId() !== $login->getUserLoginId()
83
                    ) {
84
                        $this->throwException(
85
                            'Login type %s already exist for this user',
86
                            $login->getType()
87
                        );
88
                    }
89
                }
90
            }
91
        }
92
    }
93
94
    /**
95
     * @param \DataProvider\UserDataProvider $userDataProvider
96
     * @param \DataProvider\UserDataProvider $dbUser
97
     *
98
     * @throws \Xervice\User\Business\Exception\UserException
99
     */
100
    private function userExist(UserDataProvider $userDataProvider, UserDataProvider $dbUser): void
101
    {
102
        if (
103
            $dbUser->hasUserId()
104
            && $dbUser->getUserId() !== $userDataProvider->getUserId()
105
        ) {
106
            $this->throwException('User already exists');
107
        }
108
    }
109
110
    /**
111
     * @throws \Xervice\User\Business\Exception\UserException
112
     */
113
    private function userHasEmail(UserDataProvider $userDataProvider): void
114
    {
115
        if (!$userDataProvider->hasEmail()) {
116
            $this->throwException(
117
                'User has no email'
118
            );
119
        }
120
    }
121
122
    /**
123
     * @param $message
124
     * @param mixed ...$params
125
     *
126
     * @throws \Xervice\User\Business\Exception\UserException
127
     */
128
    private function throwException($message, ...$params): void
129
    {
130
        throw new UserException(
131
            sprintf(
132
                $message,
133
                ...$params
134
            )
135
        );
136
    }
137
}