UserValidator::throwException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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