Completed
Push — 1.0 ( 2156a0...733517 )
by Valentin
07:09
created

UserManager::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 23
ccs 10
cts 10
cp 1
rs 9.0856
cc 2
eloc 10
nc 2
nop 1
crap 2
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\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 Repository
40
     */
41
    private $repository;
42
43
    /**
44
     * @var LoggerInterface
45
     */
46
    private $logger;
47
48
    /**
49
     * @var UserService
50
     */
51
    private $userService;
52
53
    /**
54
     * @var UserGroupManager
55
     */
56
    private $userGroupManager;
57
58
    /**
59
     * @param Repository       $repository
60
     * @param UserGroupManager $userGroupManager
61
     */
62 3
    public function __construct(Repository $repository, UserGroupManager $userGroupManager)
63
    {
64 3
        $this->repository = $repository;
65 3
        $this->userService = $repository->getUserService();
66 3
        $this->userGroupManager = $userGroupManager;
67 3
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 19
    public function setLogger(LoggerInterface $logger)
73
    {
74 19
        $this->logger = $logger;
75 19
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 7 View Code Duplication
    public function find(ValueObject $object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81 2
    {
82
        try {
83 7
            if (isset($object->data['username'])) {
84 7
                $user = $this->userService->loadUserByLogin($object->data['username']);
85 6
            }
86 7
        } catch (NotFoundException $notFoundException) {
87
            // We'll throw our own exception later instead.
88
        }
89
90 7
        if (!isset($user)) {
91 2
            throw new ObjectNotFoundException(User::class, array('username'));
92
        }
93
94 6
        return $user;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 3
    public function create(ObjectInterface $object)
101
    {
102 3
        if (!$object instanceof UserObject) {
103 1
            throw new UnsupportedObjectOperationException(UserObject::class, get_class($object));
104
        }
105
106 2
        $userCreateStruct = $this->userService->newUserCreateStruct(
107 2
            $object->data['username'],
108 2
            $object->data['email'],
109 2
            $object->data['password'],
110 2
            $object->data['main_language_code']
111 2
        );
112
113 2
        $object->getMapper()->getNewUserCreateStruct($userCreateStruct);
114
115 2
        $groups = [];
116 2
        foreach ($object->parents as $userGroup) {
117 2
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
118 2
            if ($userGroup instanceof UserGroupObject) {
119 2
                $groups[] = $this->userGroupManager->find($userGroup);
120 2
            }
121 2
        }
122
123 2
        $user = $this->userService->createUser($userCreateStruct, $groups);
124 2
        $object->data['id'] = $user->getUserId();
125
126 2
        return $object;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 7
    public function update(ObjectInterface $object)
133
    {
134 6
        if (!$object instanceof UserObject) {
135 1
            throw new UnsupportedObjectOperationException(UserObject::class, get_class($object));
136
        }
137
138 6
        $user = $this->find($object);
139
140
        // Populate struct
141 6
        $userUpdateStruct = $this->userService->newUserUpdateStruct();
142 6
        $object->getMapper()->getNewUserUpdateStruct($userUpdateStruct);
143
144
        // Update user
145 6
        $user = $this->userService->updateUser($user, $userUpdateStruct);
146
147
        // Assign user to usergroups
148 7
        $userGroups = $this->assignUserToUserGroups($user, $object->parents);
149
150
        // Unassign user from usergroups
151 6
        $this->unassignUserFromUserGroups($user, $userGroups);
152
153 6
        return $object;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 7
    public function createOrUpdate(ObjectInterface $object)
160
    {
161 7
        if (!$object instanceof UserObject) {
162 1
            throw new UnsupportedObjectOperationException(UserObject::class, get_class($object));
163
        }
164
165
        try {
166 7
            $this->find($object);
167
168 6
            return $this->update($object);
169 2
        } catch (NotFoundException $notFound) {
170 2
            return $this->create($object);
171
        }
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 2
    public function remove(ObjectInterface $object)
178
    {
179 2
        if (!$object instanceof UserObject) {
180 1
            throw new UnsupportedObjectOperationException(UserObject::class, get_class($object));
181
        }
182
183 1
        $user = $this->find($object);
184
185 1
        if ($user) {
186 1
            $this->userService->deleteUser($user);
187 1
        }
188
189 1
        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 6
    protected function assignUserToUserGroups(User $user, array $userGroupObjects)
201
    {
202 6
        $ezUserGroups = [];
203 6
        foreach ($userGroupObjects as $userGroup) {
204 6
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
205 6
            if ($userGroup instanceof UserGroupObject) {
206 6
                $ezUserGroup = $this->userGroupManager->find($userGroup);
207 6
                if ($ezUserGroup) {
208 6
                    $ezUserGroups[$ezUserGroup->id] = $ezUserGroup;
209
                    try {
210 6
                        $this->userService->assignUserToUserGroup($user, $ezUserGroup);
211 6
                    } catch (InvalidArgumentException $alreadyAssignedException) {
212
                        // Ignore error about: user already assigned to usergroup.
213
                    }
214 6
                }
215 6
            }
216 6
        }
217
218 6
        return $ezUserGroups;
219
    }
220
221
    /**
222
     * Unassigns a collection of eZ UserGroups from an eZ User.
223
     *
224
     * @param User        $user
225
     * @param UserGroup[] $userGroups
226
     */
227 6
    protected function unassignUserFromUserGroups(User $user, array $userGroups)
228
    {
229 6
        $existingUserGroups = $this->userService->loadUserGroupsOfUser($user);
230 6
        foreach ($existingUserGroups as $existingUserGroup) {
231 6
            if (!array_key_exists($existingUserGroup->id, $userGroups)) {
232 5
                $this->userService->unAssignUserFromUserGroup($user, $existingUserGroup);
233 5
            }
234 6
        }
235 6
    }
236
}
237