Completed
Pull Request — 1.0 (#53)
by Harald
07:01
created

UserManager   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 206
Duplicated Lines 13.59 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 28
c 7
b 0
f 3
lcom 1
cbo 10
dl 28
loc 206
ccs 85
cts 85
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setLogger() 0 4 1
B find() 14 14 5
B create() 0 28 4
B update() 0 25 3
A createOrUpdate() 0 12 3
A remove() 14 14 3
B assignUserToUserGroups() 0 20 5
A unassignUserFromUserGroups() 0 9 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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