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\Data\ValueObject; |
20
|
|
|
use Transfer\EzPlatform\Exception\ObjectNotFoundException; |
21
|
|
|
use Transfer\EzPlatform\Exception\UnsupportedObjectOperationException; |
22
|
|
|
use Transfer\EzPlatform\Repository\Values\UserGroupObject; |
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\Values\User\UserGroup; |
28
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* User Group manager. |
32
|
|
|
* |
33
|
|
|
* @internal |
34
|
|
|
*/ |
35
|
|
|
class UserGroupManager 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 ContentService |
54
|
|
|
*/ |
55
|
|
|
private $contentService; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var ContentTypeService |
59
|
|
|
*/ |
60
|
|
|
private $contentTypeService; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Repository $repository |
64
|
|
|
*/ |
65
|
6 |
|
public function __construct(Repository $repository) |
66
|
|
|
{ |
67
|
6 |
|
$this->repository = $repository; |
68
|
6 |
|
$this->userService = $repository->getUserService(); |
69
|
6 |
|
$this->contentService = $repository->getContentService(); |
70
|
6 |
|
$this->contentTypeService = $repository->getContentTypeService(); |
71
|
6 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
22 |
|
public function setLogger(LoggerInterface $logger) |
77
|
|
|
{ |
78
|
22 |
|
$this->logger = $logger; |
79
|
22 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
16 |
|
public function find(ValueObject $object, $throwException = false) |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
16 |
|
if (isset($object->data['remote_id'])) { |
88
|
4 |
|
$contentObject = $this->contentService->loadContentByRemoteId($object->data['remote_id']); |
89
|
2 |
|
$userGroup = $this->userService->loadUserGroup($contentObject->contentInfo->id); |
90
|
16 |
|
} elseif ($object->getProperty('id')) { |
91
|
16 |
|
$userGroup = $this->userService->loadUserGroup($object->getProperty('id')); |
92
|
16 |
|
} |
93
|
16 |
|
} catch (NotFoundException $notFoundException) { |
94
|
|
|
// We'll throw our own exception later instead. |
95
|
|
|
} |
96
|
|
|
|
97
|
16 |
|
if (!isset($userGroup)) { |
98
|
16 |
|
throw new ObjectNotFoundException(UserGroup::class, array('remote_id', 'id')); |
99
|
|
|
} |
100
|
|
|
|
101
|
16 |
|
return $userGroup; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Shortcut to get UserGroup by id, mainly to get parent by Id. |
106
|
|
|
* |
107
|
|
|
* @param int $id |
108
|
|
|
* @param bool $throwException |
109
|
|
|
* |
110
|
|
|
* @return UserGroup|false |
111
|
|
|
* |
112
|
|
|
* @throws NotFoundException |
113
|
|
|
*/ |
114
|
16 |
|
public function findById($id, $throwException = false) |
115
|
|
|
{ |
116
|
16 |
|
return $this->find(new ValueObject([], ['id' => $id]), $throwException); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
16 |
|
public function create(ObjectInterface $object) |
123
|
16 |
|
{ |
124
|
16 |
|
if (!$object instanceof UserGroupObject) { |
125
|
1 |
|
throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object)); |
126
|
|
|
} |
127
|
|
|
|
128
|
16 |
|
$parentUserGroup = $this->findById($object->data['parent_id'], true); |
129
|
|
|
|
130
|
|
|
// Instantiate usergroup |
131
|
16 |
|
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($object->data['content_type_identifier']); |
132
|
16 |
|
$userGroupCreateStruct = $this->userService->newUserGroupCreateStruct( |
133
|
16 |
|
$object->data['main_language_code'], |
134
|
|
|
$contentType |
135
|
16 |
|
); |
136
|
|
|
|
137
|
|
|
// Populate usergroup fields |
138
|
16 |
|
$object->getMapper()->populateUserGroupCreateStruct($userGroupCreateStruct); |
139
|
|
|
|
140
|
|
|
// Create usergroup |
141
|
16 |
|
$userGroup = $this->userService->createUserGroup($userGroupCreateStruct, $parentUserGroup); |
142
|
|
|
|
143
|
16 |
|
$object->getMapper()->userGroupToObject($userGroup); |
144
|
|
|
|
145
|
16 |
|
return $object; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
4 |
|
public function update(ObjectInterface $object) |
152
|
|
|
{ |
153
|
4 |
|
if (!$object instanceof UserGroupObject) { |
154
|
1 |
|
throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object)); |
155
|
|
|
} |
156
|
|
|
|
157
|
3 |
|
$userGroup = $this->find($object, true); |
158
|
|
|
|
159
|
3 |
|
$userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct(); |
160
|
3 |
|
$userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct(); |
161
|
|
|
|
162
|
3 |
|
$object->getMapper()->populateUserGroupUpdateStruct($userGroupUpdateStruct); |
163
|
|
|
|
164
|
3 |
|
$userGroup = $this->userService->updateUserGroup($userGroup, $userGroupUpdateStruct); |
165
|
|
|
|
166
|
3 |
|
if ($userGroup->parentId !== $object->data['parent_id']) { |
167
|
1 |
|
$newParentGroup = $this->findById($object->data['parent_id'], true); |
168
|
1 |
|
$this->userService->moveUserGroup($userGroup, $newParentGroup); |
169
|
1 |
|
$userGroup = $this->find($object, true); |
170
|
1 |
|
} |
171
|
|
|
|
172
|
3 |
|
$object->getMapper()->userGroupToObject($userGroup); |
173
|
|
|
|
174
|
3 |
|
return $object; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
16 |
|
public function createOrUpdate(ObjectInterface $object) |
181
|
|
|
{ |
182
|
16 |
|
if (!$object instanceof UserGroupObject) { |
183
|
1 |
|
throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
try { |
187
|
16 |
|
$this->find($object); |
188
|
|
|
|
189
|
3 |
|
return $this->update($object); |
190
|
16 |
|
} catch (NotFoundException $notFound) { |
191
|
16 |
|
return $this->create($object); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
3 |
View Code Duplication |
public function remove(ObjectInterface $object) |
|
|
|
|
199
|
|
|
{ |
200
|
3 |
|
if (!$object instanceof UserGroupObject) { |
201
|
1 |
|
throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
try { |
205
|
2 |
|
$userGroup = $this->find($object); |
206
|
1 |
|
$this->userService->deleteUserGroup($userGroup); |
207
|
|
|
|
208
|
1 |
|
return true; |
209
|
1 |
|
} catch (NotFoundException $notFound) { |
210
|
1 |
|
return false; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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.