-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathContentCreateController.php
More file actions
166 lines (155 loc) · 7.75 KB
/
Copy pathContentCreateController.php
File metadata and controls
166 lines (155 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\Rest\Server\Controller\Content;
use ApiPlatform\Metadata\Post;
use ApiPlatform\OpenApi\Factory\OpenApiFactory;
use ApiPlatform\OpenApi\Model;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException;
use Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException;
use Ibexa\Rest\Message;
use Ibexa\Rest\Server\Controller as RestController;
use Ibexa\Rest\Server\Exceptions\BadRequestException;
use Ibexa\Rest\Server\Exceptions\ContentFieldValidationException as RESTContentFieldValidationException;
use Ibexa\Rest\Server\Values;
use Ibexa\Rest\Server\Values\CreatedContent;
use Ibexa\Rest\Server\Values\RestContentCreateStruct;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
#[Post(
uriTemplate: '/content/objects',
extraProperties: [OpenApiFactory::OVERRIDE_OPENAPI_RESPONSES => false],
openapi: new Model\Operation(
summary: 'Create content item',
description: 'Creates a draft assigned to the authenticated user. If a different user ID is given in the input, the draft is assigned to the given user but this action requires special permissions for the authenticated user (this is useful for content staging where the transfer process does not have to authenticate with the user who created the content item in the source server). The user needs to publish the content item if it should be visible.',
tags: [
'Objects',
],
requestBody: new Model\RequestBody(
description: 'The ContentCreate schema encoded in XML or JSON format.',
content: new \ArrayObject([
'application/vnd.ibexa.api.ContentCreate+xml' => [
'schema' => [
'$ref' => '#/components/schemas/ContentCreate',
],
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/objects/POST/ContentCreate.xml.example',
],
'application/vnd.ibexa.api.ContentCreate+json' => [
'schema' => [
'$ref' => '#/components/schemas/ContentCreateWrapper',
],
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/objects/POST/ContentCreate.json.example',
],
]),
),
responses: [
Response::HTTP_CREATED => [
'description' => 'Content - If set, all information for the content item including the embedded current version is returned in XML or JSON format. ContentInfo - If set, all information for the content item (excluding the current version) is returned in XML or JSON format.',
'content' => [
'application/vnd.ibexa.api.Content+xml' => [
'schema' => [
'$ref' => '#/components/schemas/Content',
],
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/objects/content_id/GET/Content.xml.example',
],
'application/vnd.ibexa.api.Content+json' => [
'schema' => [
'$ref' => '#/components/schemas/ContentWrapper',
],
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/objects/content_id/GET/Content.json.example',
],
'application/vnd.ibexa.api.ContentInfo+xml' => [
'schema' => [
'$ref' => '#/components/schemas/ContentInfoWrapper',
],
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/objects/content_id/PATCH/ContentInfo.xml.example',
],
],
],
Response::HTTP_BAD_REQUEST => [
'description' => 'Error - the input does not match the input schema definition or the validation on a field fails.',
],
Response::HTTP_UNAUTHORIZED => [
'description' => 'Error - the user is not authorized to create this Object in this Location.',
],
Response::HTTP_NOT_FOUND => [
'description' => 'Error - the parent Location specified in the request body does not exist.',
],
],
),
)]
class ContentCreateController extends RestController
{
public function __construct(
private readonly ContentService\RelationListFacadeInterface $relationListFacade
) {
}
/**
* Creates a new content draft assigned to the authenticated user.
* If a different userId is given in the input it is assigned to the
* given user but this required special rights for the authenticated
* user (this is useful for content staging where the transfer process
* does not have to authenticate with the user which created the content
* object in the source server). The user has to publish the content if
* it should be visible.
*/
public function createContent(Request $request): CreatedContent
{
$contentCreate = $this->parseContentRequest($request);
return $this->doCreateContent($request, $contentCreate);
}
protected function parseContentRequest(Request $request): mixed
{
return $this->inputDispatcher->parse(
new Message(
['Content-Type' => $request->headers->get('Content-Type'), 'Url' => $request->getPathInfo()],
$request->getContent()
)
);
}
/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
*/
protected function doCreateContent(Request $request, RestContentCreateStruct $contentCreate): CreatedContent
{
try {
$contentCreateStruct = $contentCreate->contentCreateStruct;
$contentCreate->locationCreateStruct->sortField = $contentCreateStruct->contentType->defaultSortField;
$contentCreate->locationCreateStruct->sortOrder = $contentCreateStruct->contentType->defaultSortOrder;
$content = $this->repository->getContentService()->createContent(
$contentCreateStruct,
[$contentCreate->locationCreateStruct]
);
} catch (ContentValidationException $e) {
throw new BadRequestException($e->getMessage());
} catch (ContentFieldValidationException $e) {
throw new RESTContentFieldValidationException($e);
}
$contentValue = null;
$contentType = null;
$relations = null;
if ($this->getMediaType($request) === 'application/vnd.ibexa.api.content') {
$contentValue = $content;
$contentType = $this->repository->getContentTypeService()->loadContentType(
$content->getVersionInfo()->getContentInfo()->contentTypeId
);
$relations = iterator_to_array($this->relationListFacade->getRelations($contentValue->getVersionInfo()));
}
return new CreatedContent(
[
'content' => new Values\RestContent(
$content->contentInfo,
null,
$contentValue,
$contentType,
$relations
),
]
);
}
}