Skip to content

Commit

Permalink
Use NULL instead of ContentUrlResult::abstain()
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jan 17, 2024
1 parent ba24a4a commit 9b529db
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 36 deletions.
4 changes: 2 additions & 2 deletions core-bundle/src/Routing/Content/ArticleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public function __construct(private readonly ContaoFramework $framework)
{
}

public function resolve(object $content): ContentUrlResult
public function resolve(object $content): ContentUrlResult|null
{
if (!$content instanceof ArticleModel) {
return ContentUrlResult::abstain();
return null;
}

$pageAdapter = $this->framework->getAdapter(PageModel::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ interface ContentUrlResolverInterface
{
/**
* Returns a result for resolving the given content.
* - ContentUrlResult::abstain() if you cannot handle the content.
* - NULL if you cannot handle the content.
* - ContentUrlResult::url() if the content has a URL string that could be relative or contain insert tags.
* - ContentUrlResult::redirect() to generate the URL for a new content instead of the current one.
* - ContentUrlResult::resolve() to generate the URL for the given PageModel with the current content.
*/
public function resolve(object $content): ContentUrlResult;
public function resolve(object $content): ContentUrlResult|null;

/**
* Returns an array of parameters for the given content that can be used
Expand Down
13 changes: 0 additions & 13 deletions core-bundle/src/Routing/Content/ContentUrlResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public function __construct(public readonly object|string|null $content)
}
}

public function isAbstained(): bool
{
return null === $this->content;
}

public function isRedirect(): bool
{
return $this->redirect;
Expand All @@ -54,14 +49,6 @@ public function getTargetUrl(): string
return $this->content;
}

/**
* Provides no result to continue the resolver loop.
*/
public static function abstain(): self
{
return new self(null);
}

/**
* Result is a URL string which is possibly relative and could contain insert tags that should be resolved.
*/
Expand Down
6 changes: 3 additions & 3 deletions core-bundle/src/Routing/Content/PageResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public function __construct(private readonly ContaoFramework $framework)
{
}

public function resolve(object $content): ContentUrlResult
public function resolve(object $content): ContentUrlResult|null
{
if (!$content instanceof PageModel) {
return ContentUrlResult::abstain();
return null;
}

switch ($content->type) {
Expand All @@ -43,7 +43,7 @@ public function resolve(object $content): ContentUrlResult
return ContentUrlResult::redirect($forwardPage);
}

return ContentUrlResult::abstain();
return null;
}

public function getParametersForContent(object $content, PageModel $pageModel): array
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Routing/Content/StringResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function __construct(
) {
}

public function resolve(object $content): ContentUrlResult
public function resolve(object $content): ContentUrlResult|null
{
if (!$content instanceof StringUrl) {
return ContentUrlResult::abstain();
return null;
}

$url = $this->insertTagParser->replaceInline($content->value);
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Routing/ContentUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function resolveContent(object ...$contents): array
foreach ($this->urlResolvers as $resolver) {
$result = $resolver->resolve($contents[0]);

if ($result->isAbstained()) {
if (!$result) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion core-bundle/tests/Routing/Content/ArticleResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testAbstainsIfContentIsNotAnArticleModel(): void
$resolver = new ArticleResolver($this->mockContaoFramework());
$result = $resolver->resolve($content);

$this->assertTrue($result->isAbstained());
$this->assertNull($result);
}

public function testResolvesArticleModel(): void
Expand Down
8 changes: 4 additions & 4 deletions core-bundle/tests/Routing/Content/PageResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testAbstainsIfContentIsNotAPageModel(): void
$resolver = new PageResolver($this->mockContaoFramework());
$result = $resolver->resolve($content);

$this->assertTrue($result->isAbstained());
$this->assertNull($result);
}

public function testAbstainsIfContentPageModelIsNotRedirectOrForward(): void
Expand All @@ -36,15 +36,15 @@ public function testAbstainsIfContentPageModelIsNotRedirectOrForward(): void

$content = $this->mockClassWithProperties(PageModel::class, ['type' => 'regular']);
$result = $resolver->resolve($content);
$this->assertTrue($result->isAbstained());
$this->assertNull($result);

$content = $this->mockClassWithProperties(PageModel::class, ['type' => 'root']);
$result = $resolver->resolve($content);
$this->assertTrue($result->isAbstained());
$this->assertNull($result);

$content = $this->mockClassWithProperties(PageModel::class, ['type' => 'foobar']);
$result = $resolver->resolve($content);
$this->assertTrue($result->isAbstained());
$this->assertNull($result);
}

public function testReturnsRedirectUrl(): void
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/tests/Routing/Content/StringResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testAbstainsIfContentIsNotAStringUrl(): void
$resolver = new StringResolver($insertTagParser, $urlHelper);
$result = $resolver->resolve($content);

$this->assertTrue($result->isAbstained());
$this->assertNull($result);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions core-bundle/tests/Routing/ContentUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testRedirectsPageToAnotherPage(): void

$resolver = $this->mockResolver(
[$pageModel1, ContentUrlResult::redirect($pageModel2)],
[$pageModel2, ContentUrlResult::abstain()],
[$pageModel2, null],
);

$service = new ContentUrlGenerator($urlGenerator, $pageRegistry, $entityManager, [$resolver]);
Expand All @@ -81,7 +81,7 @@ public function testGeneratesParametersForPageModel(): void
$entityManager = $this->createMock(EntityManagerInterface::class);

$resolver = $this->mockResolver(
[$pageModel2, ContentUrlResult::abstain()],
[$pageModel2, null],
);

$service = new ContentUrlGenerator($urlGenerator, $pageRegistry, $entityManager, [$resolver]);
Expand All @@ -101,7 +101,7 @@ public function testIgnoresParametersOnRedirect(): void

$resolver = $this->mockResolver(
[$pageModel1, ContentUrlResult::redirect($pageModel2)],
[$pageModel2, ContentUrlResult::abstain()],
[$pageModel2, null],
);

$service = new ContentUrlGenerator($urlGenerator, $pageRegistry, $entityManager, [$resolver]);
Expand Down Expand Up @@ -175,7 +175,7 @@ public function testResolvesFromMultipleResolvers(): void

$pageResolver = $this->mockResolver(
[$content, ContentUrlResult::url('https://example.net')],
[$this->isInstanceOf(StringUrl::class), ContentUrlResult::abstain()],
[$this->isInstanceOf(StringUrl::class), null],
);

$stringResolver = $this->mockResolver(
Expand All @@ -200,7 +200,7 @@ public function testGeneratesContentUrl(): void

$resolver = $this->mockResolver(
[$content, ContentUrlResult::resolve($target)],
[$target, ContentUrlResult::abstain()],
[$target, null],
);

$service = new ContentUrlGenerator($urlGenerator, $pageRegistry, $entityManager, [$resolver]);
Expand All @@ -223,7 +223,7 @@ public function testFetchesContentParametersFromResolvers(): void

$resolver = $this->mockResolver(
[$content, ContentUrlResult::resolve($target)],
[$target, ContentUrlResult::abstain()],
[$target, null],
);

$resolver
Expand Down Expand Up @@ -252,7 +252,7 @@ public function testIgnoresUnknownContentParametersFromResolvers(): void

$resolver = $this->mockResolver(
[$content, ContentUrlResult::resolve($target)],
[$target, ContentUrlResult::abstain()],
[$target, null],
);

$resolver
Expand Down

0 comments on commit 9b529db

Please sign in to comment.