-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathContentRelationCreateController.php
159 lines (147 loc) · 6.26 KB
/
ContentRelationCreateController.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
<?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\NotFoundException;
use Ibexa\Contracts\Core\Repository\Values\Content\Relation;
use Ibexa\Rest\Message;
use Ibexa\Rest\Server\Controller as RestController;
use Ibexa\Rest\Server\Exceptions\ForbiddenException;
use Ibexa\Rest\Server\Values;
use JMS\TranslationBundle\Annotation\Ignore;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
#[Post(
uriTemplate: '/content/objects/{contentId}/versions/{versionNo}/relations',
extraProperties: [OpenApiFactory::OVERRIDE_OPENAPI_RESPONSES => false],
openapi: new Model\Operation(
summary: 'Create new Relation',
description: 'Creates a new Relation of type COMMON for the given draft.',
tags: [
'Objects',
],
parameters: [
new Model\Parameter(
name: 'Accept',
in: 'header',
required: true,
description: 'If set, the updated version is returned in XML or JSON format.',
schema: [
'type' => 'string',
],
),
new Model\Parameter(
name: 'Content-Type',
in: 'header',
required: true,
description: 'The RelationCreate schema encoded in XML or JSON format.',
schema: [
'type' => 'string',
],
),
new Model\Parameter(
name: 'contentId',
in: 'path',
required: true,
schema: [
'type' => 'string',
],
),
new Model\Parameter(
name: 'versionNo',
in: 'path',
required: true,
schema: [
'type' => 'string',
],
),
],
requestBody: new Model\RequestBody(
content: new \ArrayObject([
'application/vnd.ibexa.api.RelationCreate+xml' => [
'schema' => [
'$ref' => '#/components/schemas/RelationCreate',
],
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/objects/content_id/versions/version_no/relations/POST/RelationCreate.xml.example',
],
'application/vnd.ibexa.api.RelationCreate+json' => [
'schema' => [
'$ref' => '#/components/schemas/RelationCreateWrapper',
],
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/objects/content_id/versions/version_no/relations/POST/RelationCreate.json.example',
],
]),
),
responses: [
Response::HTTP_CREATED => [
'content' => [
'application/vnd.ibexa.api.Relation+xml' => [
'schema' => [
'$ref' => '#/components/schemas/Relation',
],
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/objects/content_id/versions/version_no/relations/relation_id/GET/Relation.xml.example',
],
'application/vnd.ibexa.api.Relation+json' => [
'schema' => [
'$ref' => '#/components/schemas/RelationWrapper',
],
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/objects/content_id/versions/version_no/relations/POST/Relation.json.example',
],
],
],
],
),
)]
class ContentRelationCreateController extends RestController
{
public function __construct(
private readonly ContentService\RelationListFacadeInterface $relationListFacade
) {
}
/**
* Creates a new relation of type COMMON for the given draft.
*
* @throws \Ibexa\Rest\Server\Exceptions\ForbiddenException if version $versionNumber isn't a draft
* @throws \Ibexa\Rest\Server\Exceptions\ForbiddenException if a relation to the same content already exists
*/
public function createRelation(int $contentId, int $versionNumber, Request $request): Values\CreatedRelation
{
$contentService = $this->repository->getContentService();
$destinationContentId = $this->inputDispatcher->parse(
new Message(
['Content-Type' => $request->headers->get('Content-Type')],
$request->getContent()
)
);
$contentInfo = $contentService->loadContentInfo($contentId);
$versionInfo = $contentService->loadVersionInfo($contentInfo, $versionNumber);
if (!$versionInfo->isDraft()) {
throw new ForbiddenException('Relation of type COMMON can only be added to drafts');
}
try {
$destinationContentInfo = $contentService->loadContentInfo($destinationContentId);
} catch (NotFoundException $e) {
throw new ForbiddenException(/** @Ignore */ $e->getMessage());
}
$existingRelations = iterator_to_array($this->relationListFacade->getRelations(
$versionInfo,
));
foreach ($existingRelations as $existingRelation) {
if ($existingRelation->getDestinationContentInfo()->id == $destinationContentId) {
throw new ForbiddenException('Relation of type COMMON to the selected destination content ID already exists');
}
}
$relation = $contentService->addRelation($versionInfo, $destinationContentInfo);
return new Values\CreatedRelation(
[
'relation' => new Values\RestRelation($relation, $contentId, $versionNumber),
]
);
}
}