Simplified how the not-found redirects are resolved

This commit is contained in:
Alejandro Celaya 2021-09-26 09:40:24 +02:00
parent ce7296eebb
commit 7db6136436
2 changed files with 9 additions and 15 deletions

View File

@ -27,16 +27,9 @@ class NotFoundRedirectHandler implements MiddlewareInterface
/** @var NotFoundType $notFoundType */
$notFoundType = $request->getAttribute(NotFoundType::class);
$authority = $request->getUri()->getAuthority();
$domainSpecificRedirect = $this->resolveDomainSpecificRedirect($authority, $notFoundType);
$redirectConfig = $this->domainService->findByAuthority($authority) ?? $this->redirectOptions;
$redirectResponse = $this->redirectResolver->resolveRedirectResponse($notFoundType, $redirectConfig);
return $domainSpecificRedirect
?? $this->redirectResolver->resolveRedirectResponse($notFoundType, $this->redirectOptions)
?? $handler->handle($request);
}
private function resolveDomainSpecificRedirect(string $authority, NotFoundType $notFoundType): ?ResponseInterface
{
$domain = $this->domainService->findByAuthority($authority);
return $domain === null ? null : $this->redirectResolver->resolveRedirectResponse($notFoundType, $domain);
return $redirectResponse ?? $handler->handle($request);
}
}

View File

@ -72,17 +72,18 @@ class NotFoundRedirectHandlerTest extends TestCase
$domainService->findByAuthority(Argument::cetera())
->willReturn(null)
->shouldBeCalledOnce();
$resolver->resolveRedirectResponse(Argument::cetera())
->willReturn(null)
->shouldBeCalledOnce();
$resolver->resolveRedirectResponse(
Argument::type(NotFoundType::class),
Argument::type(NotFoundRedirectOptions::class),
)->willReturn(null)->shouldBeCalledOnce();
}];
yield 'non-redirecting domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
$domainService->findByAuthority(Argument::cetera())
->willReturn(Domain::withAuthority(''))
->shouldBeCalledOnce();
$resolver->resolveRedirectResponse(Argument::cetera())
$resolver->resolveRedirectResponse(Argument::type(NotFoundType::class), Argument::type(Domain::class))
->willReturn(null)
->shouldBeCalledTimes(2);
->shouldBeCalledOnce();
}];
}