UserGroupManager::create()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 2
eloc 13
c 3
b 0
f 1
nc 2
nop 1
dl 0
loc 27
ccs 12
cts 12
cp 1
crap 2
rs 8.8571
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\UserService;
15
use Psr\Log\LoggerAwareInterface;
16
use Psr\Log\LoggerInterface;
17
use Transfer\Data\ObjectInterface;
18
use Transfer\Data\ValueObject;
19
use Transfer\EzPlatform\Exception\ObjectNotFoundException;
20
use Transfer\EzPlatform\Exception\UnsupportedObjectOperationException;
21
use Transfer\EzPlatform\Repository\Values\UserGroupObject;
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\Values\User\UserGroup;
27
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
28
29
/**
30
 * User Group manager.
31
 *
32
 * @internal
33
 */
34
class UserGroupManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface
35
{
36
    /**
37
     * @var LoggerInterface
38
     */
39
    private $logger;
40
41
    /**
42
     * @var array
43
     */
44
    private $options;
45
46
    /**
47
     * @var UserService
48
     */
49
    private $userService;
50
51
    /**
52
     * @var ContentService
53
     */
54
    private $contentService;
55
56
    /**
57
     * @var ContentTypeService
58
     */
59
    private $contentTypeService;
60
61
    /**
62
     * @param array              $options
63
     * @param UserService        $userService
64
     * @param ContentService     $contentService
65
     * @param ContentTypeService $contentTypeService
66 9
     */
67
    public function __construct(array $options, UserService $userService, ContentService $contentService, ContentTypeService $contentTypeService)
68 9
    {
69 9
        $this->options = $options;
70 9
        $this->userService = $userService;
71 9
        $this->contentService = $contentService;
72 9
        $this->contentTypeService = $contentTypeService;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77 27
     */
78
    public function setLogger(LoggerInterface $logger)
79 27
    {
80 27
        $this->logger = $logger;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85 20
     */
86
    public function find(ValueObject $object, $throwException = false)
87
    {
88 20
        try {
89 5
            if (isset($object->data['remote_id'])) {
90 2
                $contentObject = $this->contentService->loadContentByRemoteId($object->data['remote_id']);
91 20
                $userGroup = $this->userService->loadUserGroup($contentObject->contentInfo->id);
92 20
            } elseif ($object->getProperty('id')) {
93 20
                $userGroup = $this->userService->loadUserGroup($object->getProperty('id'));
94 20
            }
95
        } catch (NotFoundException $notFoundException) {
96
            // We'll throw our own exception later instead.
97
        }
98 20
99 20
        if (!isset($userGroup)) {
100
            throw new ObjectNotFoundException(UserGroup::class, array('remote_id', 'id'));
101
        }
102 20
103
        return $userGroup;
104
    }
105
106
    /**
107
     * Shortcut to get UserGroup by id, mainly to get parent by Id.
108
     *
109
     * @param int  $id
110
     * @param bool $throwException
111
     *
112
     * @return UserGroup|false
113
     *
114
     * @throws NotFoundException
115 20
     */
116 20
    public function findById($id, $throwException = false)
117 20
    {
118 20
        return $this->find(new ValueObject([], ['id' => $id]), $throwException);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123 20
     */
124
    public function create(ObjectInterface $object)
125 20
    {
126 1
        if (!$object instanceof UserGroupObject) {
127
            throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object));
128
        }
129 20
130
        $this->ensureDefaults($object);
131 20
132
        $parentUserGroup = $this->findById($object->data['parent_id'], true);
133
134 20
        // Instantiate usergroup
135 20
        $contentType = $this->contentTypeService->loadContentTypeByIdentifier($object->data['content_type_identifier']);
136 20
        $userGroupCreateStruct = $this->userService->newUserGroupCreateStruct(
137
            $object->data['main_language_code'],
138 20
            $contentType
139
        );
140
141 20
        // Populate usergroup fields
142
        $object->getMapper()->mapObjectToCreateStruct($userGroupCreateStruct);
143
144 20
        // Create usergroup
145
        $userGroup = $this->userService->createUserGroup($userGroupCreateStruct, $parentUserGroup);
146 20
147
        $object->getMapper()->userGroupToObject($userGroup);
148 20
149
        return $object;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154 4
     */
155
    public function update(ObjectInterface $object)
156 4
    {
157 1
        if (!$object instanceof UserGroupObject) {
158
            throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object));
159
        }
160 3
161
        $userGroup = $this->find($object, true);
162 3
163
        $this->ensureDefaults($object);
164 3
165 3
        $userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct();
166
        $userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct();
167 3
168
        $object->getMapper()->mapObjectToUpdateStruct($userGroupUpdateStruct);
169 3
170
        $userGroup = $this->userService->updateUserGroup($userGroup, $userGroupUpdateStruct);
171 3
172 1
        if ($userGroup->parentId !== $object->data['parent_id']) {
173 1
            $newParentGroup = $this->findById($object->data['parent_id'], true);
174 1
            $this->userService->moveUserGroup($userGroup, $newParentGroup);
175 1
            $userGroup = $this->find($object, true);
176
        }
177 3
178
        $object->getMapper()->userGroupToObject($userGroup);
179 3
180
        return $object;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185 20
     */
186
    public function createOrUpdate(ObjectInterface $object)
187 20
    {
188 1
        if (!$object instanceof UserGroupObject) {
189
            throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object));
190
        }
191
192 20
        try {
193
            $this->find($object);
194 3
195 20
            return $this->update($object);
196 20
        } catch (NotFoundException $notFound) {
197
            return $this->create($object);
198
        }
199
    }
200
201
    /**
202
     * {@inheritdoc}
203 3
     */
204
    public function remove(ObjectInterface $object)
205 3
    {
206 1
        if (!$object instanceof UserGroupObject) {
207
            throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object));
208
        }
209
210 2
        try {
211 1
            $userGroup = $this->find($object);
212
            $this->userService->deleteUserGroup($userGroup);
213 1
214 1
            return true;
215 1
        } catch (NotFoundException $notFound) {
216
            return false;
217
        }
218
    }
219
220
    /**
221
     * @param UserGroupObject $userObject
222 20
     */
223
    private function ensureDefaults(UserGroupObject $userObject)
224 20
    {
225
        $defaultData = ['main_language_code'];
226 20
227 20
        foreach ($defaultData as $defaultOption) {
228 1
            if (!isset($userObject->data[$defaultOption])) {
229 1
                $userObject->data[$defaultOption] = $this->options[$defaultOption];
230 20
            }
231 20
        }
232
    }
233
}
234