Completed
Pull Request — 1.0 (#53)
by Harald
05:27
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\Data\ValueObject;
20
use Transfer\EzPlatform\Repository\Values\UserGroupObject;
21
use Transfer\EzPlatform\Repository\Values\UserObject;
22
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
23
use Transfer\EzPlatform\Repository\Manager\Type\FinderInterface;
24
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
25
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;
26
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
27
28
/**
29
 * User manager.
30
 *
31
 * @internal
32
 */
33
class UserManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface
34
{
35
    /**
36
     * @var Repository
37
     */
38
    private $repository;
39
40
    /**
41
     * @var LoggerInterface
42
     */
43
    private $logger;
44
45
    /**
46
     * @var UserService
47
     */
48
    private $userService;
49
50
    /**
51
     * @var UserGroupManager
52
     */
53
    private $userGroupManager;
54
55
    /**
56
     * @param Repository       $repository
57
     * @param UserGroupManager $userGroupManager
58
     */
59 1
    public function __construct(Repository $repository, UserGroupManager $userGroupManager)
60
    {
61 1
        $this->repository = $repository;
62 1
        $this->userService = $repository->getUserService();
63 1
        $this->userGroupManager = $userGroupManager;
64 1
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    public function setLogger(LoggerInterface $logger)
70
    {
71 1
        $this->logger = $logger;
72 1
    }
73
74
    /**
75
     * Finds user object by username.
76
     *
77
     * @param ValueObject $object
78
     * @param bool        $throwException
79
     *
80
     * @return User|false
81
     *
82
     * @throws NotFoundException
83
     */
84 1 View Code Duplication
    public function find(ValueObject $object, $throwException = false)
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...
85
    {
86
        try {
87 1
            if (isset($object->data['username'])) {
88 1
                $user = $this->userService->loadUserByLogin($object->data['username']);
89 1
            }
90 1
        } catch (NotFoundException $notFoundException) {
91 1
            $exception = $notFoundException;
92
        }
93
94 1
        if (!isset($user)) {
95 1
            if (isset($exception) && $throwException) {
96
                throw $exception;
97
            }
98
99 1
            return false;
100
        }
101
102 1
        return $user;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 1
    public function create(ObjectInterface $object)
109
    {
110 1
        if (!$object instanceof UserObject) {
111
            return;
112
        }
113
114 1
        $userCreateStruct = $this->userService->newUserCreateStruct(
115 1
            $object->data['username'],
116 1
            $object->data['email'],
117 1
            $object->data['password'],
118 1
            $object->data['main_language_code']
119 1
        );
120
121 1
        $object->getMapper()->getNewUserCreateStruct($userCreateStruct);
122
123 1
        $groups = [];
124 1
        foreach ($object->parents as $userGroup) {
125 1
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
126 1
            if ($userGroup instanceof UserGroupObject) {
127 1
                $groups[] = $this->userGroupManager->find($userGroup);
128 1
            }
129 1
        }
130
131 1
        $user = $this->userService->createUser($userCreateStruct, $groups);
132 1
        $object->data['id'] = $user->getUserId();
133
134 1
        return $object;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 1
    public function update(ObjectInterface $object)
141
    {
142 1
        if (!$object instanceof UserObject) {
143
            return;
144
        }
145
146 1
        $user = $this->find($object, true);
147
148
        // Populate struct
149 1
        $userUpdateStruct = $this->userService->newUserUpdateStruct();
150 1
        $object->getMapper()->getNewUserUpdateStruct($userUpdateStruct);
151
152
        // Update user
153 1
        $ezuser = $this->userService->updateUser($user, $userUpdateStruct);
0 ignored issues
show
Security Bug introduced by
It seems like $user defined by $this->find($object, true) on line 146 can also be of type false; however, eZ\Publish\API\Repositor...erService::updateUser() does only seem to accept object<eZ\Publish\API\Re...itory\Values\User\User>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
154
155
        // Assign user to usergroups
156 1
        $ezUserGroups = $this->assignUserToUserGroups($ezuser, $object->parents);
157
158
        // Unassign user from usergroups
159 1
        $this->unassignUserFromUserGroups($ezuser, $ezUserGroups);
160
161 1
        return $object;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 1 View Code Duplication
    public function createOrUpdate(ObjectInterface $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...
168
    {
169 1
        if (!$object instanceof UserObject) {
170
            return;
171
        }
172
173 1
        if (!$this->find($object)) {
174 1
            return $this->create($object);
175
        } else {
176 1
            return $this->update($object);
177
        }
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function remove(ObjectInterface $object)
184
    {
185
        if (!$object instanceof UserObject) {
186
            return;
187
        }
188
189
        $user = $this->find($object);
190
191
        if ($user) {
192
            $this->userService->deleteUser($user);
193
        }
194
195
        return true;
196
    }
197
198
    /**
199
     * Assigns a collection of Transfer user groups from an eZ user, and returns the once who were added.
200
     *
201
     * @param User              $user
202
     * @param UserGroupObject[] $userGroupObjects
203
     *
204
     * @return UserGroup[]
205
     */
206 1
    protected function assignUserToUserGroups(User $user, array $userGroupObjects)
207
    {
208 1
        $ezUserGroups = [];
209 1
        foreach ($userGroupObjects as $userGroup) {
210 1
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
211 1
            if ($userGroup instanceof UserGroupObject) {
212 1
                $ezUserGroup = $this->userGroupManager->find($userGroup);
213 1
                if ($ezUserGroup) {
214 1
                    $ezUserGroups[$ezUserGroup->id] = $ezUserGroup;
215 1
                    $this->userService->assignUserToUserGroup($user, $ezUserGroup);
216 1
                }
217 1
            }
218 1
        }
219
220 1
        return $ezUserGroups;
221
    }
222
223
    /**
224
     * Unassigns a collection of eZ UserGroups from an eZ User.
225
     *
226
     * @param User        $user
227
     * @param UserGroup[] $userGroups
228
     */
229 1
    protected function unassignUserFromUserGroups(User $user, array $userGroups)
230
    {
231 1
        $existingUserGroups = $this->userService->loadUserGroupsOfUser($user);
232 1
        foreach ($existingUserGroups as $existingUserGroup) {
233 1
            if (!array_key_exists($existingUserGroup->id, $userGroups)) {
234 1
                $this->userService->unAssignUserFromUserGroup($user, $existingUserGroup);
235 1
            }
236 1
        }
237 1
    }
238
}
239