|
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\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 Repository |
|
38
|
|
|
*/ |
|
39
|
|
|
private $repository; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var LoggerInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $logger; |
|
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 Repository $repository |
|
63
|
|
|
*/ |
|
64
|
5 |
|
public function __construct(Repository $repository) |
|
65
|
|
|
{ |
|
66
|
5 |
|
$this->repository = $repository; |
|
67
|
5 |
|
$this->userService = $repository->getUserService(); |
|
68
|
5 |
|
$this->contentService = $repository->getContentService(); |
|
69
|
5 |
|
$this->contentTypeService = $repository->getContentTypeService(); |
|
70
|
5 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
4 |
|
public function setLogger(LoggerInterface $logger) |
|
76
|
|
|
{ |
|
77
|
4 |
|
$this->logger = $logger; |
|
78
|
4 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Load a UserGroup by remote_id or id. |
|
82
|
|
|
* |
|
83
|
|
|
* @param ValueObject $object |
|
84
|
|
|
* @param bool $throwException |
|
85
|
|
|
* |
|
86
|
|
|
* @return UserGroup|false |
|
87
|
|
|
* |
|
88
|
|
|
* @throws NotFoundException |
|
89
|
|
|
*/ |
|
90
|
12 |
|
public function find(ValueObject $object, $throwException = false) |
|
91
|
1 |
|
{ |
|
92
|
|
|
try { |
|
93
|
12 |
|
if (isset($object->data['remote_id'])) { |
|
94
|
|
|
$contentObject = $this->contentService->loadContentByRemoteId($object->data['remote_id']); |
|
95
|
|
|
$userGroup = $this->userService->loadUserGroup($contentObject->contentInfo->id); |
|
96
|
12 |
|
} elseif ($object->getProperty('id')) { |
|
97
|
12 |
|
$userGroup = $this->userService->loadUserGroup($object->getProperty('id')); |
|
98
|
12 |
|
} |
|
99
|
12 |
|
} catch (NotFoundException $notFoundException) { |
|
100
|
|
|
$exception = $notFoundException; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
12 |
|
if (!isset($userGroup)) { |
|
104
|
12 |
|
if (isset($exception) && $throwException) { |
|
105
|
|
|
throw $exception; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
12 |
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
12 |
|
return $userGroup; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Shortcut to get UserGroup by id, mainly to get parent by Id. |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $id |
|
118
|
|
|
* @param bool $throwException |
|
119
|
|
|
* |
|
120
|
|
|
* @return UserGroup|false |
|
121
|
|
|
* |
|
122
|
|
|
* @throws NotFoundException |
|
123
|
|
|
*/ |
|
124
|
12 |
|
public function findById($id, $throwException = false) |
|
125
|
|
|
{ |
|
126
|
12 |
|
return $this->find(new ValueObject([], ['id' => $id]), $throwException); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
12 |
|
public function create(ObjectInterface $object) |
|
133
|
|
|
{ |
|
134
|
12 |
|
if (!$object instanceof UserGroupObject) { |
|
135
|
1 |
|
throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
12 |
|
$parentUserGroup = $this->findById($object->data['parent_id'], true); |
|
139
|
|
|
|
|
140
|
|
|
// Instantiate usergroup |
|
141
|
12 |
|
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($object->data['content_type_identifier']); |
|
142
|
12 |
|
$userGroupCreateStruct = $this->userService->newUserGroupCreateStruct( |
|
143
|
12 |
|
$object->data['main_language_code'], |
|
144
|
|
|
$contentType |
|
145
|
12 |
|
); |
|
146
|
|
|
|
|
147
|
|
|
// Populate usergroup fields |
|
148
|
12 |
|
$object->getMapper()->populateUserGroupCreateStruct($userGroupCreateStruct); |
|
149
|
|
|
|
|
150
|
|
|
// Create usergroup |
|
151
|
12 |
|
$userGroup = $this->userService->createUserGroup($userGroupCreateStruct, $parentUserGroup); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
12 |
|
$object->getMapper()->userGroupToObject($userGroup); |
|
154
|
|
|
|
|
155
|
12 |
|
return $object; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
*/ |
|
161
|
3 |
|
public function update(ObjectInterface $object) |
|
162
|
|
|
{ |
|
163
|
3 |
|
if (!$object instanceof UserGroupObject) { |
|
164
|
1 |
|
throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object)); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
2 |
|
$userGroup = $this->find($object, true); |
|
168
|
|
|
|
|
169
|
2 |
|
$userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct(); |
|
170
|
2 |
|
$userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct(); |
|
171
|
|
|
|
|
172
|
2 |
|
$object->getMapper()->populateUserGroupUpdateStruct($userGroupUpdateStruct); |
|
173
|
|
|
|
|
174
|
2 |
|
$userGroup = $this->userService->updateUserGroup($userGroup, $userGroupUpdateStruct); |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
2 |
|
if ($userGroup->parentId !== $object->data['parent_id']) { |
|
177
|
1 |
|
$newParentGroup = $this->findById($object->data['parent_id'], true); |
|
178
|
1 |
|
$this->userService->moveUserGroup($userGroup, $newParentGroup); |
|
|
|
|
|
|
179
|
1 |
|
} |
|
180
|
|
|
|
|
181
|
2 |
|
$object->getMapper()->userGroupToObject($userGroup); |
|
182
|
|
|
|
|
183
|
2 |
|
return $object; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* {@inheritdoc} |
|
188
|
|
|
*/ |
|
189
|
12 |
|
public function createOrUpdate(ObjectInterface $object) |
|
190
|
|
|
{ |
|
191
|
12 |
|
if (!$object instanceof UserGroupObject) { |
|
192
|
1 |
|
throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object)); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
12 |
|
if (!$this->find($object)) { |
|
196
|
12 |
|
return $this->create($object); |
|
197
|
|
|
} else { |
|
198
|
2 |
|
return $this->update($object); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* {@inheritdoc} |
|
204
|
|
|
*/ |
|
205
|
2 |
View Code Duplication |
public function remove(ObjectInterface $object) |
|
|
|
|
|
|
206
|
|
|
{ |
|
207
|
2 |
|
if (!$object instanceof UserGroupObject) { |
|
208
|
1 |
|
throw new UnsupportedObjectOperationException(UserGroupObject::class, get_class($object)); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
1 |
|
$userGroup = $this->find($object, true); |
|
212
|
|
|
|
|
213
|
1 |
|
$this->userService->deleteUserGroup($userGroup); |
|
|
|
|
|
|
214
|
|
|
|
|
215
|
1 |
|
return true; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
This check looks for type mismatches where the missing type is
false. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTimeobject or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalsebefore passing on the value to another function or method that may not be able to handle afalse.