Skip to content

Commit 618b449

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

File tree

7 files changed

+575
-7
lines changed

7 files changed

+575
-7
lines changed

src/bundle/Resources/config/routing.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ ibexa.rest.location.copy:
405405

406406
ibexa.rest.trash_location:
407407
path: /content/locations/{locationPath}
408-
controller: Ibexa\Rest\Server\Controller\Trash::trashLocation
408+
controller: Ibexa\Rest\Server\Controller\Trash\LocationTrashController::trashLocation
409409
condition: 'ibexa_get_media_type(request) === "TrashLocationInput"'
410410
methods: [POST, TRASH]
411411
options:
@@ -718,7 +718,7 @@ ibexa.rest.unlink_content_type_from_group:
718718

719719
ibexa.rest.trash.restore_trash_item:
720720
path: /content/trash/{trashItemId}
721-
controller: Ibexa\Rest\Server\Controller\Trash::restoreItem
721+
controller: Ibexa\Rest\Server\Controller\Trash\TrashItemRestoreController::restoreItem
722722
condition: 'ibexa_get_media_type(request) === "RestoreTrashItemInput"'
723723
methods: [POST]
724724
options:
@@ -728,31 +728,31 @@ ibexa.rest.trash.restore_trash_item:
728728

729729
ibexa.rest.load_trash_items:
730730
path: /content/trash
731-
controller: Ibexa\Rest\Server\Controller\Trash::loadTrashItems
731+
controller: Ibexa\Rest\Server\Controller\Trash\TrashItemListController::loadTrashItems
732732
methods: [GET]
733733

734734
ibexa.rest.empty_trash:
735735
path: /content/trash
736-
controller: Ibexa\Rest\Server\Controller\Trash::emptyTrash
736+
controller: Ibexa\Rest\Server\Controller\Trash\TrashEmptyController::emptyTrash
737737
methods: [DELETE]
738738

739739
ibexa.rest.load_trash_item:
740740
path: /content/trash/{trashItemId}
741-
controller: Ibexa\Rest\Server\Controller\Trash::loadTrashItem
741+
controller: Ibexa\Rest\Server\Controller\Trash\TrashItemLoadByIdController::loadTrashItem
742742
methods: [GET]
743743
requirements:
744744
trashItemId: \d+
745745

746746
ibexa.rest.delete_trash_item:
747747
path: /content/trash/{trashItemId}
748-
controller: Ibexa\Rest\Server\Controller\Trash::deleteTrashItem
748+
controller: Ibexa\Rest\Server\Controller\Trash\TrashItemDeleteController::deleteTrashItem
749749
methods: [DELETE]
750750
requirements:
751751
trashItemId: \d+
752752

753753
ibexa.rest.restore_trash_item:
754754
path: /content/trash/{trashItemId}
755-
controller: Ibexa\Rest\Server\Controller\Trash::restoreTrashItem
755+
controller: Ibexa\Rest\Server\Controller\Trash\TrashItemRestoreController::restoreTrashItem
756756
methods: [MOVE]
757757
requirements:
758758
trashItemId: \d+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Trash;
9+
10+
use ApiPlatform\Metadata\Delete;
11+
use ApiPlatform\Metadata\Get;
12+
use ApiPlatform\OpenApi\Model;
13+
use Ibexa\Contracts\Core\Repository\Exceptions as ApiExceptions;
14+
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
15+
use Ibexa\Contracts\Core\Repository\LocationService;
16+
use Ibexa\Contracts\Core\Repository\TrashService;
17+
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
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 Ibexa\Rest\Value as RestValue;
23+
use InvalidArgumentException;
24+
use JMS\TranslationBundle\Annotation\Ignore;
25+
use Symfony\Component\HttpFoundation\Request;
26+
use Symfony\Component\HttpFoundation\Response;
27+
use Webmozart\Assert\Assert;
28+
29+
30+
class LocationTrashController extends RestController
31+
{
32+
public function __construct(
33+
protected TrashService $trashService,
34+
protected LocationService $locationService
35+
) {
36+
}
37+
38+
/**
39+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
40+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
41+
*/
42+
public function trashLocation(string $locationPath): RestValue
43+
{
44+
$location = $this->locationService->loadLocation(
45+
$this->extractLocationIdFromPath($locationPath),
46+
);
47+
48+
$trashItem = $this->trashService->trash($location);
49+
50+
if ($trashItem === null) {
51+
return new Values\NoContent();
52+
}
53+
54+
return new Values\ResourceCreated(
55+
$this->router->generate(
56+
'ibexa.rest.load_trash_item',
57+
['trashItemId' => $trashItem->getId()],
58+
),
59+
);
60+
}
61+
62+
private function extractLocationIdFromPath(string $path): int
63+
{
64+
$pathParts = explode('/', $path);
65+
$lastPart = array_pop($pathParts);
66+
67+
Assert::integerish($lastPart);
68+
69+
return (int)$lastPart;
70+
}
71+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Trash;
9+
10+
use ApiPlatform\Metadata\Delete;
11+
use ApiPlatform\Metadata\Get;
12+
use ApiPlatform\OpenApi\Model;
13+
use Ibexa\Contracts\Core\Repository\Exceptions as ApiExceptions;
14+
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
15+
use Ibexa\Contracts\Core\Repository\LocationService;
16+
use Ibexa\Contracts\Core\Repository\TrashService;
17+
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
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 Ibexa\Rest\Value as RestValue;
23+
use InvalidArgumentException;
24+
use JMS\TranslationBundle\Annotation\Ignore;
25+
use Symfony\Component\HttpFoundation\Request;
26+
use Symfony\Component\HttpFoundation\Response;
27+
use Webmozart\Assert\Assert;
28+
29+
#[Delete(
30+
uriTemplate: '/content/trash',
31+
name: 'Empty Trash',
32+
openapi: new Model\Operation(
33+
summary: 'Empties the Trash.',
34+
tags: [
35+
'Trash',
36+
],
37+
parameters: [
38+
],
39+
responses: [
40+
Response::HTTP_NO_CONTENT => [
41+
'description' => 'No Content - Trash emptied.',
42+
],
43+
Response::HTTP_UNAUTHORIZED => [
44+
'description' => 'Error - The user is not authorized to empty all items from Trash.',
45+
],
46+
],
47+
),
48+
)]
49+
class TrashEmptyController extends RestController
50+
{
51+
public function __construct(
52+
protected TrashService $trashService,
53+
protected LocationService $locationService
54+
) {
55+
}
56+
57+
/**
58+
* Empties the trash.
59+
*
60+
* @return \Ibexa\Rest\Server\Values\NoContent
61+
*/
62+
public function emptyTrash()
63+
{
64+
$this->trashService->emptyTrash();
65+
66+
return new Values\NoContent();
67+
}
68+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Trash;
9+
10+
use ApiPlatform\Metadata\Delete;
11+
use ApiPlatform\Metadata\Get;
12+
use ApiPlatform\OpenApi\Model;
13+
use Ibexa\Contracts\Core\Repository\Exceptions as ApiExceptions;
14+
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
15+
use Ibexa\Contracts\Core\Repository\LocationService;
16+
use Ibexa\Contracts\Core\Repository\TrashService;
17+
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
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 Ibexa\Rest\Value as RestValue;
23+
use InvalidArgumentException;
24+
use JMS\TranslationBundle\Annotation\Ignore;
25+
use Symfony\Component\HttpFoundation\Request;
26+
use Symfony\Component\HttpFoundation\Response;
27+
use Webmozart\Assert\Assert;
28+
29+
#[Delete(
30+
uriTemplate: '/content/trash/{trashItemid}',
31+
name: 'Delete Trash item',
32+
openapi: new Model\Operation(
33+
summary: 'Deletes the provided item from Trash.',
34+
tags: [
35+
'Trash',
36+
],
37+
parameters: [
38+
new Model\Parameter(
39+
name: 'trashItemid',
40+
in: 'path',
41+
required: true,
42+
schema: [
43+
'type' => 'string',
44+
],
45+
),
46+
],
47+
responses: [
48+
Response::HTTP_NO_CONTENT => [
49+
'description' => 'No Content - item deleted.',
50+
],
51+
Response::HTTP_UNAUTHORIZED => [
52+
'description' => 'Error - The user is not authorized to delete the provided item.',
53+
],
54+
Response::HTTP_NOT_FOUND => [
55+
'description' => 'Error - The provided item does not exist in Trash.',
56+
],
57+
],
58+
),
59+
)]
60+
class TrashItemDeleteController extends RestController
61+
{
62+
public function __construct(
63+
protected TrashService $trashService,
64+
protected LocationService $locationService
65+
) {
66+
}
67+
68+
/**
69+
* Deletes the given trash item.
70+
*
71+
* @param $trashItemId
72+
*
73+
* @return \Ibexa\Rest\Server\Values\NoContent
74+
*/
75+
public function deleteTrashItem($trashItemId)
76+
{
77+
$this->trashService->deleteTrashItem(
78+
$this->trashService->loadTrashItem($trashItemId)
79+
);
80+
81+
return new Values\NoContent();
82+
}
83+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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\Trash;
9+
10+
use ApiPlatform\Metadata\Delete;
11+
use ApiPlatform\Metadata\Get;
12+
use ApiPlatform\OpenApi\Model;
13+
use Ibexa\Contracts\Core\Repository\Exceptions as ApiExceptions;
14+
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
15+
use Ibexa\Contracts\Core\Repository\LocationService;
16+
use Ibexa\Contracts\Core\Repository\TrashService;
17+
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
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 Ibexa\Rest\Value as RestValue;
23+
use InvalidArgumentException;
24+
use JMS\TranslationBundle\Annotation\Ignore;
25+
use Symfony\Component\HttpFoundation\Request;
26+
use Symfony\Component\HttpFoundation\Response;
27+
use Webmozart\Assert\Assert;
28+
29+
#[Get(
30+
uriTemplate: '/content/trash',
31+
name: 'List Trash items',
32+
openapi: new Model\Operation(
33+
summary: 'Returns a list of all items in the Trash.',
34+
tags: [
35+
'Trash',
36+
],
37+
parameters: [
38+
new Model\Parameter(
39+
name: 'Accept',
40+
in: 'header',
41+
required: true,
42+
description: 'If set, the Trash item list is returned in XML or JSON format.',
43+
schema: [
44+
'type' => 'string',
45+
],
46+
),
47+
],
48+
responses: [
49+
Response::HTTP_OK => [
50+
'description' => 'OK - returns the list of items in the Trash.',
51+
'content' => [
52+
'application/vnd.ibexa.api.Trash+xml' => [
53+
'schema' => [
54+
'$ref' => '#/components/schemas/Trash',
55+
],
56+
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/trash/GET/Trash.xml.example',
57+
],
58+
'application/vnd.ibexa.api.Trash+json' => [
59+
'schema' => [
60+
'$ref' => '#/components/schemas/TrashWrapper',
61+
],
62+
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/content/trash/GET/Trash.json.example',
63+
],
64+
],
65+
],
66+
Response::HTTP_UNAUTHORIZED => [
67+
'description' => 'Error - The user has no permission to read the Trash.',
68+
],
69+
],
70+
),
71+
)]
72+
class TrashItemListController extends RestController
73+
{
74+
public function __construct(
75+
protected TrashService $trashService,
76+
protected LocationService $locationService
77+
) {
78+
}
79+
80+
/**
81+
* Returns a list of all trash items.
82+
*
83+
* @return \Ibexa\Rest\Server\Values\Trash
84+
*/
85+
public function loadTrashItems(Request $request)
86+
{
87+
$offset = $request->query->has('offset') ? (int)$request->query->get('offset') : 0;
88+
$limit = $request->query->has('limit') ? (int)$request->query->get('limit') : -1;
89+
90+
$query = new Query();
91+
$query->offset = $offset >= 0 ? $offset : null;
92+
$query->limit = $limit >= 0 ? $limit : null;
93+
94+
$trashItems = [];
95+
96+
foreach ($this->trashService->findTrashItems($query)->items as $trashItem) {
97+
$trashItems[] = new Values\RestTrashItem(
98+
$trashItem,
99+
$this->locationService->getLocationChildCount($trashItem)
100+
);
101+
}
102+
103+
return new Values\Trash(
104+
$trashItems,
105+
$request->getPathInfo()
106+
);
107+
}
108+
}

0 commit comments

Comments
 (0)