-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathContentMetadataUpdateController.php
167 lines (157 loc) · 6.62 KB
/
ContentMetadataUpdateController.php
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
167
<?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\Patch;
use ApiPlatform\OpenApi\Factory\OpenApiFactory;
use ApiPlatform\OpenApi\Model;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Rest\Message;
use Ibexa\Rest\Server\Controller as RestController;
use Ibexa\Rest\Server\Values;
use LogicException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
#[Patch(
uriTemplate: '/content/objects/{contentId}',
extraProperties: [OpenApiFactory::OVERRIDE_OPENAPI_RESPONSES => false],
openapi: new Model\Operation(
summary: 'Update content',
description: 'This method updates the content metadata which is independent from a version. PATCH or POST with header X-HTTP-Method-Override PATCH.',
tags: [
'Objects',
],
parameters: [
new Model\Parameter(
name: 'Accept',
in: 'header',
required: true,
description: 'If set, all information for the content item (excluding the current version) is returned in XML or JSON format.',
schema: [
'type' => 'string',
],
),
new Model\Parameter(
name: 'If-match',
in: 'header',
required: true,
description: 'Causes to patch only if the specified ETag is the current one. Otherwise a 412 is returned.',
schema: [
'type' => 'string',
],
),
new Model\Parameter(
name: 'Content-Type',
in: 'header',
required: true,
description: 'The ContentUpdate schema encoded in XML or JSON format.',
schema: [
'type' => 'string',
],
),
new Model\Parameter(
name: 'contentId',
in: 'path',
required: true,
schema: [
'type' => 'string',
],
),
],
requestBody: new Model\RequestBody(
content: new \ArrayObject([
'application/vnd.ibexa.api.ContentUpdate+xml' => [
'schema' => [
'$ref' => '#/components/schemas/ContentInfo',
],
],
'application/vnd.ibexa.api.ContentUpdate+json' => [
'schema' => [
'$ref' => '#/components/schemas/ContentInfoWrapper',
],
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/objects/content_id/PATCH/ContentInfo.xml.example',
],
]),
),
responses: [
Response::HTTP_OK => [
'content' => [
'application/vnd.ibexa.api.ContentInfo+xml' => [
'schema' => [
'$ref' => '#/components/schemas/ContentInfo',
],
],
'application/vnd.ibexa.api.ContentInfo+json' => [
'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.',
],
Response::HTTP_UNAUTHORIZED => [
'description' => 'Error - the user is not authorized to update this object.',
],
Response::HTTP_NOT_FOUND => [
'description' => 'Error - the content ID does not exist.',
],
Response::HTTP_PRECONDITION_FAILED => [
'description' => 'Error - the current ETag does not match with the one provided in the If-Match header.',
],
Response::HTTP_UNSUPPORTED_MEDIA_TYPE => [
'description' => 'Error - the media-type is not one of those specified in headers.',
],
],
),
)]
class ContentMetadataUpdateController extends RestController
{
/**
* Updates a content's metadata.
*/
public function updateContentMetadata(int $contentId, Request $request): Values\RestContent
{
$updateStruct = $this->inputDispatcher->parse(
new Message(
['Content-Type' => $request->headers->get('Content-Type')],
$request->getContent()
)
);
$contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
// update section
if ($updateStruct->sectionId !== null) {
$section = $this->repository->getSectionService()->loadSection($updateStruct->sectionId);
$this->repository->getSectionService()->assignSection($contentInfo, $section);
$updateStruct->sectionId = null;
}
// @todo Consider refactoring! ContentService::updateContentMetadata throws the same exception
// in case the updateStruct is empty and if remoteId already exists. Since REST version of update struct
// includes section ID in addition to other fields, we cannot throw exception if only sectionId property
// is set, so we must skip updating content in that case instead of allowing propagation of the exception.
foreach ($updateStruct as $propertyName => $propertyValue) {
if ($propertyName !== 'sectionId' && $propertyValue !== null) {
// update content
$this->repository->getContentService()->updateContentMetadata($contentInfo, $updateStruct);
$contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
break;
}
}
if ($contentInfo->mainLocationId === null) {
throw new LogicException();
}
try {
$locationInfo = $this->repository->getLocationService()->loadLocation($contentInfo->mainLocationId);
} catch (NotFoundException $e) {
$locationInfo = null;
}
return new Values\RestContent(
$contentInfo,
$locationInfo
);
}
}