mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Defined new options for new redirect configuration
This commit is contained in:
@@ -20,7 +20,7 @@ return [
|
||||
|
||||
Options\AppOptions::class => ConfigAbstractFactory::class,
|
||||
Options\DeleteShortUrlsOptions::class => ConfigAbstractFactory::class,
|
||||
Options\NotFoundShortUrlOptions::class => ConfigAbstractFactory::class,
|
||||
Options\NotFoundRedirectOptions::class => ConfigAbstractFactory::class,
|
||||
Options\UrlShortenerOptions::class => ConfigAbstractFactory::class,
|
||||
|
||||
Service\UrlShortener::class => ConfigAbstractFactory::class,
|
||||
@@ -44,7 +44,7 @@ return [
|
||||
|
||||
Options\AppOptions::class => ['config.app_options'],
|
||||
Options\DeleteShortUrlsOptions::class => ['config.delete_short_urls'],
|
||||
Options\NotFoundShortUrlOptions::class => ['config.url_shortener.not_found_short_url'],
|
||||
Options\NotFoundRedirectOptions::class => ['config.not_found_redirects'],
|
||||
Options\UrlShortenerOptions::class => ['config.url_shortener'],
|
||||
|
||||
Service\UrlShortener::class => ['httpClient', 'em', Options\UrlShortenerOptions::class],
|
||||
@@ -58,7 +58,7 @@ return [
|
||||
Service\UrlShortener::class,
|
||||
Service\VisitsTracker::class,
|
||||
Options\AppOptions::class,
|
||||
Options\NotFoundShortUrlOptions::class,
|
||||
Options\NotFoundRedirectOptions::class,
|
||||
'Logger_Shlink',
|
||||
],
|
||||
Action\PixelAction::class => [
|
||||
|
||||
@@ -18,18 +18,18 @@ class RedirectAction extends AbstractTrackingAction
|
||||
{
|
||||
use ErrorResponseBuilderTrait;
|
||||
|
||||
/** @var Options\NotFoundShortUrlOptions */
|
||||
private $notFoundOptions;
|
||||
/** @var Options\NotFoundRedirectOptions */
|
||||
private $redirectOptions;
|
||||
|
||||
public function __construct(
|
||||
UrlShortenerInterface $urlShortener,
|
||||
VisitsTrackerInterface $visitTracker,
|
||||
Options\AppOptions $appOptions,
|
||||
Options\NotFoundShortUrlOptions $notFoundOptions,
|
||||
Options\NotFoundRedirectOptions $redirectOptions,
|
||||
?LoggerInterface $logger = null
|
||||
) {
|
||||
parent::__construct($urlShortener, $visitTracker, $appOptions, $logger);
|
||||
$this->notFoundOptions = $notFoundOptions;
|
||||
$this->redirectOptions = $redirectOptions;
|
||||
}
|
||||
|
||||
protected function createSuccessResp(string $longUrl): Response
|
||||
@@ -43,8 +43,8 @@ class RedirectAction extends AbstractTrackingAction
|
||||
ServerRequestInterface $request,
|
||||
RequestHandlerInterface $handler
|
||||
): Response {
|
||||
if ($this->notFoundOptions->isRedirectionEnabled()) {
|
||||
return new RedirectResponse($this->notFoundOptions->getRedirectTo());
|
||||
if ($this->redirectOptions->hasInvalidShortUrlRedirect()) {
|
||||
return new RedirectResponse($this->redirectOptions->getInvalidShortUrlRedirect());
|
||||
}
|
||||
|
||||
return $this->buildErrorResponse($request, $handler);
|
||||
|
||||
@@ -24,7 +24,7 @@ class SimplifiedConfigParser
|
||||
'validate_url' => ['url_shortener', 'validate_url'],
|
||||
'not_found_redirect_to' => ['not_found_redirects', 'invalid_short_url'], // Deprecated
|
||||
'invalid_short_url_redirect_to' => ['not_found_redirects', 'invalid_short_url'],
|
||||
'404_redirect_to' => ['not_found_redirects', '404'],
|
||||
'regular_404_redirect_to' => ['not_found_redirects', 'regular_404'],
|
||||
'base_url_redirect_to' => ['not_found_redirects', 'base_path'],
|
||||
'db_config' => ['entity_manager', 'connection'],
|
||||
'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
|
||||
|
||||
65
module/Core/src/Options/NotFoundRedirectOptions.php
Normal file
65
module/Core/src/Options/NotFoundRedirectOptions.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Options;
|
||||
|
||||
use Zend\Stdlib\AbstractOptions;
|
||||
|
||||
class NotFoundRedirectOptions extends AbstractOptions
|
||||
{
|
||||
/** @var string|null */
|
||||
private $invalidShortUrl;
|
||||
/** @var string|null */
|
||||
private $regular404;
|
||||
/** @var string|null */
|
||||
private $baseUrl;
|
||||
|
||||
public function getInvalidShortUrlRedirect(): ?string
|
||||
{
|
||||
return $this->invalidShortUrl;
|
||||
}
|
||||
|
||||
public function hasInvalidShortUrlRedirect(): bool
|
||||
{
|
||||
return $this->invalidShortUrl !== null;
|
||||
}
|
||||
|
||||
protected function setInvalidShortUrl(?string $invalidShortUrl): self
|
||||
{
|
||||
$this->invalidShortUrl = $invalidShortUrl;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRegular404Redirect(): ?string
|
||||
{
|
||||
return $this->regular404;
|
||||
}
|
||||
|
||||
public function hasRegular404Redirect(): bool
|
||||
{
|
||||
return $this->regular404 !== null;
|
||||
}
|
||||
|
||||
protected function setRegular404(?string $regular404): self
|
||||
{
|
||||
$this->regular404 = $regular404;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBaseUrlRedirect(): ?string
|
||||
{
|
||||
return $this->baseUrl;
|
||||
}
|
||||
|
||||
public function hasBaseUrlRedirect(): bool
|
||||
{
|
||||
return $this->baseUrl !== null;
|
||||
}
|
||||
|
||||
protected function setBaseUrl(?string $baseUrl): self
|
||||
{
|
||||
$this->baseUrl = $baseUrl;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Options;
|
||||
|
||||
use Zend\Stdlib\AbstractOptions;
|
||||
|
||||
class NotFoundShortUrlOptions extends AbstractOptions
|
||||
{
|
||||
/** @var bool */
|
||||
private $enableRedirection = false;
|
||||
/** @var string|null */
|
||||
private $redirectTo;
|
||||
|
||||
public function isRedirectionEnabled(): bool
|
||||
{
|
||||
return $this->enableRedirection;
|
||||
}
|
||||
|
||||
protected function setEnableRedirection(bool $enableRedirection = true): self
|
||||
{
|
||||
$this->enableRedirection = $enableRedirection;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRedirectTo(): string
|
||||
{
|
||||
return $this->redirectTo ?? '';
|
||||
}
|
||||
|
||||
protected function setRedirectTo(?string $redirectTo): self
|
||||
{
|
||||
$this->redirectTo = $redirectTo;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -25,20 +25,20 @@ class RedirectActionTest extends TestCase
|
||||
private $urlShortener;
|
||||
/** @var ObjectProphecy */
|
||||
private $visitTracker;
|
||||
/** @var Options\NotFoundShortUrlOptions */
|
||||
private $notFoundOptions;
|
||||
/** @var Options\NotFoundRedirectOptions */
|
||||
private $redirectOptions;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
||||
$this->visitTracker = $this->prophesize(VisitsTracker::class);
|
||||
$this->notFoundOptions = new Options\NotFoundShortUrlOptions();
|
||||
$this->redirectOptions = new Options\NotFoundRedirectOptions();
|
||||
|
||||
$this->action = new RedirectAction(
|
||||
$this->urlShortener->reveal(),
|
||||
$this->visitTracker->reveal(),
|
||||
new Options\AppOptions(['disableTrackParam' => 'foobar']),
|
||||
$this->notFoundOptions
|
||||
$this->redirectOptions
|
||||
);
|
||||
}
|
||||
|
||||
@@ -89,8 +89,7 @@ class RedirectActionTest extends TestCase
|
||||
$handler = $this->prophesize(RequestHandlerInterface::class);
|
||||
$handle = $handler->handle(Argument::any())->willReturn(new Response());
|
||||
|
||||
$this->notFoundOptions->enableRedirection = true;
|
||||
$this->notFoundOptions->redirectTo = 'https://shlink.io';
|
||||
$this->redirectOptions->invalidShortUrl = 'https://shlink.io';
|
||||
|
||||
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
||||
$resp = $this->action->process($request, $handler->reveal());
|
||||
|
||||
Reference in New Issue
Block a user