Updated RedirectAction so that it makes use of the not found short url options

This commit is contained in:
Alejandro Celaya
2018-11-03 12:10:02 +01:00
parent 358b2b661e
commit 313927827d
6 changed files with 100 additions and 23 deletions

View File

@@ -9,7 +9,6 @@ use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Model\Visitor;
@@ -20,8 +19,6 @@ use function array_key_exists;
abstract class AbstractTrackingAction implements MiddlewareInterface
{
use ErrorResponseBuilderTrait;
/**
* @var UrlShortenerInterface
*/
@@ -74,12 +71,17 @@ abstract class AbstractTrackingAction implements MiddlewareInterface
$this->visitTracker->track($shortCode, Visitor::fromRequest($request));
}
return $this->createResp($url->getLongUrl());
return $this->createSuccessResp($url->getLongUrl());
} catch (InvalidShortCodeException | EntityDoesNotExistException $e) {
$this->logger->warning('An error occurred while tracking short code. {e}', ['e' => $e]);
return $this->buildErrorResponse($request, $handler);
return $this->createErrorResp($request, $handler);
}
}
abstract protected function createResp(string $longUrl): ResponseInterface;
abstract protected function createSuccessResp(string $longUrl): ResponseInterface;
abstract protected function createErrorResp(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface;
}

View File

@@ -4,12 +4,21 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Action;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Common\Response\PixelResponse;
class PixelAction extends AbstractTrackingAction
{
protected function createResp(string $longUrl): ResponseInterface
protected function createSuccessResp(string $longUrl): ResponseInterface
{
return new PixelResponse();
}
protected function createErrorResp(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
return new PixelResponse();
}
}

View File

@@ -4,14 +4,50 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Action;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait;
use Shlinkio\Shlink\Core\Options;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
use Zend\Diactoros\Response\RedirectResponse;
class RedirectAction extends AbstractTrackingAction
{
protected function createResp(string $longUrl): Response
use ErrorResponseBuilderTrait;
/**
* @var Options\NotFoundShortUrlOptions
*/
private $notFoundOptions;
public function __construct(
UrlShortenerInterface $urlShortener,
VisitsTrackerInterface $visitTracker,
Options\AppOptions $appOptions,
Options\NotFoundShortUrlOptions $notFoundOptions,
LoggerInterface $logger = null
) {
parent::__construct($urlShortener, $visitTracker, $appOptions, $logger);
$this->notFoundOptions = $notFoundOptions;
}
protected function createSuccessResp(string $longUrl): Response
{
// Return a redirect response to the long URL.
// Use a temporary redirect to make sure browsers always hit the server for analytics purposes
return new RedirectResponse($longUrl);
}
protected function createErrorResp(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): Response {
if ($this->notFoundOptions->isRedirectionEnabled()) {
return new RedirectResponse($this->notFoundOptions->getRedirectTo());
}
return $this->buildErrorResponse($request, $handler);
}
}

View File

@@ -21,18 +21,18 @@ class NotFoundShortUrlOptions extends AbstractOptions
return $this->enableRedirection;
}
protected function enableRedirection(bool $enableRedirection = true): self
protected function setEnableRedirection(bool $enableRedirection = true): self
{
$this->enableRedirection = $enableRedirection;
return $this;
}
public function getRedirectTo(): ?string
public function getRedirectTo(): string
{
return $this->redirectTo;
return $this->redirectTo ?? '';
}
protected function setRedirectTo(string $redirectTo): self
protected function setRedirectTo(?string $redirectTo): self
{
$this->redirectTo = $redirectTo;
return $this;