From c0c61b993b33795cdbbf12f173da27daeee9b750 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Mon, 18 Sep 2023 15:11:25 +0200 Subject: [PATCH] IBX-6589: Added optional parameter siteAccessList to SiteaccessResolver --- .../Siteaccess/NonAdminSiteaccessResolver.php | 20 ++++++++--- src/lib/Siteaccess/SiteaccessResolver.php | 33 +++++++++++++------ .../SiteaccessResolverInterface.php | 12 +++++-- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/lib/Siteaccess/NonAdminSiteaccessResolver.php b/src/lib/Siteaccess/NonAdminSiteaccessResolver.php index 94eae3fa04..913d7d96ef 100644 --- a/src/lib/Siteaccess/NonAdminSiteaccessResolver.php +++ b/src/lib/Siteaccess/NonAdminSiteaccessResolver.php @@ -46,23 +46,33 @@ public function getSiteaccessesForLocation( } /** + * @param \Ibexa\Core\MVC\Symfony\SiteAccess[] $siteAccessList + * * @return \Ibexa\Core\MVC\Symfony\SiteAccess[] */ public function getSiteAccessesListForLocation( Location $location, ?int $versionNo = null, - ?string $languageCode = null + ?string $languageCode = null, + array $siteAccessList = [] ): array { return array_filter( - $this->siteaccessResolver->getSiteAccessesListForLocation($location, $versionNo, $languageCode), + $this->siteaccessResolver->getSiteAccessesListForLocation( + $location, + $versionNo, + $languageCode, + $siteAccessList + ), fn (SiteAccess $siteAccess): bool => !$this->isAdminSiteAccess($siteAccess) ); } - public function getSiteAccessesListForContent(Content $content): array - { + public function getSiteAccessesListForContent( + Content $content, + array $siteAccessList = [] + ): array { return array_filter( - $this->siteaccessResolver->getSiteAccessesListForContent($content), + $this->siteaccessResolver->getSiteAccessesListForContent($content, $siteAccessList), fn (SiteAccess $siteAccess): bool => !$this->isAdminSiteAccess($siteAccess) ); } diff --git a/src/lib/Siteaccess/SiteaccessResolver.php b/src/lib/Siteaccess/SiteaccessResolver.php index cefa3f312f..9436b38e7b 100644 --- a/src/lib/Siteaccess/SiteaccessResolver.php +++ b/src/lib/Siteaccess/SiteaccessResolver.php @@ -71,21 +71,17 @@ public function getSiteaccessesForLocation( return array_column($this->getSiteAccessesListForLocation($location, $versionNo, $languageCode), 'name'); } - /** - * @return \Ibexa\Core\MVC\Symfony\SiteAccess[] - */ public function getSiteAccessesListForLocation( Location $location, ?int $versionNo = null, - ?string $languageCode = null + ?string $languageCode = null, + array $siteAccessList = [] ): array { $contentInfo = $location->getContentInfo(); $versionInfo = $this->contentService->loadVersionInfo($contentInfo, $versionNo); $languageCode = $languageCode ?? $contentInfo->mainLanguageCode; - $eligibleSiteAccesses = []; - /** @var \Ibexa\Core\MVC\Symfony\SiteAccess $siteAccess */ - foreach ($this->siteAccessService->getAll() as $siteAccess) { + foreach ($this->getSiteAccessListToCheck($siteAccessList) as $siteAccess) { $context = new SiteaccessPreviewVoterContext($location, $versionInfo, $siteAccess->name, $languageCode); foreach ($this->siteAccessPreviewVoters as $siteAccessPreviewVoter) { if ($siteAccessPreviewVoter->vote($context)) { @@ -98,8 +94,10 @@ public function getSiteAccessesListForLocation( return $eligibleSiteAccesses; } - public function getSiteAccessesListForContent(Content $content): array - { + public function getSiteAccessesListForContent( + Content $content, + array $siteAccessList = [] + ): array { $versionInfo = $content->getVersionInfo(); $contentInfo = $versionInfo->getContentInfo(); @@ -116,7 +114,12 @@ public function getSiteAccessesListForContent(Content $content): array foreach ($eligibleLocations as $location) { foreach ($eligibleLanguages as $language) { $siteAccesses = array_merge( - $this->getSiteAccessesListForLocation($location, null, $language->languageCode), + $this->getSiteAccessesListForLocation( + $location, + null, + $language->languageCode, + $siteAccessList + ), $siteAccesses ); } @@ -137,6 +140,16 @@ public function getSiteAccessesList(): array { return iterator_to_array($this->siteAccessService->getAll()); } + + /** + * @param array<\Ibexa\Core\MVC\Symfony\SiteAccess> $siteAccessList + * + * @return iterable<\Ibexa\Core\MVC\Symfony\SiteAccess> + */ + private function getSiteAccessListToCheck(array $siteAccessList = []): iterable + { + return !empty($siteAccessList) ? $siteAccessList : $this->siteAccessService->getAll(); + } } class_alias(SiteaccessResolver::class, 'EzSystems\EzPlatformAdminUi\Siteaccess\SiteaccessResolver'); diff --git a/src/lib/Siteaccess/SiteaccessResolverInterface.php b/src/lib/Siteaccess/SiteaccessResolverInterface.php index bdc209d031..8873815ad4 100644 --- a/src/lib/Siteaccess/SiteaccessResolverInterface.php +++ b/src/lib/Siteaccess/SiteaccessResolverInterface.php @@ -32,18 +32,26 @@ public function getSiteaccessesForLocation( ): array; /** + * @param \Ibexa\Core\MVC\Symfony\SiteAccess[] $siteAccessList + * * @return \Ibexa\Core\MVC\Symfony\SiteAccess[] */ public function getSiteAccessesListForLocation( Location $location, ?int $versionNo = null, - ?string $languageCode = null + ?string $languageCode = null, + array $siteAccessList = [] ): array; /** + * @param \Ibexa\Core\MVC\Symfony\SiteAccess[] $siteAccessList + * * @return \Ibexa\Core\MVC\Symfony\SiteAccess[] */ - public function getSiteAccessesListForContent(Content $content): array; + public function getSiteAccessesListForContent( + Content $content, + array $siteAccessList = [] + ): array; /** * @return \Ibexa\Core\MVC\Symfony\SiteAccess[]