|
1 | 1 | <?php |
2 | 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; |
| 3 | +namespace Silverback\ApiComponentsBundle\Helper; |
15 | 4 |
|
16 | 5 | use Doctrine\Persistence\ManagerRegistry; |
17 | 6 | use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent; |
|
25 | 14 | use Silverback\ApiComponentsBundle\Entity\Core\Route; |
26 | 15 | use Silverback\ApiComponentsBundle\Metadata\Factory\ComponentUsageMetadataFactory; |
27 | 16 | use Silverback\ApiComponentsBundle\Metadata\Factory\PageDataMetadataFactoryInterface; |
28 | | -use Symfony\Component\HttpFoundation\Request; |
29 | | -use Symfony\Component\HttpKernel\Event\ViewEvent; |
30 | 17 | use Symfony\Component\PropertyAccess\PropertyAccess; |
31 | 18 |
|
32 | | -/** |
33 | | - * @author Daniel West <daniel@silverback.is> |
34 | | - */ |
35 | | -readonly class OrphanedResourceEventListener { |
| 19 | +final class OrphanedResourceHelper |
| 20 | +{ |
36 | 21 | public function __construct( |
37 | 22 | private PageDataMetadataFactoryInterface $pageDataMetadataFactory, |
38 | 23 | private ComponentUsageMetadataFactory $usageMetadataFactory, |
39 | 24 | private ManagerRegistry $registry) { |
40 | 25 | } |
41 | 26 |
|
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 | + } |
56 | 30 |
|
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); |
62 | 34 | } |
| 35 | + } |
63 | 36 |
|
64 | | - if ($data instanceof ComponentGroup) { |
65 | | - $this->removeOrphanedComponentGroup($data); |
66 | | - return; |
67 | | - } |
| 37 | + public function handleRemovedComponentGroup(ComponentGroup $componentGroup): void { |
| 38 | + $this->removeOrphanedComponentGroup($componentGroup); |
| 39 | + } |
68 | 40 |
|
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); |
77 | 48 | } |
78 | 49 | } |
| 50 | + } |
79 | 51 |
|
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); |
91 | 62 | } |
92 | 63 | } |
93 | 64 | } |
|
0 commit comments