Skip to content

Commit 1a3031d

Browse files
committed
Refactor to orphanedResourceHelper
1 parent 8673467 commit 1a3031d

3 files changed

Lines changed: 111 additions & 64 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Silverback API Components Bundle Project
5+
*
6+
* (c) Daniel West <daniel@silverback.is>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Silverback\ApiComponentsBundle\EventListener\Api;
15+
16+
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
17+
use Silverback\ApiComponentsBundle\Entity\Core\ComponentGroup;
18+
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
19+
use Silverback\ApiComponentsBundle\Entity\Core\Layout;
20+
use Silverback\ApiComponentsBundle\Entity\Core\Page;
21+
use Silverback\ApiComponentsBundle\Entity\Core\PageDataInterface;
22+
use Silverback\ApiComponentsBundle\Entity\Core\RoutableInterface;
23+
use Silverback\ApiComponentsBundle\Helper\OrphanedResourceHelper;
24+
use Symfony\Component\HttpFoundation\Request;
25+
use Symfony\Component\HttpKernel\Event\ViewEvent;
26+
27+
/**
28+
* @author Daniel West <daniel@silverback.is>
29+
*/
30+
readonly class DeletedResourceEventListener {
31+
public function __construct(
32+
private OrphanedResourceHelper $orphanedResourceHelper) {
33+
}
34+
35+
public function onPreWrite(ViewEvent $event): void
36+
{
37+
$request = $event->getRequest();
38+
$data = $request->attributes->get('data');
39+
$resourceClass = $request->attributes->get('_api_resource_class');
40+
// only listen for deleted
41+
if (!$request->isMethod(Request::METHOD_DELETE)) {
42+
return;
43+
}
44+
45+
if ($data instanceof ComponentPosition) {
46+
$this->orphanedResourceHelper->handleRemovedComponentPosition($data);
47+
return;
48+
}
49+
50+
if ($data instanceof Page || $data instanceof AbstractComponent || $data instanceof Layout) {
51+
$this->orphanedResourceHelper->handleRemovedRootResource($data);
52+
}
53+
54+
if ($data instanceof ComponentGroup) {
55+
$this->orphanedResourceHelper->handleRemovedComponentGroup($data);
56+
}
57+
58+
if ($data instanceof PageDataInterface) {
59+
$this->orphanedResourceHelper->handleRemovedPageData($data, $resourceClass);
60+
}
61+
62+
if ($data instanceof RoutableInterface) {
63+
$this->orphanedResourceHelper->handleRemovedRoutable($data);
64+
}
65+
}
66+
}

src/EventListener/Api/OrphanedResourceEventListener.php renamed to src/Helper/OrphanedResourceHelper.php

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
<?php
22

3-
/*
4-
* This file is part of the Silverback API Components Bundle Project
5-
*
6-
* (c) Daniel West <daniel@silverback.is>
7-
*
8-
* For the full copyright and license information, please view the LICENSE
9-
* file that was distributed with this source code.
10-
*/
11-
12-
declare(strict_types=1);
13-
14-
namespace Silverback\ApiComponentsBundle\EventListener\Api;
3+
namespace Silverback\ApiComponentsBundle\Helper;
154

165
use Doctrine\Persistence\ManagerRegistry;
176
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
@@ -25,69 +14,51 @@
2514
use Silverback\ApiComponentsBundle\Entity\Core\Route;
2615
use Silverback\ApiComponentsBundle\Metadata\Factory\ComponentUsageMetadataFactory;
2716
use Silverback\ApiComponentsBundle\Metadata\Factory\PageDataMetadataFactoryInterface;
28-
use Symfony\Component\HttpFoundation\Request;
29-
use Symfony\Component\HttpKernel\Event\ViewEvent;
3017
use Symfony\Component\PropertyAccess\PropertyAccess;
3118

32-
/**
33-
* @author Daniel West <daniel@silverback.is>
34-
*/
35-
readonly class OrphanedResourceEventListener {
19+
final class OrphanedResourceHelper
20+
{
3621
public function __construct(
3722
private PageDataMetadataFactoryInterface $pageDataMetadataFactory,
3823
private ComponentUsageMetadataFactory $usageMetadataFactory,
3924
private ManagerRegistry $registry) {
4025
}
4126

42-
public function onPreWrite(ViewEvent $event): void
43-
{
44-
$request = $event->getRequest();
45-
$data = $request->attributes->get('data');
46-
$resourceClass = $request->attributes->get('_api_resource_class');
47-
// only listen for deleted
48-
if (!$request->isMethod(Request::METHOD_DELETE)) {
49-
return;
50-
}
51-
52-
if ($data instanceof ComponentPosition) {
53-
$this->removeOrphanedComponentPosition($data);
54-
return;
55-
}
27+
public function handleRemovedComponentPosition(ComponentPosition $componentPosition): void {
28+
$this->removeOrphanedComponentPosition($componentPosition);
29+
}
5630

57-
if ($data instanceof Page || $data instanceof AbstractComponent || $data instanceof Layout) {
58-
foreach ($data->getComponentGroups() as $componentGroup) {
59-
$this->removeOrphanedComponentGroup($componentGroup, $data);
60-
}
61-
return;
31+
public function handleRemovedRootResource(AbstractComponent|Page|Layout $resource): void {
32+
foreach ($resource->getComponentGroups() as $componentGroup) {
33+
$this->removeOrphanedComponentGroup($componentGroup, $resource);
6234
}
35+
}
6336

64-
if ($data instanceof ComponentGroup) {
65-
$this->removeOrphanedComponentGroup($data);
66-
return;
67-
}
37+
public function handleRemovedComponentGroup(ComponentGroup $componentGroup): void {
38+
$this->removeOrphanedComponentGroup($componentGroup);
39+
}
6840

69-
if ($data instanceof PageDataInterface) {
70-
$propertyAccessor = PropertyAccess::createPropertyAccessor();
71-
$pageDataMetadata = $this->pageDataMetadataFactory->create($resourceClass);
72-
foreach ($pageDataMetadata->getProperties() as $property) {
73-
$component = $propertyAccessor->getValue($data, $property->getProperty());
74-
if ($component instanceof ComponentInterface) {
75-
$this->removeOrphanedComponent($component);
76-
}
41+
public function handleRemovedPageData(PageDataInterface $resource, ?string $resourceClass): void {
42+
$propertyAccessor = PropertyAccess::createPropertyAccessor();
43+
$pageDataMetadata = $this->pageDataMetadataFactory->create($resourceClass ?: get_class($resource));
44+
foreach ($pageDataMetadata->getProperties() as $property) {
45+
$component = $propertyAccessor->getValue($resource, $property->getProperty());
46+
if ($component instanceof ComponentInterface) {
47+
$this->removeOrphanedComponent($component);
7748
}
7849
}
50+
}
7951

80-
if ($data instanceof RoutableInterface) {
81-
$route = $data->getRoute();
82-
if ($route) {
83-
$routeAssociations = 0;
84-
$route->getPage() && $routeAssociations++;
85-
$route->getPageData() && $routeAssociations++;
86-
$route->getRedirect() && $routeAssociations++;
87-
if ($routeAssociations <= 1) {
88-
$manager = $this->registry->getManagerForClass(Route::class);
89-
$manager?->remove($route);
90-
}
52+
public function handleRemovedRoutable(RoutableInterface $resource): void {
53+
$route = $resource->getRoute();
54+
if ($route) {
55+
$routeAssociations = 0;
56+
$route->getPage() && $routeAssociations++;
57+
$route->getPageData() && $routeAssociations++;
58+
$route->getRedirect() && $routeAssociations++;
59+
if ($routeAssociations <= 1) {
60+
$manager = $this->registry->getManagerForClass(Route::class);
61+
$manager?->remove($route);
9162
}
9263
}
9364
}

src/Resources/config/services.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
use Silverback\ApiComponentsBundle\EventListener\Api\ComponentPositionEventListener;
6666
use Silverback\ApiComponentsBundle\EventListener\Api\ComponentUsageEventListener;
6767
use Silverback\ApiComponentsBundle\EventListener\Api\FormApiEventListener;
68-
use Silverback\ApiComponentsBundle\EventListener\Api\OrphanedResourceEventListener;
68+
use Silverback\ApiComponentsBundle\EventListener\Api\DeletedResourceEventListener;
6969
use Silverback\ApiComponentsBundle\EventListener\Api\PublishableEventListener;
7070
use Silverback\ApiComponentsBundle\EventListener\Api\RouteEventListener;
7171
use Silverback\ApiComponentsBundle\EventListener\Api\UploadableEventListener;
@@ -111,6 +111,7 @@
111111
use Silverback\ApiComponentsBundle\Helper\ComponentPosition\ComponentPositionSortValueHelper;
112112
use Silverback\ApiComponentsBundle\Helper\Form\FormCachePurger;
113113
use Silverback\ApiComponentsBundle\Helper\Form\FormSubmitHelper;
114+
use Silverback\ApiComponentsBundle\Helper\OrphanedResourceHelper;
114115
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker;
115116
use Silverback\ApiComponentsBundle\Helper\RefererUrlResolver;
116117
use Silverback\ApiComponentsBundle\Helper\Route\RouteGenerator;
@@ -1391,15 +1392,24 @@
13911392

13921393
$services
13931394
->set('silverback.event_listener.api.orphaned_component')
1394-
->class(OrphanedResourceEventListener::class)
1395+
->class(DeletedResourceEventListener::class)
1396+
->args(
1397+
[
1398+
new Reference('silverback.helper.orphaned_resource_helper'),
1399+
]
1400+
)
1401+
->tag('kernel.event_listener', ['event' => ViewEvent::class, 'priority' => EventPriorities::PRE_WRITE, 'method' => 'onPreWrite']);
1402+
1403+
$services
1404+
->set('silverback.helper.orphaned_resource_helper')
1405+
->class(OrphanedResourceHelper::class)
13951406
->args(
13961407
[
13971408
new Reference('silverback.metadata_factory.page_data'),
13981409
new Reference('silverback.metadata_factory.component_usage'),
13991410
new Reference(ManagerRegistry::class),
14001411
]
1401-
)
1402-
->tag('kernel.event_listener', ['event' => ViewEvent::class, 'priority' => EventPriorities::PRE_WRITE, 'method' => 'onPreWrite']);
1412+
);
14031413

14041414
$services
14051415
->set('silverback.event_listener.api.position_remove')

0 commit comments

Comments
 (0)