mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Updated action and command to create short urls so that it accepts validity dates
This commit is contained in:
@@ -51,9 +51,17 @@ class GenerateShortcodeCommand extends Command
|
|||||||
->addOption(
|
->addOption(
|
||||||
'tags',
|
'tags',
|
||||||
't',
|
't',
|
||||||
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
|
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
|
||||||
$this->translator->translate('Tags to apply to the new short URL')
|
$this->translator->translate('Tags to apply to the new short URL')
|
||||||
);
|
)
|
||||||
|
->addOption('validSince', 's', InputOption::VALUE_REQUIRED, $this->translator->translate(
|
||||||
|
'The date from which this short URL will be valid. '
|
||||||
|
. 'If someone tries to access it before this date, it will not be found.'
|
||||||
|
))
|
||||||
|
->addOption('validUntil', 'u', InputOption::VALUE_REQUIRED, $this->translator->translate(
|
||||||
|
'The date until which this short URL will be valid. '
|
||||||
|
. 'If someone tries to access it after this date, it will not be found.'
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function interact(InputInterface $input, OutputInterface $output)
|
public function interact(InputInterface $input, OutputInterface $output)
|
||||||
@@ -93,7 +101,12 @@ class GenerateShortcodeCommand extends Command
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl), $tags);
|
$shortCode = $this->urlShortener->urlToShortCode(
|
||||||
|
new Uri($longUrl),
|
||||||
|
$tags,
|
||||||
|
$this->getOptionalDate($input, 'validSince'),
|
||||||
|
$this->getOptionalDate($input, 'validUntil')
|
||||||
|
);
|
||||||
$shortUrl = (new Uri())->withPath($shortCode)
|
$shortUrl = (new Uri())->withPath($shortCode)
|
||||||
->withScheme($this->domainConfig['schema'])
|
->withScheme($this->domainConfig['schema'])
|
||||||
->withHost($this->domainConfig['hostname']);
|
->withHost($this->domainConfig['hostname']);
|
||||||
@@ -111,4 +124,10 @@ class GenerateShortcodeCommand extends Command
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getOptionalDate(InputInterface $input, string $fieldName)
|
||||||
|
{
|
||||||
|
$since = $input->getOption($fieldName);
|
||||||
|
return $since !== null ? new \DateTime($since) : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,12 +57,18 @@ class UrlShortener implements UrlShortenerInterface
|
|||||||
*
|
*
|
||||||
* @param UriInterface $url
|
* @param UriInterface $url
|
||||||
* @param string[] $tags
|
* @param string[] $tags
|
||||||
|
* @param \DateTime|null $validSince
|
||||||
|
* @param \DateTime|null $validUntil
|
||||||
* @return string
|
* @return string
|
||||||
* @throws InvalidUrlException
|
* @throws InvalidUrlException
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
*/
|
*/
|
||||||
public function urlToShortCode(UriInterface $url, array $tags = []): string
|
public function urlToShortCode(
|
||||||
{
|
UriInterface $url,
|
||||||
|
array $tags = [],
|
||||||
|
\DateTime $validSince = null,
|
||||||
|
\DateTime $validUntil = null
|
||||||
|
): string {
|
||||||
// If the url already exists in the database, just return its short code
|
// If the url already exists in the database, just return its short code
|
||||||
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
|
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
|
||||||
'originalUrl' => $url,
|
'originalUrl' => $url,
|
||||||
@@ -80,7 +86,9 @@ class UrlShortener implements UrlShortenerInterface
|
|||||||
|
|
||||||
// First, create the short URL with an empty short code
|
// First, create the short URL with an empty short code
|
||||||
$shortUrl = new ShortUrl();
|
$shortUrl = new ShortUrl();
|
||||||
$shortUrl->setOriginalUrl((string) $url);
|
$shortUrl->setOriginalUrl((string) $url)
|
||||||
|
->setValidSince($validSince)
|
||||||
|
->setValidUntil($validUntil);
|
||||||
$this->em->persist($shortUrl);
|
$this->em->persist($shortUrl);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
|
|
||||||
|
|||||||
@@ -16,11 +16,18 @@ interface UrlShortenerInterface
|
|||||||
*
|
*
|
||||||
* @param UriInterface $url
|
* @param UriInterface $url
|
||||||
* @param string[] $tags
|
* @param string[] $tags
|
||||||
|
* @param \DateTime|null $validSince
|
||||||
|
* @param \DateTime|null $validUntil
|
||||||
* @return string
|
* @return string
|
||||||
* @throws InvalidUrlException
|
* @throws InvalidUrlException
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
*/
|
*/
|
||||||
public function urlToShortCode(UriInterface $url, array $tags = []): string;
|
public function urlToShortCode(
|
||||||
|
UriInterface $url,
|
||||||
|
array $tags = [],
|
||||||
|
\DateTime $validSince = null,
|
||||||
|
\DateTime $validUntil = null
|
||||||
|
): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to find the mapped URL for provided short code. Returns null if not found
|
* Tries to find the mapped URL for provided short code. Returns null if not found
|
||||||
|
|||||||
@@ -57,10 +57,12 @@ class CreateShortcodeAction extends AbstractRestAction
|
|||||||
], self::STATUS_BAD_REQUEST);
|
], self::STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
$longUrl = $postData['longUrl'];
|
$longUrl = $postData['longUrl'];
|
||||||
$tags = isset($postData['tags']) && is_array($postData['tags']) ? $postData['tags'] : [];
|
$tags = (array) ($postData['tags'] ?? []);
|
||||||
|
$validSince = $this->getOptionalDate($postData, 'validSince');
|
||||||
|
$validUntil = $this->getOptionalDate($postData, 'validUntil');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl), $tags);
|
$shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl), $tags, $validSince, $validUntil);
|
||||||
$shortUrl = (new Uri())->withPath($shortCode)
|
$shortUrl = (new Uri())->withPath($shortCode)
|
||||||
->withScheme($this->domainConfig['schema'])
|
->withScheme($this->domainConfig['schema'])
|
||||||
->withHost($this->domainConfig['hostname']);
|
->withHost($this->domainConfig['hostname']);
|
||||||
@@ -87,4 +89,9 @@ class CreateShortcodeAction extends AbstractRestAction
|
|||||||
], self::STATUS_INTERNAL_SERVER_ERROR);
|
], self::STATUS_INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getOptionalDate(array $postData, string $fieldName)
|
||||||
|
{
|
||||||
|
return isset($postData[$fieldName]) ? new \DateTime($postData[$fieldName]) : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class CreateShortcodeActionTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function properShortcodeConversionReturnsData()
|
public function properShortcodeConversionReturnsData()
|
||||||
{
|
{
|
||||||
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'))
|
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null)
|
||||||
->willReturn('abc123')
|
->willReturn('abc123')
|
||||||
->shouldBeCalledTimes(1);
|
->shouldBeCalledTimes(1);
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ class CreateShortcodeActionTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function anInvalidUrlReturnsError()
|
public function anInvalidUrlReturnsError()
|
||||||
{
|
{
|
||||||
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'))
|
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null)
|
||||||
->willThrow(InvalidUrlException::class)
|
->willThrow(InvalidUrlException::class)
|
||||||
->shouldBeCalledTimes(1);
|
->shouldBeCalledTimes(1);
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ class CreateShortcodeActionTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function aGenericExceptionWillReturnError()
|
public function aGenericExceptionWillReturnError()
|
||||||
{
|
{
|
||||||
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'))
|
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null)
|
||||||
->willThrow(\Exception::class)
|
->willThrow(\Exception::class)
|
||||||
->shouldBeCalledTimes(1);
|
->shouldBeCalledTimes(1);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user