Completed
Pull Request — 1.0 (#36)
by Valentin
46:37
created

UserGroupManager   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 21
c 1
b 0
f 1
lcom 1
cbo 9
dl 0
loc 171
ccs 65
cts 65
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setLogger() 0 4 1
A find() 0 8 2
B create() 0 28 3
B update() 0 30 5
B createOrUpdate() 0 15 5
A remove() 0 20 4
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\ContentService;
13
use eZ\Publish\API\Repository\ContentTypeService;
14
use eZ\Publish\API\Repository\Repository;
15
use eZ\Publish\API\Repository\UserService;
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\Exception\UserGroupNotFoundException;
21
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
22
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
23
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;
24
use eZ\Publish\API\Repository\Values\User\UserGroup;
25
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
26
27
/**
28
 * User Group manager.
29
 */
30
class UserGroupManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface
31
{
32
    /**
33
     * @var Repository
34
     */
35
    private $repository;
36
37
    /**
38
     * @var LoggerInterface
39
     */
40
    private $logger;
41
42
    /**
43
     * @var UserService
44
     */
45
    private $userService;
46
47
    /**
48
     * @var ContentService
49
     */
50
    private $contentService;
51
52
    /**
53
     * @var ContentTypeService
54
     */
55
    private $contentTypeService;
56
57
    /**
58
     * @param Repository $repository
59
     */
60 3
    public function __construct(Repository $repository)
61
    {
62 3
        $this->repository = $repository;
63 3
        $this->userService = $repository->getUserService();
64 3
        $this->contentService = $repository->getContentService();
65 3
        $this->contentTypeService = $repository->getContentTypeService();
66 3
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function setLogger(LoggerInterface $logger)
72
    {
73 4
        $this->logger = $logger;
74 4
    }
75
76
    /**
77
     * Shortcut to load a usergroup by id, without throwing an exception if it's not found.
78
     *
79
     * @param int $id
80
     *
81
     * @return UserGroup|false
82
     */
83 15
    public function find($id)
84
    {
85
        try {
86 15
            return $this->userService->loadUserGroup($id);
87 4
        } catch (NotFoundException $e) {
88 4
            return false;
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 14
    public function create(ObjectInterface $object)
96
    {
97 14
        if (!$object instanceof UserGroupObject) {
98 1
            return;
99
        }
100
101 13
        $parentUserGroup = $this->find($object->data['parent_id']);
102
103 13
        if (!$parentUserGroup) {
104 1
            throw new UserGroupNotFoundException(sprintf('Usergroup with parent_id "%s" not found.', $object->data['parent_id']));
105
        }
106
107
        // Instantiate usergroup
108 12
        $contentType = $this->contentTypeService->loadContentTypeByIdentifier($object->data['content_type_identifier']);
109 12
        $userGroupCreateStruct = $this->userService->newUserGroupCreateStruct(
110 12
            $object->data['main_language_code'],
111
            $contentType
112 12
        );
113
114
        // Populate usergroup fields
115 12
        $object->getMapper()->populateUserGroupCreateStruct($userGroupCreateStruct);
116
117
        // Create usergroup
118 12
        $userGroup = $this->userService->createUserGroup($userGroupCreateStruct, $parentUserGroup);
119 12
        $object->data['id'] = $userGroup->id;
120
121 12
        return $object;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 12
    public function update(ObjectInterface $object)
128 12
    {
129 6
        if (!$object instanceof UserGroupObject) {
130 1
            return;
131
        }
132
133 5
        if (!array_key_exists('id', $object->data)) {
134 1
            throw new UserGroupNotFoundException('Unable to update usergroup without an id.');
135
        }
136
137 4
        $userGroup = $this->find($object->data['id']);
138
139 4
        if (!$userGroup) {
140 1
            throw new UserGroupNotFoundException(sprintf('Usergroup with id "%s" not found.', $object->data['id']));
141
        }
142
143 3
        $userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct();
144 3
        $userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct();
145
146 3
        $object->getMapper()->populateUserGroupUpdateStruct($userGroupUpdateStruct);
147
148 3
        $this->userService->updateUserGroup($userGroup, $userGroupUpdateStruct);
149
150 3
        if ($userGroup->parentId !== $object->data['parent_id']) {
151 1
            $newParentGroup = $this->find($object->data['parent_id']);
152 1
            $this->userService->moveUserGroup($userGroup, $newParentGroup);
153 1
        }
154
155 3
        return $object;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 10
    public function createOrUpdate(ObjectInterface $object)
162
    {
163 10
        if (!$object instanceof UserGroupObject) {
164 1
            return;
165
        }
166
167 9
        if (isset($object->data['id'])) {
168 1
            $userGroup = $this->find($object->data['id']);
169 1
        }
170 9
        if (!isset($userGroup) || false === $userGroup) {
171 9
            return $this->create($object);
172
        } else {
173 1
            return $this->update($object);
174
        }
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 4
    public function remove(ObjectInterface $object)
181
    {
182 4
        if (!$object instanceof UserGroupObject) {
183 1
            return;
184
        }
185
186 3
        if (array_key_exists('id', $object->data)) {
187 2
            $userGroup = $this->find($object->data['id']);
188 2
        } else {
189 1
            throw new UserGroupNotFoundException(sprintf('Usergroup with id "%s" not found.', ''));
190
        }
191
192 2
        if (!$userGroup) {
193 1
            throw new UserGroupNotFoundException(sprintf('Usergroup with id "%s" not found.', $object->data['id']));
194
        }
195
196 1
        $this->userService->deleteUserGroup($userGroup);
197
198 1
        return true;
199
    }
200
}
201