Skip to content

Commit 3c2c53f

Browse files
tischsoicbarw4
authored andcommitted
split controllers - one action per controller: URLAlias without services
1 parent 618b449 commit 3c2c53f

File tree

6 files changed

+553
-5
lines changed

6 files changed

+553
-5
lines changed

src/bundle/Resources/config/routing.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,29 +1147,29 @@ ibexa.rest.delete_session:
11471147

11481148
ibexa.rest.list_global_url_aliases:
11491149
path: /content/urlaliases
1150-
controller: Ibexa\Rest\Server\Controller\URLAlias::listGlobalURLAliases
1150+
controller: Ibexa\Rest\Server\Controller\URLAlias\URLAliasListGlobalController::listGlobalURLAliases
11511151
methods: [GET]
11521152

11531153
ibexa.rest.list_location_url_aliases:
11541154
path: /content/locations/{locationPath}/urlaliases
1155-
controller: Ibexa\Rest\Server\Controller\URLAlias::listLocationURLAliases
1155+
controller: Ibexa\Rest\Server\Controller\URLAlias\URLAliasListLocationController::listLocationURLAliases
11561156
methods: [GET]
11571157
requirements:
11581158
locationPath: "[0-9/]+"
11591159

11601160
ibexa.rest.create_url_alias:
11611161
path: /content/urlaliases
1162-
controller: Ibexa\Rest\Server\Controller\URLAlias::createURLAlias
1162+
controller: Ibexa\Rest\Server\Controller\URLAlias\URLAliasCreateController::createURLAlias
11631163
methods: [POST]
11641164

11651165
ibexa.rest.load_url_alias:
11661166
path: /content/urlaliases/{urlAliasId}
1167-
controller: Ibexa\Rest\Server\Controller\URLAlias::loadURLAlias
1167+
controller: Ibexa\Rest\Server\Controller\URLAlias\URLAliasLoadByIdController::loadURLAlias
11681168
methods: [GET]
11691169

11701170
ibexa.rest.delete_url_alias:
11711171
path: /content/urlaliases/{urlAliasId}
1172-
controller: Ibexa\Rest\Server\Controller\URLAlias::deleteURLAlias
1172+
controller: Ibexa\Rest\Server\Controller\URLAlias\URLAliasDeleteController::deleteURLAlias
11731173
methods: [DELETE]
11741174

11751175
# Services
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
8+
namespace Ibexa\Rest\Server\Controller\URLAlias;
9+
10+
use ApiPlatform\Metadata\Delete;
11+
use ApiPlatform\Metadata\Get;
12+
use ApiPlatform\Metadata\Post;
13+
use ApiPlatform\OpenApi\Factory\OpenApiFactory;
14+
use ApiPlatform\OpenApi\Model;
15+
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
16+
use Ibexa\Contracts\Core\Repository\LocationService;
17+
use Ibexa\Contracts\Core\Repository\URLAliasService;
18+
use Ibexa\Rest\Message;
19+
use Ibexa\Rest\Server\Controller as RestController;
20+
use Ibexa\Rest\Server\Exceptions\ForbiddenException;
21+
use Ibexa\Rest\Server\Values;
22+
use JMS\TranslationBundle\Annotation\Ignore;
23+
use Symfony\Component\HttpFoundation\Request;
24+
use Symfony\Component\HttpFoundation\Response;
25+
26+
#[Post(
27+
uriTemplate: '/content/urlaliases',
28+
name: 'Create URL alias',
29+
extraProperties: [OpenApiFactory::OVERRIDE_OPENAPI_RESPONSES => false],
30+
openapi: new Model\Operation(
31+
summary: 'Creates a URL alias.',
32+
tags: [
33+
'Url Alias',
34+
],
35+
parameters: [
36+
new Model\Parameter(
37+
name: 'Accept',
38+
in: 'header',
39+
required: true,
40+
description: 'If set, the created URL alias is returned in XML or JSON format.',
41+
schema: [
42+
'type' => 'string',
43+
],
44+
),
45+
new Model\Parameter(
46+
name: 'Content-Type',
47+
in: 'header',
48+
required: true,
49+
description: 'The URL alias input schema encoded in XML or JSON format.',
50+
schema: [
51+
'type' => 'string',
52+
],
53+
),
54+
],
55+
requestBody: new Model\RequestBody(
56+
content: new \ArrayObject([
57+
'application/vnd.ibexa.api.UrlAliasCreate+xml' => [
58+
'schema' => [
59+
'$ref' => '#/components/schemas/UrlAliasCreate',
60+
],
61+
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/urlaliases/POST/UrlAliasCreate.xml.example',
62+
],
63+
'application/vnd.ibexa.api.UrlAliasCreate+json' => [
64+
'schema' => [
65+
'$ref' => '#/components/schemas/UrlAliasCreateWrapper',
66+
],
67+
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/urlaliases/POST/UrlAliasCreate.json.example',
68+
],
69+
]),
70+
),
71+
responses: [
72+
Response::HTTP_CREATED => [
73+
'description' => 'URL alias created.',
74+
'content' => [
75+
'application/vnd.ibexa.api.UrlAlias+xml' => [
76+
'schema' => [
77+
'$ref' => '#/components/schemas/UrlAlias',
78+
],
79+
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/urlaliases/url_alias_id/GET/UrlAlias.xml.example',
80+
],
81+
'application/vnd.ibexa.api.UrlAlias+json' => [
82+
'schema' => [
83+
'$ref' => '#/components/schemas/UrlAliasWrapper',
84+
],
85+
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/urlaliases/url_alias_id/GET/UrlAlias.json.example',
86+
],
87+
],
88+
],
89+
Response::HTTP_BAD_REQUEST => [
90+
'description' => 'Error - The input does not match the input schema definition.',
91+
],
92+
Response::HTTP_UNAUTHORIZED => [
93+
'description' => 'Error - The user is not authorized to create a URL alias.',
94+
],
95+
Response::HTTP_FORBIDDEN => [
96+
'description' => 'Error - A URL alias with the same identifier already exists.',
97+
],
98+
],
99+
),
100+
)]
101+
class URLAliasCreateController extends RestController
102+
{
103+
public function __construct(
104+
protected URLAliasService $urlAliasService,
105+
protected LocationService $locationService
106+
) {
107+
}
108+
109+
/**
110+
* Creates a new URL alias.
111+
*
112+
* @throws \Ibexa\Rest\Server\Exceptions\ForbiddenException
113+
*
114+
* @return \Ibexa\Rest\Server\Values\CreatedURLAlias
115+
*/
116+
public function createURLAlias(Request $request)
117+
{
118+
$urlAliasCreate = $this->inputDispatcher->parse(
119+
new Message(
120+
['Content-Type' => $request->headers->get('Content-Type')],
121+
$request->getContent()
122+
)
123+
);
124+
125+
if ($urlAliasCreate['_type'] === 'LOCATION') {
126+
$locationPathParts = explode(
127+
'/',
128+
$this->requestParser->parseHref($urlAliasCreate['location']['_href'], 'locationPath')
129+
);
130+
131+
$location = $this->locationService->loadLocation(
132+
array_pop($locationPathParts)
133+
);
134+
135+
try {
136+
$createdURLAlias = $this->urlAliasService->createUrlAlias(
137+
$location,
138+
$urlAliasCreate['path'],
139+
$urlAliasCreate['languageCode'],
140+
$urlAliasCreate['forward'],
141+
$urlAliasCreate['alwaysAvailable']
142+
);
143+
} catch (InvalidArgumentException $e) {
144+
throw new ForbiddenException(/** @Ignore */ $e->getMessage());
145+
}
146+
} else {
147+
try {
148+
$createdURLAlias = $this->urlAliasService->createGlobalUrlAlias(
149+
$urlAliasCreate['resource'],
150+
$urlAliasCreate['path'],
151+
$urlAliasCreate['languageCode'],
152+
$urlAliasCreate['forward'],
153+
$urlAliasCreate['alwaysAvailable']
154+
);
155+
} catch (InvalidArgumentException $e) {
156+
throw new ForbiddenException(/** @Ignore */ $e->getMessage());
157+
}
158+
}
159+
160+
return new Values\CreatedURLAlias(
161+
[
162+
'urlAlias' => $createdURLAlias,
163+
]
164+
);
165+
}
166+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
8+
namespace Ibexa\Rest\Server\Controller\URLAlias;
9+
10+
use ApiPlatform\Metadata\Delete;
11+
use ApiPlatform\Metadata\Get;
12+
use ApiPlatform\Metadata\Post;
13+
use ApiPlatform\OpenApi\Factory\OpenApiFactory;
14+
use ApiPlatform\OpenApi\Model;
15+
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
16+
use Ibexa\Contracts\Core\Repository\LocationService;
17+
use Ibexa\Contracts\Core\Repository\URLAliasService;
18+
use Ibexa\Rest\Message;
19+
use Ibexa\Rest\Server\Controller as RestController;
20+
use Ibexa\Rest\Server\Exceptions\ForbiddenException;
21+
use Ibexa\Rest\Server\Values;
22+
use JMS\TranslationBundle\Annotation\Ignore;
23+
use Symfony\Component\HttpFoundation\Request;
24+
use Symfony\Component\HttpFoundation\Response;
25+
26+
#[Delete(
27+
uriTemplate: '/content/urlaliases/{urlAliasId}',
28+
name: 'Delete URL alias',
29+
openapi: new Model\Operation(
30+
summary: 'Deletes the provided URL alias.',
31+
tags: [
32+
'Url Alias',
33+
],
34+
parameters: [
35+
new Model\Parameter(
36+
name: 'urlAliasId',
37+
in: 'path',
38+
required: true,
39+
schema: [
40+
'type' => 'string',
41+
],
42+
),
43+
],
44+
responses: [
45+
Response::HTTP_NO_CONTENT => [
46+
'description' => 'No Content - URL alias deleted.',
47+
],
48+
Response::HTTP_UNAUTHORIZED => [
49+
'description' => 'Error - The user is not authorized to delete a URL alias.',
50+
],
51+
Response::HTTP_NOT_FOUND => [
52+
'description' => 'Error - The URL alias does not exist.',
53+
],
54+
],
55+
),
56+
)]
57+
class URLAliasDeleteController extends RestController
58+
{
59+
public function __construct(
60+
protected URLAliasService $urlAliasService,
61+
protected LocationService $locationService
62+
) {
63+
}
64+
65+
/**
66+
* The given URL alias is deleted.
67+
*
68+
* @param $urlAliasId
69+
*
70+
* @return \Ibexa\Rest\Server\Values\NoContent
71+
*/
72+
public function deleteURLAlias($urlAliasId)
73+
{
74+
$this->urlAliasService->removeAliases(
75+
[
76+
$this->urlAliasService->load($urlAliasId),
77+
]
78+
);
79+
80+
return new Values\NoContent();
81+
}
82+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
8+
namespace Ibexa\Rest\Server\Controller\URLAlias;
9+
10+
use ApiPlatform\Metadata\Delete;
11+
use ApiPlatform\Metadata\Get;
12+
use ApiPlatform\Metadata\Post;
13+
use ApiPlatform\OpenApi\Factory\OpenApiFactory;
14+
use ApiPlatform\OpenApi\Model;
15+
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
16+
use Ibexa\Contracts\Core\Repository\LocationService;
17+
use Ibexa\Contracts\Core\Repository\URLAliasService;
18+
use Ibexa\Rest\Message;
19+
use Ibexa\Rest\Server\Controller as RestController;
20+
use Ibexa\Rest\Server\Exceptions\ForbiddenException;
21+
use Ibexa\Rest\Server\Values;
22+
use JMS\TranslationBundle\Annotation\Ignore;
23+
use Symfony\Component\HttpFoundation\Request;
24+
use Symfony\Component\HttpFoundation\Response;
25+
26+
#[Get(
27+
uriTemplate: '/content/urlaliases',
28+
name: 'List global URL aliases',
29+
openapi: new Model\Operation(
30+
summary: 'Returns the list of global URL aliases.',
31+
tags: [
32+
'Url Alias',
33+
],
34+
parameters: [
35+
new Model\Parameter(
36+
name: 'Accept',
37+
in: 'header',
38+
required: true,
39+
description: 'If set, the URL alias list contains only references and is returned in XML or JSON format.',
40+
schema: [
41+
'type' => 'string',
42+
],
43+
),
44+
],
45+
responses: [
46+
Response::HTTP_OK => [
47+
'description' => 'OK - returns the list of URL aliases.',
48+
'content' => [
49+
'application/vnd.ibexa.api.UrlAliasRefList+xml' => [
50+
'schema' => [
51+
'$ref' => '#/components/schemas/UrlAliasRefList',
52+
],
53+
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/urlaliases/GET/UrlAliasRefList.xml.example',
54+
],
55+
'application/vnd.ibexa.api.UrlAliasRefList+json' => [
56+
'schema' => [
57+
'$ref' => '#/components/schemas/UrlAliasRefListWrapper',
58+
],
59+
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/urlaliases/GET/UrlAliasRefList.json.example',
60+
],
61+
],
62+
],
63+
Response::HTTP_UNAUTHORIZED => [
64+
'description' => 'Error - The user has no permission to read URL aliases.',
65+
],
66+
],
67+
),
68+
)]
69+
class URLAliasListGlobalController extends RestController
70+
{
71+
public function __construct(
72+
protected URLAliasService $urlAliasService,
73+
protected LocationService $locationService
74+
) {
75+
}
76+
77+
/**
78+
* Returns the list of global URL aliases.
79+
*
80+
* @return \Ibexa\Rest\Server\Values\URLAliasRefList
81+
*/
82+
public function listGlobalURLAliases()
83+
{
84+
return new Values\URLAliasRefList(
85+
$this->urlAliasService->listGlobalAliases(),
86+
$this->router->generate('ibexa.rest.list_global_url_aliases')
87+
);
88+
}
89+
}

0 commit comments

Comments
 (0)