From c459612ecc47e48b347159a6715df1e61fa100f6 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Mon, 2 Oct 2023 14:30:02 +0200 Subject: [PATCH] IBX-6644: Gracefully handle broken custom URL aliases --- src/bundle/Resources/config/services.yaml | 2 ++ .../translations/content_url.en.xliff | 4 +-- src/lib/UI/Dataset/CustomUrlsDataset.php | 28 +++++++++++++++++-- src/lib/UI/Dataset/DatasetFactory.php | 14 ++++++++-- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/bundle/Resources/config/services.yaml b/src/bundle/Resources/config/services.yaml index 0a6797521d..5b5238cd36 100644 --- a/src/bundle/Resources/config/services.yaml +++ b/src/bundle/Resources/config/services.yaml @@ -59,6 +59,8 @@ services: lazy: true arguments: $configResolver: '@ezpublish.config.resolver' + calls: + - [setLogger, ['@?logger']] EzSystems\EzPlatformAdminUi\UI\Service\: resource: '../../../lib/UI/Service' diff --git a/src/bundle/Resources/translations/content_url.en.xliff b/src/bundle/Resources/translations/content_url.en.xliff index 961e9bff8c..54e7a0d708 100644 --- a/src/bundle/Resources/translations/content_url.en.xliff +++ b/src/bundle/Resources/translations/content_url.en.xliff @@ -102,8 +102,8 @@ key: tab.urls.modal_close - This Content item has no custom URL aliases. - This Content item has no custom URL aliases. + This Content item has no custom URL aliases or they are broken. + This Content item has no custom URL aliases or they are broken. key: tab.urls.no_custom_urls diff --git a/src/lib/UI/Dataset/CustomUrlsDataset.php b/src/lib/UI/Dataset/CustomUrlsDataset.php index 48543bab8f..0a707d9f91 100644 --- a/src/lib/UI/Dataset/CustomUrlsDataset.php +++ b/src/lib/UI/Dataset/CustomUrlsDataset.php @@ -8,10 +8,12 @@ namespace EzSystems\EzPlatformAdminUi\UI\Dataset; +use eZ\Publish\API\Repository\Exceptions\BadStateException; use eZ\Publish\API\Repository\URLAliasService; use eZ\Publish\API\Repository\Values\Content\Location; use eZ\Publish\API\Repository\Values\Content\URLAlias; use EzSystems\EzPlatformAdminUi\UI\Value\ValueFactory; +use Psr\Log\LoggerInterface; class CustomUrlsDataset { @@ -24,16 +26,21 @@ class CustomUrlsDataset /** @var \EzSystems\EzPlatformAdminUi\UI\Value\Content\UrlAlias[] */ private $data; + /** @var \Psr\Log\LoggerInterface */ + private $logger; + /** * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService * @param \EzSystems\EzPlatformAdminUi\UI\Value\ValueFactory $valueFactory */ public function __construct( URLAliasService $urlAliasService, - ValueFactory $valueFactory + ValueFactory $valueFactory, + LoggerInterface $logger ) { $this->urlAliasService = $urlAliasService; $this->valueFactory = $valueFactory; + $this->logger = $logger; } /** @@ -43,7 +50,24 @@ public function __construct( */ public function load(Location $location): self { - $customUrlAliases = $this->urlAliasService->listLocationAliases($location, true, null, true); + try { + $customUrlAliases = $this->urlAliasService->listLocationAliases( + $location, + true, + null, + true + ); + } catch (BadStateException $e) { + $this->logger->warning( + sprintf( + 'At least one custom alias belonging to location %d is broken. Fix it by using the ezplatform:urls:regenerate-aliases command.', + $location->id + ), + [$e->getMessage()] + ); + $customUrlAliases = []; + } + $this->data = array_map( function (URLAlias $urlAlias) { return $this->valueFactory->createUrlAlias($urlAlias); diff --git a/src/lib/UI/Dataset/DatasetFactory.php b/src/lib/UI/Dataset/DatasetFactory.php index dd9b4989a5..f72d918e47 100644 --- a/src/lib/UI/Dataset/DatasetFactory.php +++ b/src/lib/UI/Dataset/DatasetFactory.php @@ -19,9 +19,15 @@ use eZ\Publish\API\Repository\UserService; use eZ\Publish\Core\MVC\ConfigResolverInterface; use EzSystems\EzPlatformAdminUi\UI\Value\ValueFactory; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; -class DatasetFactory +class DatasetFactory implements LoggerAwareInterface { + use LoggerAwareTrait; + /** @var \eZ\Publish\API\Repository\ContentService */ protected $contentService; @@ -66,7 +72,8 @@ public function __construct( UserService $userService, BookmarkService $bookmarkService, ValueFactory $valueFactory, - ConfigResolverInterface $configResolver + ConfigResolverInterface $configResolver, + ?LoggerInterface $logger = null ) { $this->contentService = $contentService; $this->contentTypeService = $contentTypeService; @@ -79,6 +86,7 @@ public function __construct( $this->bookmarkService = $bookmarkService; $this->valueFactory = $valueFactory; $this->configResolver = $configResolver; + $this->logger = $logger ?? new NullLogger(); } /** @@ -150,7 +158,7 @@ public function objectStates(): ObjectStatesDataset */ public function customUrls(): CustomUrlsDataset { - return new CustomUrlsDataset($this->urlAliasService, $this->valueFactory); + return new CustomUrlsDataset($this->urlAliasService, $this->valueFactory, $this->logger); } /**