UserManager   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 31
c 7
b 0
f 1
lcom 1
cbo 10
dl 0
loc 242
ccs 99
cts 99
cp 1
rs 9.8

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setLogger() 0 4 1
A find() 0 16 4
B create() 0 31 4
B update() 0 25 2
A createOrUpdate() 0 14 3
A remove() 0 14 3
B assignUserToUserGroups() 0 20 5
A unassignUserFromUserGroups() 0 9 3
A getContentType() 0 8 2
A ensureDefaults() 0 10 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\ContentTypeService;
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\Data\ValueObject;
20
use Transfer\EzPlatform\Exception\ObjectNotFoundException;
21
use Transfer\EzPlatform\Exception\UnsupportedObjectOperationException;
22
use Transfer\EzPlatform\Repository\Values\UserGroupObject;
23
use Transfer\EzPlatform\Repository\Values\UserObject;
24
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
25
use Transfer\EzPlatform\Repository\Manager\Type\FinderInterface;
26
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
27
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;
28
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
29
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
30
31
/**
32
 * User manager.
33
 *
34
 * @internal
35
 */
36
class UserManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface
37
{
38
    /**
39
     * @var LoggerInterface
40
     */
41
    private $logger;
42
43
    /**
44
     * @var array
45
     */
46
    private $options;
47
48
    /**
49
     * @var UserService
50
     */
51
    private $userService;
52
53
    /**
54
     * @var ContentTypeService
55
     */
56
    private $contentTypeService;
57
58
    /**
59
     * @var UserGroupManager
60
     */
61
    private $userGroupManager;
62
63
    /**
64
     * @param array              $options
65
     * @param UserService        $userService
66
     * @param ContentTypeService $contentTypeService
67
     * @param UserGroupManager   $userGroupManager
68 5
     */
69
    public function __construct(array $options, UserService $userService, ContentTypeService $contentTypeService, UserGroupManager $userGroupManager)
70 5
    {
71 5
        $this->options = $options;
72 5
        $this->userService = $userService;
73 5
        $this->contentTypeService = $contentTypeService;
74 5
        $this->userGroupManager = $userGroupManager;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79 23
     */
80
    public function setLogger(LoggerInterface $logger)
81 23
    {
82 23
        $this->logger = $logger;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87 10
     */
88
    public function find(ValueObject $object)
89
    {
90 10
        try {
91 10
            if (isset($object->data['username'])) {
92 8
                $user = $this->userService->loadUserByLogin($object->data['username']);
93 10
            }
94
        } catch (NotFoundException $notFoundException) {
95
            // We'll throw our own exception later instead.
96
        }
97 10
98 5
        if (!isset($user)) {
99
            throw new ObjectNotFoundException(User::class, array('username'));
100
        }
101 8
102
        return $user;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107 10
     */
108
    public function create(ObjectInterface $object)
109 6
    {
110 1
        if (!$object instanceof UserObject) {
111
            throw new UnsupportedObjectOperationException(UserObject::class, get_class($object));
112
        }
113 5
114
        $this->ensureDefaults($object);
115 5
116 5
        $userCreateStruct = $this->userService->newUserCreateStruct(
117 5
            $object->data['username'],
118 5
            $object->data['email'],
119 5
            $object->data['password'],
120 5
            $object->data['main_language_code'],
121 5
            $this->getContentType($object)
122
        );
123 5
124
        $object->getMapper()->mapObjectToCreateStruct($userCreateStruct);
125 5
126 5
        $groups = [];
127 5
        foreach ($object->data['parents'] as $userGroup) {
128 10
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
129 5
            if ($userGroup instanceof UserGroupObject) {
130 5
                $groups[] = $this->userGroupManager->find($userGroup);
131 5
            }
132
        }
133 5
134 5
        $user = $this->userService->createUser($userCreateStruct, $groups);
135
        $object->data['id'] = $user->getUserId();
136 5
137
        return $object;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142 10
     */
143
    public function update(ObjectInterface $object)
144 8
    {
145 1
        if (!$object instanceof UserObject) {
146
            throw new UnsupportedObjectOperationException(UserObject::class, get_class($object));
147
        }
148 10
149
        $this->ensureDefaults($object);
150 8
151
        $user = $this->find($object);
152
153 8
        // Populate struct
154 8
        $userUpdateStruct = $this->userService->newUserUpdateStruct();
155
        $object->getMapper()->mapObjectToUpdateStruct($userUpdateStruct);
156
157 8
        // Update user
158
        $user = $this->userService->updateUser($user, $userUpdateStruct);
159
160 8
        // Assign user to usergroups
161
        $userGroups = $this->assignUserToUserGroups($user, $object->data['parents']);
162
163 8
        // Unassign user from usergroups
164
        $this->unassignUserFromUserGroups($user, $userGroups);
165 8
166
        return $object;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171 10
     */
172
    public function createOrUpdate(ObjectInterface $object)
173 10
    {
174 1
        if (!$object instanceof UserObject) {
175
            throw new UnsupportedObjectOperationException(UserObject::class, get_class($object));
176
        }
177
178 10
        try {
179
            $this->find($object);
180 8
181 5
            return $this->update($object);
182 5
        } catch (NotFoundException $notFound) {
183
            return $this->create($object);
184
        }
185
    }
186
187
    /**
188
     * {@inheritdoc}
189 2
     */
190
    public function remove(ObjectInterface $object)
191 2
    {
192 1
        if (!$object instanceof UserObject) {
193
            throw new UnsupportedObjectOperationException(UserObject::class, get_class($object));
194
        }
195 1
196
        $user = $this->find($object);
197 1
198 1
        if ($user) {
199 1
            $this->userService->deleteUser($user);
200
        }
201 1
202
        return true;
203
    }
204
205
    /**
206
     * Assigns a collection of Transfer user groups from an eZ user, and returns the once who were added.
207
     *
208
     * @param User              $user
209
     * @param UserGroupObject[] $userGroupObjects
210
     *
211
     * @return UserGroup[]
212 8
     */
213
    protected function assignUserToUserGroups(User $user, array $userGroupObjects)
214 8
    {
215 8
        $ezUserGroups = [];
216 8
        foreach ($userGroupObjects as $userGroup) {
217 8
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
218 8
            if ($userGroup instanceof UserGroupObject) {
219 8
                $ezUserGroup = $this->userGroupManager->find($userGroup);
220 8
                if ($ezUserGroup) {
221
                    $ezUserGroups[$ezUserGroup->id] = $ezUserGroup;
222 8
                    try {
223 8
                        $this->userService->assignUserToUserGroup($user, $ezUserGroup);
224
                    } catch (InvalidArgumentException $alreadyAssignedException) {
225
                        // Ignore error about: user already assigned to usergroup.
226 8
                    }
227 8
                }
228 8
            }
229
        }
230 8
231
        return $ezUserGroups;
232
    }
233
234
    /**
235
     * Unassigns a collection of eZ UserGroups from an eZ User.
236
     *
237
     * @param User        $user
238
     * @param UserGroup[] $userGroups
239 8
     */
240
    protected function unassignUserFromUserGroups(User $user, array $userGroups)
241 8
    {
242 8
        $existingUserGroups = $this->userService->loadUserGroupsOfUser($user);
243 8
        foreach ($existingUserGroups as $existingUserGroup) {
244 7
            if (!array_key_exists($existingUserGroup->id, $userGroups)) {
245 7
                $this->userService->unAssignUserFromUserGroup($user, $existingUserGroup);
246 8
            }
247 8
        }
248
    }
249
250
    /**
251
     * @param UserObject $object
252
     *
253
     * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType|null
254 5
     */
255
    protected function getContentType(UserObject $object)
256 5
    {
257 1
        if (isset($object->data['content_type'])) {
258
            return $this->contentTypeService->loadContentTypeByIdentifier($object->data['content_type']);
259
        }
260 4
261
        return;
262
    }
263
264
    /**
265
     * @param UserObject $object
266 10
     */
267
    private function ensureDefaults(UserObject $object)
268 10
    {
269
        $defaultData = ['main_language_code'];
270 10
271 10
        foreach ($defaultData as $defaultOption) {
272 1
            if (!isset($object->data[$defaultOption])) {
273 1
                $object->data[$defaultOption] = $this->options[$defaultOption];
274 10
            }
275 10
        }
276
    }
277
}
278