#867 Small refactoring on NotFoundRedirecthandler

This commit is contained in:
Alejandro Celaya 2020-11-10 17:30:06 +01:00
parent 4dbcf6857e
commit deeca582db

View file

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ErrorHandler;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\RedirectResponse;
use Mezzio\Router\RouteResult;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
@ -12,17 +12,19 @@ use Psr\Http\Message\UriInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Action\RedirectAction;
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
use Shlinkio\Shlink\Core\Options;
use function rtrim;
class NotFoundRedirectHandler implements MiddlewareInterface
{
private NotFoundRedirectOptions $redirectOptions;
private Options\NotFoundRedirectOptions $redirectOptions;
private string $shlinkBasePath;
public function __construct(NotFoundRedirectOptions $redirectOptions, string $shlinkBasePath)
{
public function __construct(
Options\NotFoundRedirectOptions $redirectOptions,
string $shlinkBasePath
) {
$this->redirectOptions = $redirectOptions;
$this->shlinkBasePath = $shlinkBasePath;
}
@ -41,11 +43,11 @@ class NotFoundRedirectHandler implements MiddlewareInterface
$isBaseUrl = rtrim($uri->getPath(), '/') === $this->shlinkBasePath;
if ($isBaseUrl && $this->redirectOptions->hasBaseUrlRedirect()) {
return new Response\RedirectResponse($this->redirectOptions->getBaseUrlRedirect());
return new RedirectResponse($this->redirectOptions->getBaseUrlRedirect());
}
if (!$isBaseUrl && $routeResult->isFailure() && $this->redirectOptions->hasRegular404Redirect()) {
return new Response\RedirectResponse($this->redirectOptions->getRegular404Redirect());
return new RedirectResponse($this->redirectOptions->getRegular404Redirect());
}
if (
@ -53,7 +55,7 @@ class NotFoundRedirectHandler implements MiddlewareInterface
$routeResult->getMatchedRouteName() === RedirectAction::class &&
$this->redirectOptions->hasInvalidShortUrlRedirect()
) {
return new Response\RedirectResponse($this->redirectOptions->getInvalidShortUrlRedirect());
return new RedirectResponse($this->redirectOptions->getInvalidShortUrlRedirect());
}
return null;