Skip to content

Commit

Permalink
IBX-6644: Gracefully handle broken custom URL aliases (#2111)
Browse files Browse the repository at this point in the history
* IBX-6644: Gracefully handle broken custom URL aliases

* IBX-6644: Template update

* IBX-6644: Applied review remarks
  • Loading branch information
barw4 committed Oct 5, 2023
1 parent 118c25f commit f721921
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ services:
lazy: true
arguments:
$configResolver: '@ezpublish.config.resolver'
tags:
- { name: 'monolog.logger', channel: 'ibexa.admin_ui' }

EzSystems\EzPlatformAdminUi\UI\Service\:
resource: '../../../lib/UI/Service'
Expand Down
28 changes: 26 additions & 2 deletions src/lib/UI/Dataset/CustomUrlsDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}

/**
Expand All @@ -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
),
['exception' => $e]
);
$customUrlAliases = [];
}

$this->data = array_map(
function (URLAlias $urlAlias) {
return $this->valueFactory->createUrlAlias($urlAlias);
Expand Down
14 changes: 11 additions & 3 deletions src/lib/UI/Dataset/DatasetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -79,6 +86,7 @@ public function __construct(
$this->bookmarkService = $bookmarkService;
$this->valueFactory = $valueFactory;
$this->configResolver = $configResolver;
$this->logger = $logger ?? new NullLogger();
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit f721921

Please sign in to comment.