Skip to content

Commit bfba0f7

Browse files
authored
IBX-9727: Improved SiteaccessGuesser after core content VO strict types changes (#92)
For more details see https://issues.ibexa.co/browse/IBX-9727 and #92 Key changes: * Improved SiteaccessGuesser after core content VO strict types changes * [PHPStan] Removed resolved issues from the baseline
1 parent 74e2f91 commit bfba0f7

File tree

2 files changed

+26
-47
lines changed

2 files changed

+26
-47
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -816,30 +816,6 @@ parameters:
816816
count: 1
817817
path: src/lib/Resolver/SelectionFieldResolver.php
818818

819-
-
820-
message: '#^Method Ibexa\\GraphQL\\Resolver\\SiteaccessGuesser\\SiteaccessGuesser\:\:__construct\(\) has parameter \$siteAccessGroups with no value type specified in iterable type array\.$#'
821-
identifier: missingType.iterableValue
822-
count: 1
823-
path: src/lib/Resolver/SiteaccessGuesser/SiteaccessGuesser.php
824-
825-
-
826-
message: '#^Method Ibexa\\GraphQL\\Resolver\\SiteaccessGuesser\\SiteaccessGuesser\:\:guessForLocation\(\) should return Ibexa\\Core\\MVC\\Symfony\\SiteAccess but returns Ibexa\\Core\\MVC\\Symfony\\SiteAccess\|null\.$#'
827-
identifier: return.type
828-
count: 1
829-
path: src/lib/Resolver/SiteaccessGuesser/SiteaccessGuesser.php
830-
831-
-
832-
message: '#^Property Ibexa\\GraphQL\\Resolver\\SiteaccessGuesser\\SiteaccessGuesser\:\:\$siteAccessGroups type has no value type specified in iterable type array\.$#'
833-
identifier: missingType.iterableValue
834-
count: 1
835-
path: src/lib/Resolver/SiteaccessGuesser/SiteaccessGuesser.php
836-
837-
-
838-
message: '#^Variable \$saList in PHPDoc tag @var does not match assigned variable \$matchingSiteaccessRootDepth\.$#'
839-
identifier: varTag.differentVariable
840-
count: 1
841-
path: src/lib/Resolver/SiteaccessGuesser/SiteaccessGuesser.php
842-
843819
-
844820
message: '#^Method Ibexa\\GraphQL\\Resolver\\ThumbnailResolver\:\:resolveThumbnail\(\) return type has no value type specified in iterable type array\.$#'
845821
identifier: missingType.iterableValue

src/lib/Resolver/SiteaccessGuesser/SiteaccessGuesser.php

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ class SiteaccessGuesser
2424

2525
private SiteAccessServiceInterface $siteAccessService;
2626

27+
/** @var array<string, string> */
2728
private array $siteAccessGroups;
2829

30+
/**
31+
* @param array<string, string> $siteAccessGroups
32+
*/
2933
public function __construct(
3034
SiteAccessServiceInterface $siteAccessService,
3135
SiteAccessProviderInterface $provider,
@@ -45,56 +49,55 @@ public function guessForLocation(Location $location): SiteAccess
4549
{
4650
// Test if the location is part of the current tree root, as it is the most likely
4751
$treeRootLocationId = $this->configResolver->getParameter('content.tree_root.location_id');
48-
if ($this->isInSubtree($location, $treeRootLocationId)) {
49-
return $this->siteAccessService->getCurrent();
52+
if ($this->getDepthInSubtree($location, $treeRootLocationId) !== false) {
53+
return $this->siteAccessService->getCurrent() ?? throw new NoValidSiteaccessException($location);
5054
}
5155

52-
// we won't look into siteaccesses that don't use the same repository
56+
// we won't look into SiteAccess-es that don't use the same repository
5357
$currentRepository = $this->configResolver->getParameter('repository');
5458

59+
$matchingSiteAccessRootDepth = 0;
5560
/** @var \Ibexa\Core\MVC\Symfony\SiteAccess[] $saList */
56-
$matchingSiteaccessRootDepth = 0;
5761
$saList = iterator_to_array($this->provider->getSiteAccesses());
5862

59-
foreach ($saList as $siteaccess) {
60-
$repository = $this->configResolver->getParameter('repository', 'ibexa.site_access.config', $siteaccess->name);
61-
62-
if ($repository !== $currentRepository) {
63-
continue;
64-
}
63+
foreach ($saList as $siteAccess) {
64+
$repository = $this->configResolver->getParameter('repository', 'ibexa.site_access.config', $siteAccess->name);
6565

66-
if ($this->isAdminSiteaccess($siteaccess)) {
66+
if ($repository !== $currentRepository || $this->isAdminSiteAccess($siteAccess)) {
6767
continue;
6868
}
6969

70-
$treeRootLocationId = $this->configResolver->getParameter('content.tree_root.location_id', 'ibexa.site_access.config', $siteaccess->name);
71-
if (($rootDepth = $this->isInSubtree($location, $treeRootLocationId)) !== false) {
72-
if ($rootDepth > $matchingSiteaccessRootDepth) {
73-
$matchingSiteaccess = $siteaccess;
74-
$matchingSiteaccessRootDepth = $rootDepth;
75-
}
70+
$treeRootLocationId = $this->configResolver->getParameter(
71+
'content.tree_root.location_id',
72+
'ibexa.site_access.config',
73+
$siteAccess->name
74+
);
75+
$rootDepth = $this->getDepthInSubtree($location, $treeRootLocationId);
76+
if ($rootDepth !== false && $rootDepth > $matchingSiteAccessRootDepth) {
77+
$matchingSiteAccess = $siteAccess;
78+
$matchingSiteAccessRootDepth = $rootDepth;
7679
}
7780
}
7881

79-
if (!isset($matchingSiteaccess)) {
82+
if (!isset($matchingSiteAccess)) {
8083
throw new NoValidSiteaccessException($location);
8184
}
8285

83-
return $matchingSiteaccess;
86+
return $matchingSiteAccess;
8487
}
8588

8689
/**
8790
* Tests if $location is part of a subtree, and returns the root depth.
8891
*
8992
* @return int|false The root depth (used to select the deepest, most specific tree root), false if it isn't part of that subtree.
9093
*/
91-
private function isInSubtree(Location $location, int $treeRootLocationId): int|string|false
94+
private function getDepthInSubtree(Location $location, int $treeRootLocationId): int|false
9295
{
93-
return array_search($treeRootLocationId, $location->path);
96+
return array_search($treeRootLocationId, array_map('intval', $location->getPath()), true);
9497
}
9598

96-
private function isAdminSiteaccess(SiteAccess $siteaccess): bool
99+
private function isAdminSiteAccess(SiteAccess $siteAccess): bool
97100
{
98-
return (new IsAdmin($this->siteAccessGroups))->isSatisfiedBy($siteaccess);
101+
return (new IsAdmin($this->siteAccessGroups))->isSatisfiedBy($siteAccess);
99102
}
100103
}

0 commit comments

Comments
 (0)