Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Core/REST/Server/Controller/ObjectState.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Server\Controller;
8
9 use eZ\Publish\API\Repository\Values\Content\Language;
10 use eZ\Publish\Core\REST\Common\Message;
11 use eZ\Publish\Core\REST\Server\Values;
12 use eZ\Publish\Core\REST\Common\Values\RestObjectState;
13 use eZ\Publish\Core\REST\Server\Controller as RestController;
14 use eZ\Publish\API\Repository\ObjectStateService;
15 use eZ\Publish\API\Repository\ContentService;
16 use eZ\Publish\Core\REST\Common\Values\ContentObjectStates;
17 use eZ\Publish\API\Repository\Exceptions\NotFoundException;
18 use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
19 use eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException;
20 use Symfony\Component\HttpFoundation\Request;
21
25 class ObjectState extends RestController
26 {
32 protected $objectStateService;
33
39 protected $contentService;
40
47 public function __construct(ObjectStateService $objectStateService, ContentService $contentService)
48 {
49 $this->objectStateService = $objectStateService;
50 $this->contentService = $contentService;
51 }
52
60 public function createObjectStateGroup(Request $request)
61 {
62 try {
63 $createdStateGroup = $this->objectStateService->createObjectStateGroup(
64 $this->inputDispatcher->parse(
65 new Message(
66 ['Content-Type' => $request->headers->get('Content-Type')],
67 $request->getContent()
68 )
69 )
70 );
71 } catch (InvalidArgumentException $e) {
72 throw new ForbiddenException($e->getMessage());
73 }
74
75 return new Values\CreatedObjectStateGroup(
76 [
77 'objectStateGroup' => $createdStateGroup,
78 ]
79 );
80 }
81
91 public function createObjectState($objectStateGroupId, Request $request)
92 {
93 $objectStateGroup = $this->objectStateService->loadObjectStateGroup($objectStateGroupId);
94
95 try {
96 $createdObjectState = $this->objectStateService->createObjectState(
97 $objectStateGroup,
98 $this->inputDispatcher->parse(
99 new Message(
100 ['Content-Type' => $request->headers->get('Content-Type')],
101 $request->getContent()
102 )
103 )
104 );
105 } catch (InvalidArgumentException $e) {
106 throw new ForbiddenException($e->getMessage());
107 }
108
109 return new Values\CreatedObjectState(
110 [
111 'objectState' => new RestObjectState(
112 $createdObjectState,
113 $objectStateGroup->id
114 ),
115 ]
116 );
117 }
118
126 public function loadObjectStateGroup($objectStateGroupId)
127 {
128 return $this->objectStateService->loadObjectStateGroup($objectStateGroupId, Language::ALL);
129 }
130
139 public function loadObjectState($objectStateGroupId, $objectStateId)
140 {
141 return new RestObjectState(
142 $this->objectStateService->loadObjectState($objectStateId, Language::ALL),
143 $objectStateGroupId
144 );
145 }
146
152 public function loadObjectStateGroups()
153 {
154 return new Values\ObjectStateGroupList(
155 $this->objectStateService->loadObjectStateGroups(0, -1, Language::ALL)
156 );
157 }
158
166 public function loadObjectStates($objectStateGroupId)
167 {
168 $objectStateGroup = $this->objectStateService->loadObjectStateGroup($objectStateGroupId);
169
170 return new Values\ObjectStateList(
171 $this->objectStateService->loadObjectStates($objectStateGroup, Language::ALL),
172 $objectStateGroup->id
173 );
174 }
175
183 public function deleteObjectStateGroup($objectStateGroupId)
184 {
185 $this->objectStateService->deleteObjectStateGroup(
186 $this->objectStateService->loadObjectStateGroup($objectStateGroupId)
187 );
188
189 return new Values\NoContent();
190 }
191
199 public function deleteObjectState($objectStateId)
200 {
201 $this->objectStateService->deleteObjectState(
202 $this->objectStateService->loadObjectState($objectStateId)
203 );
204
205 return new Values\NoContent();
206 }
207
217 public function updateObjectStateGroup($objectStateGroupId, Request $request)
218 {
219 $updateStruct = $this->inputDispatcher->parse(
220 new Message(
221 ['Content-Type' => $request->headers->get('Content-Type')],
222 $request->getContent()
223 )
224 );
225
226 $objectStateGroup = $this->objectStateService->loadObjectStateGroup($objectStateGroupId);
227
228 try {
229 $updatedStateGroup = $this->objectStateService->updateObjectStateGroup($objectStateGroup, $updateStruct);
230
231 return $updatedStateGroup;
232 } catch (InvalidArgumentException $e) {
233 throw new ForbiddenException($e->getMessage());
234 }
235 }
236
247 public function updateObjectState($objectStateGroupId, $objectStateId, Request $request)
248 {
249 $updateStruct = $this->inputDispatcher->parse(
250 new Message(
251 ['Content-Type' => $request->headers->get('Content-Type')],
252 $request->getContent()
253 )
254 );
255
256 $objectState = $this->objectStateService->loadObjectState($objectStateId);
257
258 try {
259 $updatedObjectState = $this->objectStateService->updateObjectState($objectState, $updateStruct);
260
261 return new RestObjectState($updatedObjectState, $objectStateGroupId);
262 } catch (InvalidArgumentException $e) {
263 throw new ForbiddenException($e->getMessage());
264 }
265 }
266
274 public function getObjectStatesForContent($contentId)
275 {
276 $groups = $this->objectStateService->loadObjectStateGroups();
277 $contentInfo = $this->contentService->loadContentInfo($contentId);
278
279 $contentObjectStates = [];
280
281 foreach ($groups as $group) {
282 try {
283 $state = $this->objectStateService->getContentState($contentInfo, $group);
284 $contentObjectStates[] = new RestObjectState($state, $group->id);
285 } catch (NotFoundException $e) {
286 // Do nothing
287 }
288 }
289
290 return new ContentObjectStates($contentObjectStates);
291 }
292
303 public function setObjectStatesForContent($contentId, Request $request)
304 {
305 $newObjectStates = $this->inputDispatcher->parse(
306 new Message(
307 ['Content-Type' => $request->headers->get('Content-Type')],
308 $request->getContent()
309 )
310 );
311
312 $countByGroups = [];
313 foreach ($newObjectStates as $newObjectState) {
314 $groupId = (int)$newObjectState->groupId;
315 if (array_key_exists($groupId, $countByGroups)) {
316 ++$countByGroups[$groupId];
317 } else {
318 $countByGroups[$groupId] = 1;
319 }
320 }
321
322 foreach ($countByGroups as $groupId => $count) {
323 if ($count > 1) {
324 throw new ForbiddenException("Multiple object states provided for group with ID $groupId");
325 }
326 }
327
328 $contentInfo = $this->contentService->loadContentInfo($contentId);
329
330 $contentObjectStates = [];
331 foreach ($newObjectStates as $newObjectState) {
332 $objectStateGroup = $this->objectStateService->loadObjectStateGroup($newObjectState->groupId);
333 $this->objectStateService->setContentState($contentInfo, $objectStateGroup, $newObjectState->objectState);
334 $contentObjectStates[(int)$objectStateGroup->id] = $newObjectState;
335 }
336
337 return new ContentObjectStates($contentObjectStates);
338 }
339 }
eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
This exception is thrown if a service method is called with an illegal or non appropriate value.
Definition: API/Repository/Exceptions/InvalidArgumentException.php:13
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\API\Repository\Values\Content\Language
Definition: API/Repository/Values/Content/Language.php:20
eZ\Publish\Core\Base\Exceptions\ForbiddenException
Forbidden Exception implementation.
Definition: Core/Base/Exceptions/ForbiddenException.php:17
eZ\Publish\Core\REST\Common\Message
Simple response struct.
Definition: Core/REST/Common/Message.php:13
eZ\Publish\Core\REST\Common\Values\ContentObjectStates
ContentObjectStates view model.
Definition: Common/Values/ContentObjectStates.php:15
eZ\Publish\Core\REST\Common\Values\RestObjectState
This class wraps the object state with added groupId property.
Definition: Common/Values/RestObjectState.php:16
eZ\Publish\Core\REST\Server\Controller\ObjectState
ObjectState controller.
Definition: Core/REST/Server/Controller/ObjectState.php:26
eZ\Publish\Core\REST\Server\Controller\ObjectState\createObjectStateGroup
createObjectStateGroup(Request $request)
Creates a new object state group.
Definition: Core/REST/Server/Controller/ObjectState.php:60
eZ\Publish\Core\REST\Server\Controller\ObjectState\loadObjectState
loadObjectState($objectStateGroupId, $objectStateId)
Loads an object state.
Definition: Core/REST/Server/Controller/ObjectState.php:139
eZ\Publish\Core\REST\Server\Controller\ObjectState\loadObjectStateGroup
loadObjectStateGroup($objectStateGroupId)
Loads an object state group.
Definition: Core/REST/Server/Controller/ObjectState.php:126
eZ\Publish\Core\REST\Server\Controller\ObjectState\deleteObjectState
deleteObjectState($objectStateId)
The given object state is deleted.
Definition: Core/REST/Server/Controller/ObjectState.php:199
eZ\Publish\Core\REST\Server\Controller\ObjectState\createObjectState
createObjectState($objectStateGroupId, Request $request)
Creates a new object state.
Definition: Core/REST/Server/Controller/ObjectState.php:91
eZ\Publish\Core\REST\Server\Controller\ObjectState\deleteObjectStateGroup
deleteObjectStateGroup($objectStateGroupId)
The given object state group including the object states is deleted.
Definition: Core/REST/Server/Controller/ObjectState.php:183
eZ\Publish\Core\REST\Server\Controller\ObjectState\setObjectStatesForContent
setObjectStatesForContent($contentId, Request $request)
Updates object states of content An object state in the input overrides the state of the object state...
Definition: Core/REST/Server/Controller/ObjectState.php:303
eZ\Publish\Core\REST\Server\Controller\ObjectState\$objectStateService
$objectStateService
Definition: Core/REST/Server/Controller/ObjectState.php:32
eZ\Publish\Core\REST\Server\Controller\ObjectState\updateObjectState
updateObjectState($objectStateGroupId, $objectStateId, Request $request)
Updates an object state.
Definition: Core/REST/Server/Controller/ObjectState.php:247
eZ\Publish\Core\REST\Server\Controller\ObjectState\loadObjectStateGroups
loadObjectStateGroups()
Returns a list of all object state groups.
Definition: Core/REST/Server/Controller/ObjectState.php:152
eZ\Publish\Core\REST\Server\Controller\ObjectState\updateObjectStateGroup
updateObjectStateGroup($objectStateGroupId, Request $request)
Updates an object state group.
Definition: Core/REST/Server/Controller/ObjectState.php:217
eZ\Publish\Core\REST\Server\Controller\ObjectState\$contentService
$contentService
Definition: Core/REST/Server/Controller/ObjectState.php:39
eZ\Publish\Core\REST\Server\Controller\ObjectState\loadObjectStates
loadObjectStates($objectStateGroupId)
Returns a list of all object states of the given group.
Definition: Core/REST/Server/Controller/ObjectState.php:166
eZ\Publish\Core\REST\Server\Controller\ObjectState\getObjectStatesForContent
getObjectStatesForContent($contentId)
Returns the object states of content.
Definition: Core/REST/Server/Controller/ObjectState.php:274
eZ\Publish\Core\REST\Server\Controller\ObjectState\__construct
__construct(ObjectStateService $objectStateService, ContentService $contentService)
Construct controller.
Definition: Core/REST/Server/Controller/ObjectState.php:47
eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
Exception thrown if the request is forbidden.
Definition: Core/REST/Server/Exceptions/ForbiddenException.php:15
eZ\Publish\Core\REST\Server\Values\CreatedObjectStateGroup
Struct representing a freshly created object state group.
Definition: Values/CreatedObjectStateGroup.php:15
eZ\Publish\Core\REST\Server\Values\CreatedObjectState
Struct representing a freshly created object state.
Definition: Values/CreatedObjectState.php:15
eZ\Publish\Core\REST\Server\Values\NoContent
Definition: Values/NoContent.php:12
eZ\Publish\Core\REST\Server\Values\ObjectStateGroupList
ObjectStateGroup list view model.
Definition: Server/Values/ObjectStateGroupList.php:15
eZ\Publish\Core\REST\Server\Values\ObjectStateList
ObjectState list view model.
Definition: Server/Values/ObjectStateList.php:15
eZ\Publish\API\Repository\ContentService
Definition: API/Repository/ContentService.php:27
eZ\Publish\API\Repository\ObjectStateService
Definition: API/Repository/ObjectStateService.php:23
eZ\Publish\Core\REST\Server\Controller
Definition: BinaryContent.php:7
eZ\Publish\Core\REST\Server\Values
Definition: Core/REST/Server/Values/BookmarkList.php:9
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.