mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-28 00:38:46 +03:00
Renamed ShortUrlInputFilter and added named constructors to it
This commit is contained in:
parent
08f4a424e6
commit
7e90fd45a7
13 changed files with 99 additions and 92 deletions
|
@ -10,7 +10,7 @@ use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
|||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
@ -146,19 +146,19 @@ class GenerateShortUrlCommand extends BaseCommand
|
|||
|
||||
try {
|
||||
$shortUrl = $this->urlShortener->shorten(ShortUrlMeta::fromRawData([
|
||||
ShortUrlMetaInputFilter::LONG_URL => $longUrl,
|
||||
ShortUrlMetaInputFilter::VALID_SINCE => $this->getOptionWithDeprecatedFallback($input, 'valid-since'),
|
||||
ShortUrlMetaInputFilter::VALID_UNTIL => $this->getOptionWithDeprecatedFallback($input, 'valid-until'),
|
||||
ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug,
|
||||
ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits !== null ? (int) $maxVisits : null,
|
||||
ShortUrlMetaInputFilter::FIND_IF_EXISTS => $this->getOptionWithDeprecatedFallback(
|
||||
ShortUrlInputFilter::LONG_URL => $longUrl,
|
||||
ShortUrlInputFilter::VALID_SINCE => $this->getOptionWithDeprecatedFallback($input, 'valid-since'),
|
||||
ShortUrlInputFilter::VALID_UNTIL => $this->getOptionWithDeprecatedFallback($input, 'valid-until'),
|
||||
ShortUrlInputFilter::CUSTOM_SLUG => $customSlug,
|
||||
ShortUrlInputFilter::MAX_VISITS => $maxVisits !== null ? (int) $maxVisits : null,
|
||||
ShortUrlInputFilter::FIND_IF_EXISTS => $this->getOptionWithDeprecatedFallback(
|
||||
$input,
|
||||
'find-if-exists',
|
||||
),
|
||||
ShortUrlMetaInputFilter::DOMAIN => $input->getOption('domain'),
|
||||
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $shortCodeLength,
|
||||
ShortUrlMetaInputFilter::VALIDATE_URL => $doValidateUrl,
|
||||
ShortUrlMetaInputFilter::TAGS => $tags,
|
||||
ShortUrlInputFilter::DOMAIN => $input->getOption('domain'),
|
||||
ShortUrlInputFilter::SHORT_CODE_LENGTH => $shortCodeLength,
|
||||
ShortUrlInputFilter::VALIDATE_URL => $doValidateUrl,
|
||||
ShortUrlInputFilter::TAGS => $tags,
|
||||
]));
|
||||
|
||||
$io->writeln([
|
||||
|
|
|
@ -14,7 +14,7 @@ use Shlinkio\Shlink\Core\Model\ShortUrlEdit;
|
|||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
|
@ -51,7 +51,7 @@ class ShortUrl extends AbstractEntity
|
|||
|
||||
public static function withLongUrl(string $longUrl): self
|
||||
{
|
||||
return self::fromMeta(ShortUrlMeta::fromRawData([ShortUrlMetaInputFilter::LONG_URL => $longUrl]));
|
||||
return self::fromMeta(ShortUrlMeta::fromRawData([ShortUrlInputFilter::LONG_URL => $longUrl]));
|
||||
}
|
||||
|
||||
public static function fromMeta(
|
||||
|
@ -83,12 +83,12 @@ class ShortUrl extends AbstractEntity
|
|||
?ShortUrlRelationResolverInterface $relationResolver = null
|
||||
): self {
|
||||
$meta = [
|
||||
ShortUrlMetaInputFilter::LONG_URL => $url->longUrl(),
|
||||
ShortUrlMetaInputFilter::DOMAIN => $url->domain(),
|
||||
ShortUrlMetaInputFilter::VALIDATE_URL => false,
|
||||
ShortUrlInputFilter::LONG_URL => $url->longUrl(),
|
||||
ShortUrlInputFilter::DOMAIN => $url->domain(),
|
||||
ShortUrlInputFilter::VALIDATE_URL => false,
|
||||
];
|
||||
if ($importShortCode) {
|
||||
$meta[ShortUrlMetaInputFilter::CUSTOM_SLUG] = $url->shortCode();
|
||||
$meta[ShortUrlInputFilter::CUSTOM_SLUG] = $url->shortCode();
|
||||
}
|
||||
|
||||
$instance = self::fromMeta(ShortUrlMeta::fromRawData($meta), $relationResolver);
|
||||
|
|
|
@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\Core\Model;
|
|||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
|
||||
use function array_key_exists;
|
||||
use function Shlinkio\Shlink\Core\getOptionalBoolFromInputFilter;
|
||||
|
@ -44,21 +44,21 @@ final class ShortUrlEdit
|
|||
*/
|
||||
private function validateAndInit(array $data): void
|
||||
{
|
||||
$inputFilter = new ShortUrlMetaInputFilter($data);
|
||||
$inputFilter = ShortUrlInputFilter::withNonRequiredLongUrl($data);
|
||||
if (! $inputFilter->isValid()) {
|
||||
throw ValidationException::fromInputFilter($inputFilter);
|
||||
}
|
||||
|
||||
$this->longUrlPropWasProvided = array_key_exists(ShortUrlMetaInputFilter::LONG_URL, $data);
|
||||
$this->validSincePropWasProvided = array_key_exists(ShortUrlMetaInputFilter::VALID_SINCE, $data);
|
||||
$this->validUntilPropWasProvided = array_key_exists(ShortUrlMetaInputFilter::VALID_UNTIL, $data);
|
||||
$this->maxVisitsPropWasProvided = array_key_exists(ShortUrlMetaInputFilter::MAX_VISITS, $data);
|
||||
$this->longUrlPropWasProvided = array_key_exists(ShortUrlInputFilter::LONG_URL, $data);
|
||||
$this->validSincePropWasProvided = array_key_exists(ShortUrlInputFilter::VALID_SINCE, $data);
|
||||
$this->validUntilPropWasProvided = array_key_exists(ShortUrlInputFilter::VALID_UNTIL, $data);
|
||||
$this->maxVisitsPropWasProvided = array_key_exists(ShortUrlInputFilter::MAX_VISITS, $data);
|
||||
|
||||
$this->longUrl = $inputFilter->getValue(ShortUrlMetaInputFilter::LONG_URL);
|
||||
$this->validSince = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE));
|
||||
$this->validUntil = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL));
|
||||
$this->maxVisits = getOptionalIntFromInputFilter($inputFilter, ShortUrlMetaInputFilter::MAX_VISITS);
|
||||
$this->validateUrl = getOptionalBoolFromInputFilter($inputFilter, ShortUrlMetaInputFilter::VALIDATE_URL);
|
||||
$this->longUrl = $inputFilter->getValue(ShortUrlInputFilter::LONG_URL);
|
||||
$this->validSince = parseDateField($inputFilter->getValue(ShortUrlInputFilter::VALID_SINCE));
|
||||
$this->validUntil = parseDateField($inputFilter->getValue(ShortUrlInputFilter::VALID_UNTIL));
|
||||
$this->maxVisits = getOptionalIntFromInputFilter($inputFilter, ShortUrlInputFilter::MAX_VISITS);
|
||||
$this->validateUrl = getOptionalBoolFromInputFilter($inputFilter, ShortUrlInputFilter::VALIDATE_URL);
|
||||
}
|
||||
|
||||
public function longUrl(): ?string
|
||||
|
|
|
@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\Core\Model;
|
|||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
use function Shlinkio\Shlink\Core\getOptionalBoolFromInputFilter;
|
||||
|
@ -57,25 +57,25 @@ final class ShortUrlMeta
|
|||
*/
|
||||
private function validateAndInit(array $data): void
|
||||
{
|
||||
$inputFilter = new ShortUrlMetaInputFilter($data, true);
|
||||
$inputFilter = ShortUrlInputFilter::withRequiredLongUrl($data);
|
||||
if (! $inputFilter->isValid()) {
|
||||
throw ValidationException::fromInputFilter($inputFilter);
|
||||
}
|
||||
|
||||
$this->longUrl = $inputFilter->getValue(ShortUrlMetaInputFilter::LONG_URL);
|
||||
$this->validSince = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE));
|
||||
$this->validUntil = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL));
|
||||
$this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG);
|
||||
$this->maxVisits = getOptionalIntFromInputFilter($inputFilter, ShortUrlMetaInputFilter::MAX_VISITS);
|
||||
$this->findIfExists = $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS);
|
||||
$this->validateUrl = getOptionalBoolFromInputFilter($inputFilter, ShortUrlMetaInputFilter::VALIDATE_URL);
|
||||
$this->domain = $inputFilter->getValue(ShortUrlMetaInputFilter::DOMAIN);
|
||||
$this->longUrl = $inputFilter->getValue(ShortUrlInputFilter::LONG_URL);
|
||||
$this->validSince = parseDateField($inputFilter->getValue(ShortUrlInputFilter::VALID_SINCE));
|
||||
$this->validUntil = parseDateField($inputFilter->getValue(ShortUrlInputFilter::VALID_UNTIL));
|
||||
$this->customSlug = $inputFilter->getValue(ShortUrlInputFilter::CUSTOM_SLUG);
|
||||
$this->maxVisits = getOptionalIntFromInputFilter($inputFilter, ShortUrlInputFilter::MAX_VISITS);
|
||||
$this->findIfExists = $inputFilter->getValue(ShortUrlInputFilter::FIND_IF_EXISTS);
|
||||
$this->validateUrl = getOptionalBoolFromInputFilter($inputFilter, ShortUrlInputFilter::VALIDATE_URL);
|
||||
$this->domain = $inputFilter->getValue(ShortUrlInputFilter::DOMAIN);
|
||||
$this->shortCodeLength = getOptionalIntFromInputFilter(
|
||||
$inputFilter,
|
||||
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH,
|
||||
ShortUrlInputFilter::SHORT_CODE_LENGTH,
|
||||
) ?? DEFAULT_SHORT_CODES_LENGTH;
|
||||
$this->apiKey = $inputFilter->getValue(ShortUrlMetaInputFilter::API_KEY);
|
||||
$this->tags = $inputFilter->getValue(ShortUrlMetaInputFilter::TAGS);
|
||||
$this->apiKey = $inputFilter->getValue(ShortUrlInputFilter::API_KEY);
|
||||
$this->tags = $inputFilter->getValue(ShortUrlInputFilter::TAGS);
|
||||
}
|
||||
|
||||
public function getLongUrl(): string
|
||||
|
|
|
@ -16,7 +16,7 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|||
use const Shlinkio\Shlink\Core\CUSTOM_SLUGS_REGEXP;
|
||||
use const Shlinkio\Shlink\Core\MIN_SHORT_CODES_LENGTH;
|
||||
|
||||
class ShortUrlMetaInputFilter extends InputFilter
|
||||
class ShortUrlInputFilter extends InputFilter
|
||||
{
|
||||
use Validation\InputFactoryTrait;
|
||||
|
||||
|
@ -32,18 +32,25 @@ class ShortUrlMetaInputFilter extends InputFilter
|
|||
public const API_KEY = 'apiKey';
|
||||
public const TAGS = 'tags';
|
||||
|
||||
private bool $requireLongUrl;
|
||||
|
||||
public function __construct(array $data, bool $requireLongUrl = false)
|
||||
private function __construct(array $data, bool $requireLongUrl)
|
||||
{
|
||||
$this->requireLongUrl = $requireLongUrl;
|
||||
$this->initialize();
|
||||
$this->initialize($requireLongUrl);
|
||||
$this->setData($data);
|
||||
}
|
||||
|
||||
private function initialize(): void
|
||||
public static function withRequiredLongUrl(array $data): self
|
||||
{
|
||||
$longUrlInput = $this->createInput(self::LONG_URL, $this->requireLongUrl);
|
||||
return new self($data, true);
|
||||
}
|
||||
|
||||
public static function withNonRequiredLongUrl(array $data): self
|
||||
{
|
||||
return new self($data, false);
|
||||
}
|
||||
|
||||
private function initialize(bool $requireLongUrl): void
|
||||
{
|
||||
$longUrlInput = $this->createInput(self::LONG_URL, $requireLongUrl);
|
||||
$longUrlInput->getValidatorChain()->attach(new Validator\NotEmpty([
|
||||
Validator\NotEmpty::OBJECT,
|
||||
Validator\NotEmpty::SPACE,
|
|
@ -9,7 +9,7 @@ use PHPUnit\Framework\TestCase;
|
|||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
|
||||
|
||||
use function Functional\map;
|
||||
|
@ -75,7 +75,7 @@ class ShortUrlTest extends TestCase
|
|||
public function shortCodesHaveExpectedLength(?int $length, int $expectedLength): void
|
||||
{
|
||||
$shortUrl = ShortUrl::fromMeta(ShortUrlMeta::fromRawData(
|
||||
[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $length, 'longUrl' => ''],
|
||||
[ShortUrlInputFilter::SHORT_CODE_LENGTH => $length, 'longUrl' => ''],
|
||||
));
|
||||
|
||||
self::assertEquals($expectedLength, strlen($shortUrl->getShortCode()));
|
||||
|
|
|
@ -8,7 +8,7 @@ use Cake\Chronos\Chronos;
|
|||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use stdClass;
|
||||
|
||||
class ShortUrlMetaTest extends TestCase
|
||||
|
@ -27,37 +27,37 @@ class ShortUrlMetaTest extends TestCase
|
|||
public function provideInvalidData(): iterable
|
||||
{
|
||||
yield [[
|
||||
ShortUrlMetaInputFilter::VALID_SINCE => '',
|
||||
ShortUrlMetaInputFilter::VALID_UNTIL => '',
|
||||
ShortUrlMetaInputFilter::CUSTOM_SLUG => 'foobar',
|
||||
ShortUrlMetaInputFilter::MAX_VISITS => 'invalid',
|
||||
ShortUrlInputFilter::VALID_SINCE => '',
|
||||
ShortUrlInputFilter::VALID_UNTIL => '',
|
||||
ShortUrlInputFilter::CUSTOM_SLUG => 'foobar',
|
||||
ShortUrlInputFilter::MAX_VISITS => 'invalid',
|
||||
]];
|
||||
yield [[
|
||||
ShortUrlMetaInputFilter::VALID_SINCE => '2017',
|
||||
ShortUrlMetaInputFilter::MAX_VISITS => 5,
|
||||
ShortUrlInputFilter::VALID_SINCE => '2017',
|
||||
ShortUrlInputFilter::MAX_VISITS => 5,
|
||||
]];
|
||||
yield [[
|
||||
ShortUrlMetaInputFilter::VALID_SINCE => new stdClass(),
|
||||
ShortUrlMetaInputFilter::VALID_UNTIL => 'foo',
|
||||
ShortUrlInputFilter::VALID_SINCE => new stdClass(),
|
||||
ShortUrlInputFilter::VALID_UNTIL => 'foo',
|
||||
]];
|
||||
yield [[
|
||||
ShortUrlMetaInputFilter::VALID_UNTIL => 500,
|
||||
ShortUrlMetaInputFilter::DOMAIN => 4,
|
||||
ShortUrlInputFilter::VALID_UNTIL => 500,
|
||||
ShortUrlInputFilter::DOMAIN => 4,
|
||||
]];
|
||||
yield [[
|
||||
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => 3,
|
||||
ShortUrlInputFilter::SHORT_CODE_LENGTH => 3,
|
||||
]];
|
||||
yield [[
|
||||
ShortUrlMetaInputFilter::CUSTOM_SLUG => '/',
|
||||
ShortUrlInputFilter::CUSTOM_SLUG => '/',
|
||||
]];
|
||||
yield [[
|
||||
ShortUrlMetaInputFilter::CUSTOM_SLUG => '',
|
||||
ShortUrlInputFilter::CUSTOM_SLUG => '',
|
||||
]];
|
||||
yield [[
|
||||
ShortUrlMetaInputFilter::CUSTOM_SLUG => ' ',
|
||||
ShortUrlInputFilter::CUSTOM_SLUG => ' ',
|
||||
]];
|
||||
yield [[
|
||||
ShortUrlMetaInputFilter::LONG_URL => [],
|
||||
ShortUrlInputFilter::LONG_URL => [],
|
||||
]];
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
|||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
|
||||
|
||||
class CreateShortUrlAction extends AbstractCreateShortUrlAction
|
||||
|
@ -21,7 +21,7 @@ class CreateShortUrlAction extends AbstractCreateShortUrlAction
|
|||
protected function buildShortUrlData(Request $request): ShortUrlMeta
|
||||
{
|
||||
$payload = (array) $request->getParsedBody();
|
||||
$payload[ShortUrlMetaInputFilter::API_KEY] = AuthenticationMiddleware::apiKeyFromRequest($request);
|
||||
$payload[ShortUrlInputFilter::API_KEY] = AuthenticationMiddleware::apiKeyFromRequest($request);
|
||||
|
||||
return ShortUrlMeta::fromRawData($payload);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
|||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
|
||||
|
||||
class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction
|
||||
|
@ -21,10 +21,10 @@ class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction
|
|||
$apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
|
||||
|
||||
return ShortUrlMeta::fromRawData([
|
||||
ShortUrlMetaInputFilter::LONG_URL => $longUrl,
|
||||
ShortUrlMetaInputFilter::API_KEY => $apiKey,
|
||||
ShortUrlInputFilter::LONG_URL => $longUrl,
|
||||
ShortUrlInputFilter::API_KEY => $apiKey,
|
||||
// This will usually be null, unless this API key enforces one specific domain
|
||||
ShortUrlMetaInputFilter::DOMAIN => $request->getAttribute(ShortUrlMetaInputFilter::DOMAIN),
|
||||
ShortUrlInputFilter::DOMAIN => $request->getAttribute(ShortUrlInputFilter::DOMAIN),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ use Psr\Http\Message\ResponseInterface;
|
|||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
|
||||
class DefaultShortCodesLengthMiddleware implements MiddlewareInterface
|
||||
{
|
||||
|
@ -22,8 +22,8 @@ class DefaultShortCodesLengthMiddleware implements MiddlewareInterface
|
|||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
if (! isset($body[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH])) {
|
||||
$body[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH] = $this->defaultShortCodesLength;
|
||||
if (! isset($body[ShortUrlInputFilter::SHORT_CODE_LENGTH])) {
|
||||
$body[ShortUrlInputFilter::SHORT_CODE_LENGTH] = $this->defaultShortCodesLength;
|
||||
}
|
||||
|
||||
return $handler->handle($request->withParsedBody($body));
|
||||
|
|
|
@ -10,7 +10,7 @@ use Psr\Http\Message\ServerRequestInterface;
|
|||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use Shlinkio\Shlink\Rest\ApiKey\Role;
|
||||
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
|
||||
|
||||
|
@ -36,11 +36,11 @@ class OverrideDomainMiddleware implements MiddlewareInterface
|
|||
|
||||
if ($requestMethod === RequestMethodInterface::METHOD_POST) {
|
||||
$payload = $request->getParsedBody();
|
||||
$payload[ShortUrlMetaInputFilter::DOMAIN] = $domain->getAuthority();
|
||||
$payload[ShortUrlInputFilter::DOMAIN] = $domain->getAuthority();
|
||||
|
||||
return $handler->handle($request->withParsedBody($payload));
|
||||
}
|
||||
|
||||
return $handler->handle($request->withAttribute(ShortUrlMetaInputFilter::DOMAIN, $domain->getAuthority()));
|
||||
return $handler->handle($request->withAttribute(ShortUrlInputFilter::DOMAIN, $domain->getAuthority()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ use Prophecy\PhpUnit\ProphecyTrait;
|
|||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\DefaultShortCodesLengthMiddleware;
|
||||
|
||||
class DefaultShortCodesLengthMiddlewareTest extends TestCase
|
||||
|
@ -38,8 +38,8 @@ class DefaultShortCodesLengthMiddlewareTest extends TestCase
|
|||
$request = ServerRequestFactory::fromGlobals()->withParsedBody($body);
|
||||
$handle = $this->handler->handle(Argument::that(function (ServerRequestInterface $req) use ($expectedLength) {
|
||||
$parsedBody = $req->getParsedBody();
|
||||
Assert::assertArrayHasKey(ShortUrlMetaInputFilter::SHORT_CODE_LENGTH, $parsedBody);
|
||||
Assert::assertEquals($expectedLength, $parsedBody[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH]);
|
||||
Assert::assertArrayHasKey(ShortUrlInputFilter::SHORT_CODE_LENGTH, $parsedBody);
|
||||
Assert::assertEquals($expectedLength, $parsedBody[ShortUrlInputFilter::SHORT_CODE_LENGTH]);
|
||||
|
||||
return $req;
|
||||
}))->willReturn(new Response());
|
||||
|
@ -51,7 +51,7 @@ class DefaultShortCodesLengthMiddlewareTest extends TestCase
|
|||
|
||||
public function provideBodies(): iterable
|
||||
{
|
||||
yield 'value provided' => [[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => 6], 6];
|
||||
yield 'value provided' => [[ShortUrlInputFilter::SHORT_CODE_LENGTH => 6], 6];
|
||||
yield 'value not provided' => [[], 8];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ use Psr\Http\Message\ServerRequestInterface;
|
|||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use Shlinkio\Shlink\Rest\ApiKey\Role;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\OverrideDomainMiddleware;
|
||||
|
@ -82,21 +82,21 @@ class OverrideDomainMiddlewareTest extends TestCase
|
|||
|
||||
public function provideBodies(): iterable
|
||||
{
|
||||
yield 'no domain provided' => [new Domain('foo.com'), [], [ShortUrlMetaInputFilter::DOMAIN => 'foo.com']];
|
||||
yield 'no domain provided' => [new Domain('foo.com'), [], [ShortUrlInputFilter::DOMAIN => 'foo.com']];
|
||||
yield 'other domain provided' => [
|
||||
new Domain('bar.com'),
|
||||
[ShortUrlMetaInputFilter::DOMAIN => 'foo.com'],
|
||||
[ShortUrlMetaInputFilter::DOMAIN => 'bar.com'],
|
||||
[ShortUrlInputFilter::DOMAIN => 'foo.com'],
|
||||
[ShortUrlInputFilter::DOMAIN => 'bar.com'],
|
||||
];
|
||||
yield 'same domain provided' => [
|
||||
new Domain('baz.com'),
|
||||
[ShortUrlMetaInputFilter::DOMAIN => 'baz.com'],
|
||||
[ShortUrlMetaInputFilter::DOMAIN => 'baz.com'],
|
||||
[ShortUrlInputFilter::DOMAIN => 'baz.com'],
|
||||
[ShortUrlInputFilter::DOMAIN => 'baz.com'],
|
||||
];
|
||||
yield 'more body params' => [
|
||||
new Domain('doma.in'),
|
||||
[ShortUrlMetaInputFilter::DOMAIN => 'baz.com', 'something' => 'else', 'foo' => 123],
|
||||
[ShortUrlMetaInputFilter::DOMAIN => 'doma.in', 'something' => 'else', 'foo' => 123],
|
||||
[ShortUrlInputFilter::DOMAIN => 'baz.com', 'something' => 'else', 'foo' => 123],
|
||||
[ShortUrlInputFilter::DOMAIN => 'doma.in', 'something' => 'else', 'foo' => 123],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -113,7 +113,7 @@ class OverrideDomainMiddlewareTest extends TestCase
|
|||
$getDomain = $this->domainService->getDomain('123')->willReturn($domain);
|
||||
$handle = $this->handler->handle(Argument::that(
|
||||
function (ServerRequestInterface $req): bool {
|
||||
Assert::assertEquals($req->getAttribute(ShortUrlMetaInputFilter::DOMAIN), 'something.com');
|
||||
Assert::assertEquals($req->getAttribute(ShortUrlInputFilter::DOMAIN), 'something.com');
|
||||
return true;
|
||||
},
|
||||
))->willReturn(new Response());
|
||||
|
|
Loading…
Reference in a new issue