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+
314namespace Silverback \ApiComponentsBundle \Helper ;
415
516use Doctrine \Persistence \ManagerRegistry ;
2132 public function __construct (
2233 private PageDataMetadataFactoryInterface $ pageDataMetadataFactory ,
2334 private ComponentUsageMetadataFactory $ usageMetadataFactory ,
24- private ManagerRegistry $ registry ) {
35+ private ManagerRegistry $ registry ,
36+ ) {
2537 }
2638
27- public function handleRemovedComponentPosition (ComponentPosition $ componentPosition ): void {
39+ public function handleRemovedComponentPosition (ComponentPosition $ componentPosition ): void
40+ {
2841 $ this ->removeOrphanedComponentPosition ($ componentPosition );
2942 }
3043
31- public function handleRemovedRootResource (AbstractComponent |Page |Layout $ resource ): void {
44+ public function handleRemovedRootResource (AbstractComponent |Page |Layout $ resource ): void
45+ {
3246 foreach ($ resource ->getComponentGroups () as $ componentGroup ) {
3347 $ this ->removeOrphanedComponentGroup ($ componentGroup , $ resource );
3448 }
3549 }
3650
37- public function handleRemovedComponentGroup (ComponentGroup $ componentGroup ): void {
51+ public function handleRemovedComponentGroup (ComponentGroup $ componentGroup ): void
52+ {
3853 $ this ->removeOrphanedComponentGroup ($ componentGroup );
3954 }
4055
41- public function handleRemovedPageData (PageDataInterface $ resource , ?string $ resourceClass ): void {
56+ public function handleRemovedPageData (PageDataInterface $ resource , ?string $ resourceClass ): void
57+ {
4258 $ propertyAccessor = PropertyAccess::createPropertyAccessor ();
43- $ pageDataMetadata = $ this ->pageDataMetadataFactory ->create ($ resourceClass ?: get_class ( $ resource) );
59+ $ pageDataMetadata = $ this ->pageDataMetadataFactory ->create ($ resourceClass ?: $ resource::class );
4460 foreach ($ pageDataMetadata ->getProperties () as $ property ) {
4561 $ component = $ propertyAccessor ->getValue ($ resource , $ property ->getProperty ());
4662 if ($ component instanceof ComponentInterface) {
@@ -49,7 +65,8 @@ public function handleRemovedPageData(PageDataInterface $resource, ?string $reso
4965 }
5066 }
5167
52- public function handleRemovedRoutable (RoutableInterface $ resource ): void {
68+ public function handleRemovedRoutable (RoutableInterface $ resource ): void
69+ {
5370 $ route = $ resource ->getRoute ();
5471 if ($ route ) {
5572 $ routeAssociations = 0 ;
@@ -63,7 +80,8 @@ public function handleRemovedRoutable(RoutableInterface $resource): void {
6380 }
6481 }
6582
66- public function checkAndRemoveOrphanedComponentGroup (ComponentGroup $ componentGroup ): bool {
83+ public function checkAndRemoveOrphanedComponentGroup (ComponentGroup $ componentGroup ): bool
84+ {
6785 if ($ componentGroup ->pages ->count () || $ componentGroup ->layouts ->count () || $ componentGroup ->components ->count ()) {
6886 return false ;
6987 }
@@ -78,10 +96,12 @@ public function checkAndRemoveOrphanedComponentGroup(ComponentGroup $componentGr
7896 $ groupManager ?->remove($ componentGroup );
7997 $ groupManager ?->flush();
8098 $ positionManager ?->flush();
99+
81100 return true ;
82101 }
83102
84- public function checkAndRemoveOrphanedComponent (AbstractComponent $ component ): bool {
103+ public function checkAndRemoveOrphanedComponent (AbstractComponent $ component ): bool
104+ {
85105 return $ this ->removeOrphanedComponent ($ component , 0 , true );
86106 }
87107
@@ -105,10 +125,12 @@ private function isComponentGroupInOtherLocations(ComponentGroup $componentGroup
105125 return true ;
106126 }
107127 }
128+
108129 return false ;
109130 }
110131
111- private function removeOrphanedComponentGroup (ComponentGroup $ componentGroup , AbstractComponent |Page |Layout |null $ deletedLocation = null ): void {
132+ private function removeOrphanedComponentGroup (ComponentGroup $ componentGroup , AbstractComponent |Page |Layout |null $ deletedLocation = null ): void
133+ {
112134 $ groupExistsElsewhere = $ this ->isComponentGroupInOtherLocations ($ componentGroup , $ deletedLocation );
113135 if ($ groupExistsElsewhere ) {
114136 return ;
@@ -125,7 +147,8 @@ private function removeOrphanedComponentGroup(ComponentGroup $componentGroup, Ab
125147 }
126148 }
127149
128- private function removeOrphanedComponentPosition (ComponentPosition $ componentPosition ): void {
150+ private function removeOrphanedComponentPosition (ComponentPosition $ componentPosition ): void
151+ {
129152 if ($ componentPosition ->component ) {
130153 $ this ->removeOrphanedComponent ($ componentPosition ->component );
131154 }
@@ -135,16 +158,16 @@ private function removeOrphanedComponent(ComponentInterface $component, int $cou
135158 {
136159 $ metadata = $ this ->usageMetadataFactory ->create ($ component );
137160 if ($ countCheck === $ metadata ->getTotal ()) {
138- $ resourceClass = get_class ( $ component) ;
161+ $ resourceClass = $ component::class ;
139162 $ manager = $ this ->registry ->getManagerForClass ($ resourceClass );
140163 $ manager ?->remove($ component );
141164 if ($ doFlush ) {
142165 $ manager ?->flush();
143166 }
167+
144168 return true ;
145169 }
170+
146171 return false ;
147172 }
148-
149-
150173}
0 commit comments