77namespace Ibexa \Rest \Server \Output \ValueObjectVisitor ;
88
99use Ibexa \Contracts \Core \Repository \ContentService ;
10+ use Ibexa \Contracts \Core \Repository \Exceptions \UnauthorizedException ;
1011use Ibexa \Contracts \Core \Repository \LocationService ;
11- use Ibexa \Contracts \Core \Repository \Values \Content \ Location as LocationValue ;
12+ use Ibexa \Contracts \Core \Repository \Values \Content ;
1213use Ibexa \Contracts \Rest \Output \Generator ;
1314use Ibexa \Contracts \Rest \Output \ValueObjectVisitor ;
1415use Ibexa \Contracts \Rest \Output \Visitor ;
15- use Ibexa \Core \Base \Exceptions \UnauthorizedException ;
1616use Ibexa \Rest \Server \Values \RestContent as RestContentValue ;
1717
1818/**
@@ -26,8 +26,10 @@ class Location extends ValueObjectVisitor
2626 /** @var \Ibexa\Contracts\Core\Repository\ContentService */
2727 private $ contentService ;
2828
29- public function __construct (LocationService $ locationService , ContentService $ contentService )
30- {
29+ public function __construct (
30+ LocationService $ locationService ,
31+ ContentService $ contentService
32+ ) {
3133 $ this ->locationService = $ locationService ;
3234 $ this ->contentService = $ contentService ;
3335 }
@@ -48,8 +50,15 @@ public function visit(Visitor $visitor, Generator $generator, $location)
4850 $ generator ->endObjectElement ('Location ' );
4951 }
5052
51- protected function visitLocationAttributes (Visitor $ visitor , Generator $ generator , LocationValue $ location )
52- {
53+ /**
54+ * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
55+ * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
56+ */
57+ protected function visitLocationAttributes (
58+ Visitor $ visitor ,
59+ Generator $ generator ,
60+ Content \Location $ location
61+ ) {
5362 $ generator ->startAttribute (
5463 'href ' ,
5564 $ this ->router ->generate (
@@ -153,15 +162,8 @@ protected function visitLocationAttributes(Visitor $visitor, Generator $generato
153162 $ generator ->endAttribute ('href ' );
154163
155164 $ content = $ location ->getContent ();
156- $ contentInfo = $ location ->contentInfo ;
157-
158- try {
159- $ mainLocation = $ contentInfo ->mainLocationId === $ location ->id
160- ? $ location
161- : $ this ->locationService ->loadLocation ($ contentInfo ->mainLocationId );
162- } catch (UnauthorizedException $ e ) {
163- $ mainLocation = null ;
164- }
165+ $ contentInfo = $ location ->getContentInfo ();
166+ $ mainLocation = $ this ->resolveMainLocation ($ contentInfo , $ location );
165167
166168 $ visitor ->visitValueObject (
167169 new RestContentValue (
@@ -175,6 +177,29 @@ protected function visitLocationAttributes(Visitor $visitor, Generator $generato
175177
176178 $ generator ->endObjectElement ('ContentInfo ' );
177179 }
180+
181+ /**
182+ * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
183+ */
184+ private function resolveMainLocation (
185+ Content \ContentInfo $ contentInfo ,
186+ Content \Location $ location
187+ ): ?Content \Location {
188+ $ mainLocationId = $ contentInfo ->getMainLocationId ();
189+ if ($ mainLocationId === null ) {
190+ return null ;
191+ }
192+
193+ if ($ mainLocationId === $ location ->id ) {
194+ return $ location ;
195+ }
196+
197+ try {
198+ return $ this ->locationService ->loadLocation ($ mainLocationId );
199+ } catch (UnauthorizedException $ e ) {
200+ return null ;
201+ }
202+ }
178203}
179204
180205class_alias (Location::class, 'EzSystems\EzPlatformRest\Server\Output\ValueObjectVisitor\Location ' );
0 commit comments