Completed
Pull Request — 1.0 (#53)
by Harald
06:31
created

UserManager::find()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 20
Code Lines 11

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 20
loc 20
ccs 7
cts 7
cp 1
rs 8.8571
cc 6
eloc 11
nc 9
nop 2
crap 6
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 3
     */
59
    public function __construct(Repository $repository, UserGroupManager $userGroupManager)
60 3
    {
61 3
        $this->repository = $repository;
62 3
        $this->userService = $repository->getUserService();
63 3
        $this->userGroupManager = $userGroupManager;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68 3
     */
69
    public function setLogger(LoggerInterface $logger)
70 3
    {
71 3
        $this->logger = $logger;
72
    }
73
74
    /**
75
     * Finds user object by username.
76
     *
77
     * @param ValueObject $object
78
     * @param bool        $throwException
79
     *
80 10
     * @return User|false
81 4
     *
82 10
     * @throws NotFoundException
83 1
     */
84 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 9
            if (isset($object->data['username'])) {
88 9
                $user = $this->userService->loadUserByLogin($object->data['username']);
89 5
            }
90
        } catch (NotFoundException $notFoundException) {
91
            $exception = $notFoundException;
92 5
        }
93
94
        if (!isset($user)) {
95
            if (isset($exception) && $throwException) {
96
                throw $exception;
97
            }
98 5
99
            return false;
100 5
        }
101 1
102
        return $user;
103
    }
104 4
105 4
    /**
106 4
     * {@inheritdoc}
107 4
     */
108 4
    public function create(ObjectInterface $object)
109 4
    {
110
        if (!$object instanceof UserObject) {
111 4
            return;
112
        }
113 4
114 4
        $userCreateStruct = $this->userService->newUserCreateStruct(
115 4
            $object->data['username'],
116 4
            $object->data['email'],
117 4
            $object->data['password'],
118 4
            $object->data['main_language_code']
119 4
        );
120
121 4
        $object->getMapper()->getNewUserCreateStruct($userCreateStruct);
122 4
123
        $groups = [];
124 4
        foreach ($object->parents as $userGroup) {
125
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
126
            if ($userGroup instanceof UserGroupObject) {
127
                $groups[] = $this->userGroupManager->find($userGroup);
128
            }
129
        }
130 6
131
        $user = $this->userService->createUser($userCreateStruct, $groups);
132 6
        $object->data['id'] = $user->getUserId();
133 1
134
        return $object;
135
    }
136 5
137
    /**
138 5
     * {@inheritdoc}
139 1
     */
140
    public function update(ObjectInterface $object)
141
    {
142
        if (!$object instanceof UserObject) {
143 4
            return;
144 4
        }
145
146
        $user = $this->find($object, true);
147 4
148
        // Populate struct
149
        $userUpdateStruct = $this->userService->newUserUpdateStruct();
150 4
        $object->getMapper()->getNewUserUpdateStruct($userUpdateStruct);
151
152
        // Update user
153 4
        $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 4
        // Assign user to usergroups
156
        $ezUserGroups = $this->assignUserToUserGroups($ezuser, $object->parents);
157
158
        // Unassign user from usergroups
159
        $this->unassignUserFromUserGroups($ezuser, $ezUserGroups);
160
161 6
        return $object;
162
    }
163 6
164 1
    /**
165
     * {@inheritdoc}
166
     */
167 5 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 3
    {
169
        if (!$object instanceof UserObject) {
170 3
            return;
171
        }
172
173
        if (!$this->find($object)) {
174
            return $this->create($object);
175
        } else {
176
            return $this->update($object);
177 3
        }
178
    }
179 3
180 1
    /**
181
     * {@inheritdoc}
182
     */
183 2
    public function remove(ObjectInterface $object)
184
    {
185 2
        if (!$object instanceof UserObject) {
186 1
            return;
187 1
        }
188
189 2
        $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 4
     *
201
     * @param User              $user
202 4
     * @param UserGroupObject[] $userGroupObjects
203 4
     *
204 4
     * @return UserGroup[]
205 4
     */
206 4
    protected function assignUserToUserGroups(User $user, array $userGroupObjects)
207 4
    {
208 4
        $ezUserGroups = [];
209 4
        foreach ($userGroupObjects as $userGroup) {
210 4
            $userGroup = $this->userGroupManager->createOrUpdate($userGroup);
211 4
            if ($userGroup instanceof UserGroupObject) {
212 4
                $ezUserGroup = $this->userGroupManager->find($userGroup);
213
                if ($ezUserGroup) {
214 4
                    $ezUserGroups[$ezUserGroup->id] = $ezUserGroup;
215
                    $this->userService->assignUserToUserGroup($user, $ezUserGroup);
216
                }
217
            }
218
        }
219
220
        return $ezUserGroups;
221
    }
222
223 4
    /**
224
     * Unassigns a collection of eZ UserGroups from an eZ User.
225 4
     *
226 4
     * @param User        $user
227 4
     * @param UserGroup[] $userGroups
228 4
     */
229 4
    protected function unassignUserFromUserGroups(User $user, array $userGroups)
230 4
    {
231 4
        $existingUserGroups = $this->userService->loadUserGroupsOfUser($user);
232
        foreach ($existingUserGroups as $existingUserGroup) {
233
            if (!array_key_exists($existingUserGroup->id, $userGroups)) {
234
                $this->userService->unAssignUserFromUserGroup($user, $existingUserGroup);
235
            }
236
        }
237
    }
238
}
239