Skip to content

Commit e0e4fca

Browse files
Feat/data admin (#172)
* Output associated resource for admin users when getting a route resource * Prioritise redirect as the assocaited resource * Fix to remove orphaned routes
1 parent b2de365 commit e0e4fca

File tree

6 files changed

+33
-8
lines changed

6 files changed

+33
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
/infection.json
1515
/clover.xml
1616
/tests/Functional/app/vendor/
17+
/tests/Functional/app/public/uploads/
1718
/tools/
1819

1920
###> symfony/phpunit-bridge ###

features/bootstrap/DoctrineContext.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ public function thereIsAPageDataResourceWithRoutePath(?string $path): void
586586
->setPageData($pageData);
587587
$this->timestampedHelper->persistTimestampedFields($route, true);
588588
$this->manager->persist($route);
589+
$this->restContext->resources['page_data_route'] = $this->iriConverter->getIriFromResource($route);
589590
}
590591

591592
$this->manager->flush();

features/main/route.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ Feature: Route resources
104104
And the JSON node "redirectedFrom[0].redirectedFrom" should exist
105105
And the JSON node "redirectedFrom[0].redirectedFrom[0].path" should be equal to the string "/redirect1"
106106

107+
@loginUser
108+
Scenario: If I delete a route aware resource, the associated routes should also be deleted
109+
Given there is a PageData resource with the route path "/my-route"
110+
When I send a "DELETE" request to the resource "page_data"
111+
Then the response status code should be 204
112+
And the resource "page_data" should not exist
113+
And the resource "page_data_route" should not exist
114+
115+
107116
Scenario: A resource with a relation to a route should return the path instead of the IRI
108117
Given there is a PageData resource with the route path "/my-route"
109118
When I send a "GET" request to the resource "page_data"

src/Entity/Core/Route.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,9 @@ public function setPageData(?AbstractPageData $pageData): self
200200

201201
return $this;
202202
}
203+
204+
#[Groups(['Route:cwa_resource:read:ROLE_ADMIN'])]
205+
public function getAssociatedResource() {
206+
return $this->redirect ?: $this->pageData ?: $this->page;
207+
}
203208
}

src/EventListener/Api/OrphanedComponentEventListener.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
use Doctrine\Persistence\ManagerRegistry;
1717
use Silverback\ApiComponentsBundle\Entity\Core\ComponentInterface;
1818
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
19+
use Silverback\ApiComponentsBundle\Entity\Core\Page;
1920
use Silverback\ApiComponentsBundle\Entity\Core\PageDataInterface;
21+
use Silverback\ApiComponentsBundle\Entity\Core\RoutableInterface;
22+
use Silverback\ApiComponentsBundle\Entity\Core\Route;
2023
use Silverback\ApiComponentsBundle\Metadata\Factory\ComponentUsageMetadataFactory;
2124
use Silverback\ApiComponentsBundle\Metadata\Factory\PageDataMetadataFactoryInterface;
2225
use Symfony\Component\HttpFoundation\Request;
@@ -65,16 +68,27 @@ public function onPreWrite(ViewEvent $event): void
6568
}
6669
}
6770
}
71+
if ($data instanceof RoutableInterface) {
72+
$route = $data->getRoute();
73+
if ($route) {
74+
$routeAssociations = 0;
75+
$route->getPage() && $routeAssociations++;
76+
$route->getPageData() && $routeAssociations++;
77+
$route->getRedirect() && $routeAssociations++;
78+
if ($routeAssociations <= 1) {
79+
$manager = $this->registry->getManagerForClass(Route::class);
80+
$manager?->remove($route);
81+
}
82+
}
83+
}
6884
}
6985

7086
private function removeOrphanedComponent(ComponentInterface $component, string $resourceClass): void
7187
{
7288
$metadata = $this->usageMetadataFactory->create($component);
7389
if (1 === $metadata->getTotal()) {
7490
$manager = $this->registry->getManagerForClass($resourceClass);
75-
if ($manager) {
76-
$manager->remove($component);
77-
}
91+
$manager?->remove($component);
7892
}
7993
}
8094
}

tests/Functional/app/public/uploads/image.svg

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)