mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
#867 Small refactoring on NotFoundRedirecthandler
This commit is contained in:
parent
4dbcf6857e
commit
deeca582db
@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\Core\ErrorHandler;
|
namespace Shlinkio\Shlink\Core\ErrorHandler;
|
||||||
|
|
||||||
use Laminas\Diactoros\Response;
|
use Laminas\Diactoros\Response\RedirectResponse;
|
||||||
use Mezzio\Router\RouteResult;
|
use Mezzio\Router\RouteResult;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
@ -12,17 +12,19 @@ use Psr\Http\Message\UriInterface;
|
|||||||
use Psr\Http\Server\MiddlewareInterface;
|
use Psr\Http\Server\MiddlewareInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
use Shlinkio\Shlink\Core\Action\RedirectAction;
|
use Shlinkio\Shlink\Core\Action\RedirectAction;
|
||||||
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
use Shlinkio\Shlink\Core\Options;
|
||||||
|
|
||||||
use function rtrim;
|
use function rtrim;
|
||||||
|
|
||||||
class NotFoundRedirectHandler implements MiddlewareInterface
|
class NotFoundRedirectHandler implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
private NotFoundRedirectOptions $redirectOptions;
|
private Options\NotFoundRedirectOptions $redirectOptions;
|
||||||
private string $shlinkBasePath;
|
private string $shlinkBasePath;
|
||||||
|
|
||||||
public function __construct(NotFoundRedirectOptions $redirectOptions, string $shlinkBasePath)
|
public function __construct(
|
||||||
{
|
Options\NotFoundRedirectOptions $redirectOptions,
|
||||||
|
string $shlinkBasePath
|
||||||
|
) {
|
||||||
$this->redirectOptions = $redirectOptions;
|
$this->redirectOptions = $redirectOptions;
|
||||||
$this->shlinkBasePath = $shlinkBasePath;
|
$this->shlinkBasePath = $shlinkBasePath;
|
||||||
}
|
}
|
||||||
@ -41,11 +43,11 @@ class NotFoundRedirectHandler implements MiddlewareInterface
|
|||||||
$isBaseUrl = rtrim($uri->getPath(), '/') === $this->shlinkBasePath;
|
$isBaseUrl = rtrim($uri->getPath(), '/') === $this->shlinkBasePath;
|
||||||
|
|
||||||
if ($isBaseUrl && $this->redirectOptions->hasBaseUrlRedirect()) {
|
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()) {
|
if (!$isBaseUrl && $routeResult->isFailure() && $this->redirectOptions->hasRegular404Redirect()) {
|
||||||
return new Response\RedirectResponse($this->redirectOptions->getRegular404Redirect());
|
return new RedirectResponse($this->redirectOptions->getRegular404Redirect());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -53,7 +55,7 @@ class NotFoundRedirectHandler implements MiddlewareInterface
|
|||||||
$routeResult->getMatchedRouteName() === RedirectAction::class &&
|
$routeResult->getMatchedRouteName() === RedirectAction::class &&
|
||||||
$this->redirectOptions->hasInvalidShortUrlRedirect()
|
$this->redirectOptions->hasInvalidShortUrlRedirect()
|
||||||
) {
|
) {
|
||||||
return new Response\RedirectResponse($this->redirectOptions->getInvalidShortUrlRedirect());
|
return new RedirectResponse($this->redirectOptions->getInvalidShortUrlRedirect());
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
Loading…
Reference in New Issue
Block a user