Update ShortUrlRedirectionBuilder to accept a request object instead of a raw query array

This commit is contained in:
Alejandro Celaya 2023-01-21 10:37:12 +01:00
parent c1b7c6ba6c
commit 237fb95b4b
7 changed files with 30 additions and 11 deletions

View File

@ -18,15 +18,15 @@ class RedirectAction extends AbstractTrackingAction implements StatusCodeInterfa
public function __construct(
ShortUrlResolverInterface $urlResolver,
RequestTrackerInterface $requestTracker,
private ShortUrlRedirectionBuilderInterface $redirectionBuilder,
private RedirectResponseHelperInterface $redirectResponseHelper,
private readonly ShortUrlRedirectionBuilderInterface $redirectionBuilder,
private readonly RedirectResponseHelperInterface $redirectResponseHelper,
) {
parent::__construct($urlResolver, $requestTracker);
}
protected function createSuccessResp(ShortUrl $shortUrl, ServerRequestInterface $request): Response
{
$longUrl = $this->redirectionBuilder->buildShortUrlRedirect($shortUrl, $request->getQueryParams());
$longUrl = $this->redirectionBuilder->buildShortUrlRedirect($shortUrl, $request);
return $this->redirectResponseHelper->buildRedirectResponse($longUrl);
}
}

View File

@ -7,6 +7,7 @@ namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
use GuzzleHttp\Psr7\Query;
use Laminas\Stdlib\ArrayUtils;
use League\Uri\Uri;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Options\TrackingOptions;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
@ -14,12 +15,16 @@ use function sprintf;
class ShortUrlRedirectionBuilder implements ShortUrlRedirectionBuilderInterface
{
public function __construct(private TrackingOptions $trackingOptions)
public function __construct(private readonly TrackingOptions $trackingOptions)
{
}
public function buildShortUrlRedirect(ShortUrl $shortUrl, array $currentQuery, ?string $extraPath = null): string
{
public function buildShortUrlRedirect(
ShortUrl $shortUrl,
ServerRequestInterface $request,
?string $extraPath = null,
): string {
$currentQuery = $request->getQueryParams();
$uri = Uri::createFromString($shortUrl->getLongUrl());
$shouldForwardQuery = $shortUrl->forwardQuery();

View File

@ -4,9 +4,14 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
interface ShortUrlRedirectionBuilderInterface
{
public function buildShortUrlRedirect(ShortUrl $shortUrl, array $currentQuery, ?string $extraPath = null): string;
public function buildShortUrlRedirect(
ShortUrl $shortUrl,
ServerRequestInterface $request,
?string $extraPath = null,
): string;
}

View File

@ -68,7 +68,6 @@ class ExtraPathRedirectMiddleware implements MiddlewareInterface
int $shortCodeSegments = 1,
): ResponseInterface {
$uri = $request->getUri();
$query = $request->getQueryParams();
[$potentialShortCode, $extraPath] = $this->resolvePotentialShortCodeAndExtraPath($uri, $shortCodeSegments);
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($potentialShortCode, $uri->getAuthority());
@ -76,7 +75,7 @@ class ExtraPathRedirectMiddleware implements MiddlewareInterface
$shortUrl = $this->resolver->resolveEnabledShortUrl($identifier);
$this->requestTracker->trackIfApplicable($shortUrl, $request);
$longUrl = $this->redirectionBuilder->buildShortUrlRedirect($shortUrl, $query, $extraPath);
$longUrl = $this->redirectionBuilder->buildShortUrlRedirect($shortUrl, $request, $extraPath);
return $this->redirectResponseHelper->buildRedirectResponse($longUrl);
} catch (ShortUrlNotFoundException) {
if ($extraPath === null || ! $this->urlShortenerOptions->multiSegmentSlugsEnabled) {

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ShortUrl\Helper;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Options\TrackingOptions;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
@ -34,7 +35,11 @@ class ShortUrlRedirectionBuilderTest extends TestCase
'longUrl' => 'https://domain.com/foo/bar?some=thing',
'forwardQuery' => $forwardQuery,
]));
$result = $this->redirectionBuilder->buildShortUrlRedirect($shortUrl, $query, $extraPath);
$result = $this->redirectionBuilder->buildShortUrlRedirect(
$shortUrl,
ServerRequestFactory::fromGlobals()->withQueryParams($query),
$extraPath,
);
self::assertEquals($expectedUrl, $result);
}

View File

@ -159,7 +159,7 @@ class ExtraPathRedirectMiddlewareTest extends TestCase
);
$this->redirectionBuilder->expects($this->once())->method('buildShortUrlRedirect')->with(
$shortUrl,
[],
$this->isInstanceOf(ServerRequestInterface::class),
$expectedExtraPath,
)->willReturn('the_built_long_url');
$this->redirectResponseHelper->expects($this->once())->method('buildRedirectResponse')->with(

View File

@ -9,6 +9,7 @@ use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use ReflectionObject;
use Shlinkio\Shlink\Core\Model\DeviceType;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
@ -48,6 +49,10 @@ class ShortUrlsFixture extends AbstractFixture implements DependentFixtureInterf
'apiKey' => $authorApiKey,
'longUrl' =>
'https://blog.alejandrocelaya.com/2017/12/09/acmailer-7-0-the-most-important-release-in-a-long-time/',
'deviceLongUrls' => [
DeviceType::ANDROID->value => 'https://blog.alejandrocelaya.com/android',
DeviceType::IOS->value => 'https://blog.alejandrocelaya.com/ios',
],
'tags' => ['foo', 'bar'],
]), $relationResolver), '2019-01-01 00:00:10');
$manager->persist($defShortUrl);