-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathContentLoadByIdController.php
145 lines (132 loc) · 5.86 KB
/
ContentLoadByIdController.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
<?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\Get;
use ApiPlatform\OpenApi\Model;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Values\Content\Language;
use Ibexa\Rest\Server\Controller as RestController;
use Ibexa\Rest\Server\Values;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
#[Get(
uriTemplate: '/content/objects/{contentId}',
openapi: new Model\Operation(
summary: 'Load content',
description: 'Loads the content item for the given ID. Depending on the Accept header the current version is embedded (i.e. the current published version or if it does not exist, the draft of the authenticated user).',
tags: [
'Objects',
],
parameters: [
new Model\Parameter(
name: 'Accept',
in: 'header',
required: true,
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.',
schema: [
'type' => 'string',
],
),
new Model\Parameter(
name: 'If-None-Match',
in: 'header',
required: true,
description: 'If the provided ETag matches the current ETag then a "304 Not Modified" is returned. The ETag changes if the meta data has changed, this happens also if there is a new published version.',
schema: [
'type' => 'string',
],
),
new Model\Parameter(
name: 'contentId',
in: 'path',
required: true,
schema: [
'type' => 'string',
],
),
],
responses: [
Response::HTTP_OK => [
'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/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_UNAUTHORIZED => [
'description' => 'Error - the user is not authorized to read this object. This could also happen if there is no published version yet and another user owns a draft of this content item.',
],
Response::HTTP_NOT_FOUND => [
'description' => 'Error - the ID is not found.',
],
],
),
)]
class ContentLoadByIdController extends RestController
{
public function __construct(
private readonly ContentService\RelationListFacadeInterface $relationListFacade
) {
}
/**
* Loads a content info, potentially with the current version embedded.
*/
public function loadContent(int $contentId, Request $request): Values\CachedValue|Values\RestContent
{
$contentService = $this->repository->getContentService();
$contentInfo = $contentService->loadContentInfo($contentId);
$mainLocation = null;
if (!empty($contentInfo->mainLocationId)) {
$mainLocation = $this->repository->getLocationService()->loadLocation($contentInfo->mainLocationId);
}
$contentType = $this->repository->getContentTypeService()->loadContentType($contentInfo->contentTypeId);
$contentVersion = null;
$relations = null;
if ($this->getMediaType($request) === 'application/vnd.ibexa.api.content') {
$languages = Language::ALL;
if ($request->query->has('languages')) {
$languages = explode(',', $request->query->getString('languages'));
}
$contentVersion = $contentService->loadContent($contentId, $languages);
$relations = iterator_to_array($this->relationListFacade->getRelations($contentVersion->getVersionInfo()));
}
$restContent = new Values\RestContent(
$contentInfo,
$mainLocation,
$contentVersion,
$contentType,
$relations,
$request->getPathInfo()
);
if ($contentInfo->mainLocationId === null) {
return $restContent;
}
return new Values\CachedValue(
$restContent,
['locationId' => $contentInfo->mainLocationId]
);
}
}