Completed
Push — 1.0 ( 19d976...2156a0 )
by Valentin
05:35
created

UserManager   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 25
c 2
b 0
f 1
lcom 1
cbo 9
dl 0
loc 201
ccs 83
cts 83
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setLogger() 0 4 1
A findByUsername() 0 14 3
B create() 0 28 4
B update() 0 27 3
A createOrUpdate() 0 12 3
A remove() 0 14 3
A assignUserToUserGroups() 0 16 4
A unassignUserFromUserGroups() 0 9 3
1
<?php
2
3
/*
4
 * This file is part of Transfer.
5
 *
6
 * For the full copyright and license information, please view the LICENSE file located
7
 * in the root directory.
8
 */
9
10
namespace Transfer\EzPlatform\Repository\Manager;
11
12
use eZ\Publish\API\Repository\Repository;
13
use eZ\Publish\API\Repository\UserService;
14
use eZ\Publish\API\Repository\Values\User\User;
15
use eZ\Publish\API\Repository\Values\User\UserGroup;
16
use Psr\Log\LoggerAwareInterface;
17
use Psr\Log\LoggerInterface;
18
use Transfer\Data\ObjectInterface;
19
use Transfer\EzPlatform\Data\UserGroupObject;
20
use Transfer\EzPlatform\Data\UserObject;
21
use Transfer\EzPlatform\Exception\UserNotFoundException;
22
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
23
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
24
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;
25
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
26
27
/**
28
 * User manager.
29
 *
30
 * @internal
31
 */
32
class UserManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface
33
{
34
    /**
35
     * @var Repository
36
     */
37
    private $repository;
38
39
    /**
40
     * @var LoggerInterface
41
     */
42
    private $logger;
43
44
    /**
45
     * @var UserService
46
     */
47
    private $userService;
48
49
    /**
50
     * @var UserGroupManager
51
     */
52
    private $userGroupManager;
53
54
    /**
55
     * @param Repository       $repository
56
     * @param UserGroupManager $userGroupManager
57
     */
58 3
    public function __construct(Repository $repository, UserGroupManager $userGroupManager)
59
    {
60 3
        $this->repository = $repository;
61 3
        $this->userService = $repository->getUserService();
62 3
        $this->userGroupManager = $userGroupManager;
63 3
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 3
    public function setLogger(LoggerInterface $logger)
69
    {
70 3
        $this->logger = $logger;
71 3
    }
72
73
    /**
74
     * Finds user object by username.
75
     *
76
     * @param string $username
77
     *
78
     * @return User|false
79
     */
80 10
    public function findByUsername($username)
81 4
    {
82 10
        if (!is_string($username)) {
83 1
            return false;
84
        }
85
86
        try {
87 9
            $user = $this->userService->loadUserByLogin($username);
88 9
        } catch (NotFoundException $e) {
89 5
            return false;
90
        }
91
92 5
        return $user;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 5
    public function create(ObjectInterface $object)
99
    {
100 5
        if (!$object instanceof UserObject) {
101 1
            return;
102
        }
103
104 4
        $userCreateStruct = $this->userService->newUserCreateStruct(
105 4
            $object->data['username'],
106 4
            $object->data['email'],
107 4
            $object->data['password'],
108 4
            $object->data['main_language_code']
109 4
        );
110
111 4
        $object->getMapper()->getNewUserCreateStruct($userCreateStruct);
112
113 4
        $groups = [];
114 4
        foreach ($object->parents as $userGroup) {
115 4
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
116 4
            if ($userGroup instanceof UserGroupObject) {
117 4
                $groups[] = $this->userGroupManager->find($userGroup->data['id']);
118 4
            }
119 4
        }
120
121 4
        $user = $this->userService->createUser($userCreateStruct, $groups);
122 4
        $object->data['id'] = $user->getUserId();
123
124 4
        return $object;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 6
    public function update(ObjectInterface $object)
131
    {
132 6
        if (!$object instanceof UserObject) {
133 1
            return;
134
        }
135
136 5
        $user = $this->findByUsername($object->data['username']);
137
138 5
        if (!$user) {
139 1
            throw new UserNotFoundException(sprintf('User with username "%s" not found.', $object->data['username']));
140
        }
141
142
        // Populate struct
143 4
        $userUpdateStruct = $this->userService->newUserUpdateStruct();
144 4
        $object->getMapper()->getNewUserUpdateStruct($userUpdateStruct);
145
146
        // Update user
147 4
        $ezuser = $this->userService->updateUser($user, $userUpdateStruct);
148
149
        // Assign user to usergroups
150 4
        $ezUserGroups = $this->assignUserToUserGroups($ezuser, $object->parents);
151
152
        // Unassign user from usergroups
153 4
        $this->unassignUserFromUserGroups($ezuser, $ezUserGroups);
154
155 4
        return $object;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 6
    public function createOrUpdate(ObjectInterface $object)
162
    {
163 6
        if (!$object instanceof UserObject) {
164 1
            return;
165
        }
166
167 5
        if (!$this->findByUsername($object->data['username'])) {
168 3
            return $this->create($object);
169
        } else {
170 3
            return $this->update($object);
171
        }
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 3
    public function remove(ObjectInterface $object)
178
    {
179 3
        if (!$object instanceof UserObject) {
180 1
            return;
181
        }
182
183 2
        $user = $this->findByUsername($object->data['username']);
184
185 2
        if ($user) {
186 1
            $this->userService->deleteUser($user);
187 1
        }
188
189 2
        return true;
190
    }
191
192
    /**
193
     * Assigns a collection of Transfer user groups from an eZ user, and returns the once who were added.
194
     *
195
     * @param User              $user
196
     * @param UserGroupObject[] $userGroupObjects
197
     *
198
     * @return UserGroup[]
199
     */
200 4
    protected function assignUserToUserGroups(User $user, array $userGroupObjects)
201
    {
202 4
        $ezUserGroups = [];
203 4
        foreach ($userGroupObjects as $userGroup) {
204 4
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
205 4
            if ($userGroup instanceof UserGroupObject) {
206 4
                $ezUserGroup = $this->userGroupManager->find($userGroup->data['id']);
207 4
                if ($ezUserGroup) {
208 4
                    $ezUserGroups[$ezUserGroup->id] = $ezUserGroup;
209 4
                    $this->userService->assignUserToUserGroup($user, $ezUserGroup);
210 4
                }
211 4
            }
212 4
        }
213
214 4
        return $ezUserGroups;
215
    }
216
217
    /**
218
     * Unassigns a collection of eZ UserGroups from an eZ User.
219
     *
220
     * @param User        $user
221
     * @param UserGroup[] $userGroups
222
     */
223 4
    protected function unassignUserFromUserGroups(User $user, array $userGroups)
224
    {
225 4
        $existingUserGroups = $this->userService->loadUserGroupsOfUser($user);
226 4
        foreach ($existingUserGroups as $existingUserGroup) {
227 4
            if (!array_key_exists($existingUserGroup->id, $userGroups)) {
228 4
                $this->userService->unAssignUserFromUserGroup($user, $existingUserGroup);
229 4
            }
230 4
        }
231 4
    }
232
}
233