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

UserWriter::updateUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 0
cts 5
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\Writer;
5
6
7
use DataProvider\UserDataProvider;
8
use DataProvider\UserLoginDataProvider;
9
use Orm\Xervice\User\Persistence\User;
0 ignored issues
show
Bug introduced by
The type Orm\Xervice\User\Persistence\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Orm\Xervice\User\Persistence\UserCredential;
0 ignored issues
show
Bug introduced by
The type Orm\Xervice\User\Persistence\UserCredential was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Orm\Xervice\User\Persistence\UserLogin;
0 ignored issues
show
Bug introduced by
The type Orm\Xervice\User\Persistence\UserLogin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Xervice\User\Business\Exception\UserException;
13
use Xervice\User\Business\Validator\UserValidatorInterface;
14
use Xervice\User\UserQueryContainerInterface;
15
16
class UserWriter implements UserWriterInterface
17
{
18
    /**
19
     * @var \Xervice\User\Business\Validator\UserValidatorInterface
20
     */
21
    private $validator;
22
23
    /**
24
     * @var \Xervice\User\UserQueryContainerInterface
25
     */
26
    private $queryContainer;
27
28
    /**
29
     * UserWriter constructor.
30
     *
31
     * @param \Xervice\User\Business\Validator\UserValidatorInterface $validator
32
     * @param \Xervice\User\UserQueryContainerInterface $queryContainer
33
     */
34
    public function __construct(
35
        UserValidatorInterface $validator,
36
        UserQueryContainerInterface $queryContainer
37
    ) {
38
        $this->validator = $validator;
39
        $this->queryContainer = $queryContainer;
40
    }
41
42
    /**
43
     * @param \DataProvider\UserDataProvider $userDataProvider
44
     *
45
     * @return \DataProvider\UserDataProvider
46
     * @throws \Propel\Runtime\Exception\PropelException
47
     * @throws \Xervice\User\Business\Exception\UserException
48
     */
49
    public function createUser(UserDataProvider $userDataProvider): UserDataProvider
50
    {
51
        $this->validator->validateUser($userDataProvider);
52
53
        $user = new User();
54
        return $this->persistUserFromDataProvider($userDataProvider, $user);
55
    }
56
57
    /**
58
     * @param \DataProvider\UserDataProvider $userDataProvider
59
     *
60
     * @return \DataProvider\UserDataProvider
61
     * @throws \Propel\Runtime\Exception\PropelException
62
     * @throws \Xervice\User\Business\Exception\UserException
63
     */
64
    public function updateUser(UserDataProvider $userDataProvider): UserDataProvider
65
    {
66
        $this->validator->validateUser($userDataProvider);
67
68
        $user = $this->queryContainer->getUserQuery()->findOneByUserId($userDataProvider->getUserId());
69
        return $this->persistUserFromDataProvider($userDataProvider, $user);
70
    }
71
72
    /**
73
     * @param \DataProvider\UserDataProvider $userDataProvider
74
     *
75
     * @throws \Propel\Runtime\Exception\PropelException
76
     */
77
    public function deleteUser(UserDataProvider $userDataProvider): void
78
    {
79
        $user = new User();
80
        $user->setUserId($userDataProvider->getUserId());
81
        $user->delete();
82
    }
83
84
    /**
85
     * @param \Orm\Xervice\User\Persistence\User $user
86
     * @param \DataProvider\UserDataProvider $userDataProvider
87
     *
88
     * @return \Orm\Xervice\User\Persistence\User
89
     */
90
    private function hydrateEntityFromDataProvider(User $user, UserDataProvider $userDataProvider): User
91
    {
92
        $user->fromArray($userDataProvider->toArray());
93
94
        if ($userDataProvider->hasUserLogins()) {
95
            foreach ($userDataProvider->getUserLogins() as $loginDataProvider) {
96
                $userLoginEntity = $this->getUserLoginEntity($loginDataProvider);
97
                $user->addUserLogin($userLoginEntity);
98
            }
99
        }
100
101
        return $user;
102
    }
103
104
    /**
105
     * @param \DataProvider\UserLoginDataProvider $loginDataProvider
106
     *
107
     * @return \Orm\Xervice\User\Persistence\UserLogin
108
     * @throws \Propel\Runtime\Exception\PropelException
109
     */
110
    private function getUserLoginEntity(UserLoginDataProvider $loginDataProvider): UserLogin
111
    {
112
        $userLoginEntity = new UserLogin();
113
        $userLoginEntity->fromArray($loginDataProvider->toArray());
114
115
        if ($loginDataProvider->hasUserCredential()) {
116
            $userLoginEntity->setUserCredential(
117
                $this->getUserCredentialsEntity($loginDataProvider->getUserCredential())
118
            );
119
        }
120
121
        return $userLoginEntity;
122
    }
123
124
    /**
125
     * @param $credentialDataProvider
126
     *
127
     * @return \Orm\Xervice\User\Persistence\UserCredential
128
     */
129
    private function getUserCredentialsEntity($credentialDataProvider): UserCredential
130
    {
131
        $userCredentialEntity = new UserCredential();
132
        $userCredentialEntity->fromArray($credentialDataProvider->toArray());
133
        return $userCredentialEntity;
134
    }
135
136
    /**
137
     * @param \DataProvider\UserDataProvider $userDataProvider
138
     * @param $user
139
     *
140
     * @return \DataProvider\UserDataProvider
141
     * @throws \Propel\Runtime\Exception\PropelException
142
     */
143
    private function persistUserFromDataProvider(
144
        UserDataProvider $userDataProvider,
145
        User $user
146
    ): \DataProvider\UserDataProvider {
147
148
        $user = $this->hydrateEntityFromDataProvider($user, $userDataProvider);
149
        $user->save();
150
151
        return $userDataProvider->setUserId($user->getUserId());
152
    }
153
}