Skip to content

Commit

Permalink
IBX-6644: Gracefully handle broken custom URL aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Oct 2, 2023
1 parent 118c25f commit c459612
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 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'
calls:
- [setLogger, ['@?logger']]

EzSystems\EzPlatformAdminUi\UI\Service\:
resource: '../../../lib/UI/Service'
Expand Down
4 changes: 2 additions & 2 deletions src/bundle/Resources/translations/content_url.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
<note>key: tab.urls.modal_close</note>
</trans-unit>
<trans-unit id="56d218db883ed4cc59dbbca53364dc2e725013e4" resname="tab.urls.no_custom_urls">
<source>This Content item has no custom URL aliases.</source>
<target state="new">This Content item has no custom URL aliases.</target>
<source>This Content item has no custom URL aliases or they are broken.</source>
<target state="new">This Content item has no custom URL aliases or they are broken.</target>
<note>key: tab.urls.no_custom_urls</note>
</trans-unit>
<trans-unit id="c9fdd1434b44a4592626e8fe493c19141aab24d5" resname="tab.urls.no_system_urls">
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
),
[$e->getMessage()]
);
$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 c459612

Please sign in to comment.