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