Completed
Push — 1.0 ( 19670d...450ed0 )
by Valentin
09:36
created

UserManager::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 2
    public function __construct(Repository $repository, UserGroupManager $userGroupManager)
59
    {
60 2
        $this->repository = $repository;
61 2
        $this->userService = $repository->getUserService();
62 2
        $this->userGroupManager = $userGroupManager;
63 2
    }
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 9
    public function findByUsername($username)
81 3
    {
82 9
        if (!is_string($username)) {
83 1
            return false;
84
        }
85
86
        try {
87 8
            $user = $this->userService->loadUserByLogin($username);
88 8
        } catch (NotFoundException $e) {
89 4
            return false;
90
        }
91
92 4
        return $user;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 4
    public function create(ObjectInterface $object)
99
    {
100 4
        if (!$object instanceof UserObject) {
101 1
            return;
102
        }
103
104 3
        $userCreateStruct = $this->userService->newUserCreateStruct(
105 3
            $object->data['username'],
106 3
            $object->data['email'],
107 3
            $object->data['password'],
108 3
            $object->data['main_language_code']
109 3
        );
110
111 3
        $object->getMapper()->getNewUserCreateStruct($userCreateStruct);
112
113 3
        $groups = [];
114 3
        foreach ($object->parents as $userGroup) {
115 3
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
116 3
            if ($userGroup instanceof UserGroupObject) {
117 3
                $groups[] = $this->userGroupManager->find($userGroup->data['id']);
118 3
            }
119 3
        }
120
121 3
        $user = $this->userService->createUser($userCreateStruct, $groups);
122 3
        $object->data['id'] = $user->getUserId();
123
124 3
        return $object;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 5
    public function update(ObjectInterface $object)
131
    {
132 5
        if (!$object instanceof UserObject) {
133 1
            return;
134
        }
135
136 4
        $user = $this->findByUsername($object->data['username']);
137
138 4
        if (!$user) {
139 1
            throw new UserNotFoundException(sprintf('User with username "%s" not found.', $object->data['username']));
140
        }
141
142
        // Populate struct
143 3
        $userUpdateStruct = $this->userService->newUserUpdateStruct();
144 3
        $object->getMapper()->getNewUserUpdateStruct($userUpdateStruct);
145
146
        // Update user
147 3
        $ezuser = $this->userService->updateUser($user, $userUpdateStruct);
148
149
        // Assign user to usergroups
150 3
        $ezUserGroups = $this->assignUserToUserGroups($ezuser, $object->parents);
151
152
        // Unassign user from usergroups
153 3
        $this->unassignUserFromUserGroups($ezuser, $ezUserGroups);
154
155 3
        return $object;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 5
    public function createOrUpdate(ObjectInterface $object)
162
    {
163 5
        if (!$object instanceof UserObject) {
164 1
            return;
165
        }
166
167 4
        if (!$this->findByUsername($object->data['username'])) {
168 2
            return $this->create($object);
169
        } else {
170 2
            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 3
    protected function assignUserToUserGroups(User $user, array $userGroupObjects)
201
    {
202 3
        $ezUserGroups = [];
203 3
        foreach ($userGroupObjects as $userGroup) {
204 3
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
205 3
            if ($userGroup instanceof UserGroupObject) {
206 3
                $ezUserGroup = $this->userGroupManager->find($userGroup->data['id']);
207 3
                if ($ezUserGroup) {
208 3
                    $ezUserGroups[$ezUserGroup->id] = $ezUserGroup;
209 3
                    $this->userService->assignUserToUserGroup($user, $ezUserGroup);
210 3
                }
211 3
            }
212 3
        }
213
214 3
        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 3
    protected function unassignUserFromUserGroups(User $user, array $userGroups)
224
    {
225 3
        $existingUserGroups = $this->userService->loadUserGroupsOfUser($user);
226 3
        foreach ($existingUserGroups as $existingUserGroup) {
227 3
            if (!array_key_exists($existingUserGroup->id, $userGroups)) {
228 3
                $this->userService->unAssignUserFromUserGroup($user, $existingUserGroup);
229 3
            }
230 3
        }
231 3
    }
232
}
233