mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-27 20:11:34 +03:00
Updated short URL creation responses to include more information
This commit is contained in:
parent
17779dbbc6
commit
f3c92f4110
10 changed files with 39 additions and 32 deletions
|
@ -115,10 +115,10 @@ class GenerateShortcodeCommand extends Command
|
||||||
$this->getOptionalDate($input, 'validUntil'),
|
$this->getOptionalDate($input, 'validUntil'),
|
||||||
$customSlug,
|
$customSlug,
|
||||||
$maxVisits !== null ? (int) $maxVisits : null
|
$maxVisits !== null ? (int) $maxVisits : null
|
||||||
);
|
)->getShortCode();
|
||||||
$shortUrl = (new Uri())->withPath($shortCode)
|
$shortUrl = (string) (new Uri())->withPath($shortCode)
|
||||||
->withScheme($this->domainConfig['schema'])
|
->withScheme($this->domainConfig['schema'])
|
||||||
->withHost($this->domainConfig['hostname']);
|
->withHost($this->domainConfig['hostname']);
|
||||||
|
|
||||||
$io->writeln([
|
$io->writeln([
|
||||||
\sprintf('%s <info>%s</info>', $this->translator->translate('Processed long URL:'), $longUrl),
|
\sprintf('%s <info>%s</info>', $this->translator->translate('Processed long URL:'), $longUrl),
|
||||||
|
|
|
@ -117,7 +117,7 @@ class ListShortcodesCommand extends Command
|
||||||
foreach ($result as $row) {
|
foreach ($result as $row) {
|
||||||
$shortUrl = $transformer->transform($row);
|
$shortUrl = $transformer->transform($row);
|
||||||
if ($showTags) {
|
if ($showTags) {
|
||||||
$shortUrl['tags'] = implode(', ', $shortUrl['tags']);
|
$shortUrl['tags'] = \implode(', ', $shortUrl['tags']);
|
||||||
} else {
|
} else {
|
||||||
unset($shortUrl['tags']);
|
unset($shortUrl['tags']);
|
||||||
}
|
}
|
||||||
|
@ -146,7 +146,7 @@ class ListShortcodesCommand extends Command
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$orderBy = explode(',', $orderBy);
|
$orderBy = \explode(',', $orderBy);
|
||||||
return count($orderBy) === 1 ? $orderBy[0] : [$orderBy[0] => $orderBy[1]];
|
return \count($orderBy) === 1 ? $orderBy[0] : [$orderBy[0] => $orderBy[1]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Shlinkio\Shlink\CLI\Command\Shortcode\GenerateShortcodeCommand;
|
use Shlinkio\Shlink\CLI\Command\Shortcode\GenerateShortcodeCommand;
|
||||||
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
||||||
use Symfony\Component\Console\Application;
|
use Symfony\Component\Console\Application;
|
||||||
|
@ -41,8 +42,12 @@ class GenerateShortcodeCommandTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function properShortCodeIsCreatedIfLongUrlIsCorrect()
|
public function properShortCodeIsCreatedIfLongUrlIsCorrect()
|
||||||
{
|
{
|
||||||
$this->urlShortener->urlToShortCode(Argument::cetera())->willReturn('abc123')
|
$this->urlShortener->urlToShortCode(Argument::cetera())
|
||||||
->shouldBeCalledTimes(1);
|
->willReturn(
|
||||||
|
(new ShortUrl())->setShortCode('abc123')
|
||||||
|
->setLongUrl('')
|
||||||
|
)
|
||||||
|
->shouldBeCalledTimes(1);
|
||||||
|
|
||||||
$this->commandTester->execute([
|
$this->commandTester->execute([
|
||||||
'command' => 'shortcode:generate',
|
'command' => 'shortcode:generate',
|
||||||
|
|
|
@ -81,7 +81,7 @@ class UrlShortener implements UrlShortenerInterface
|
||||||
\DateTime $validUntil = null,
|
\DateTime $validUntil = null,
|
||||||
string $customSlug = null,
|
string $customSlug = null,
|
||||||
int $maxVisits = null
|
int $maxVisits = null
|
||||||
): string {
|
): ShortUrl {
|
||||||
// If the URL validation is enabled, check that the URL actually exists
|
// If the URL validation is enabled, check that the URL actually exists
|
||||||
if ($this->urlValidationEnabled) {
|
if ($this->urlValidationEnabled) {
|
||||||
$this->checkUrlExists($url);
|
$this->checkUrlExists($url);
|
||||||
|
@ -108,7 +108,7 @@ class UrlShortener implements UrlShortenerInterface
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
|
|
||||||
$this->em->commit();
|
$this->em->commit();
|
||||||
return $shortCode;
|
return $shortUrl;
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
if ($this->em->getConnection()->isTransactionActive()) {
|
if ($this->em->getConnection()->isTransactionActive()) {
|
||||||
$this->em->rollback();
|
$this->em->rollback();
|
||||||
|
|
|
@ -33,7 +33,7 @@ interface UrlShortenerInterface
|
||||||
\DateTime $validUntil = null,
|
\DateTime $validUntil = null,
|
||||||
string $customSlug = null,
|
string $customSlug = null,
|
||||||
int $maxVisits = null
|
int $maxVisits = null
|
||||||
): string;
|
): ShortUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
|
|
|
@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Transformer;
|
||||||
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
||||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Entity\Tag;
|
use Shlinkio\Shlink\Core\Entity\Tag;
|
||||||
|
use Zend\Diactoros\Uri;
|
||||||
|
|
||||||
class ShortUrlDataTransformer implements DataTransformerInterface
|
class ShortUrlDataTransformer implements DataTransformerInterface
|
||||||
{
|
{
|
||||||
|
@ -31,12 +32,7 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'shortCode' => $shortCode,
|
'shortCode' => $shortCode,
|
||||||
'shortUrl' => \sprintf(
|
'shortUrl' => $this->buildShortUrl($shortCode),
|
||||||
'%s://%s/%s',
|
|
||||||
$this->domainConfig['schema'] ?? 'http',
|
|
||||||
$this->domainConfig['hostname'] ?? '',
|
|
||||||
$shortCode
|
|
||||||
),
|
|
||||||
'longUrl' => $longUrl,
|
'longUrl' => $longUrl,
|
||||||
'dateCreated' => $dateCreated !== null ? $dateCreated->format(\DateTime::ATOM) : null,
|
'dateCreated' => $dateCreated !== null ? $dateCreated->format(\DateTime::ATOM) : null,
|
||||||
'visitsCount' => $value->getVisitsCount(),
|
'visitsCount' => $value->getVisitsCount(),
|
||||||
|
@ -47,6 +43,13 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function buildShortUrl(string $shortCode): string
|
||||||
|
{
|
||||||
|
return (string) (new Uri())->withPath($shortCode)
|
||||||
|
->withScheme($this->domainConfig['schema'] ?? 'http')
|
||||||
|
->withHost($this->domainConfig['hostname'] ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
private function serializeTag(Tag $tag): string
|
private function serializeTag(Tag $tag): string
|
||||||
{
|
{
|
||||||
return $tag->getName();
|
return $tag->getName();
|
||||||
|
|
|
@ -85,8 +85,8 @@ class UrlShortenerTest extends TestCase
|
||||||
public function urlIsProperlyShortened()
|
public function urlIsProperlyShortened()
|
||||||
{
|
{
|
||||||
// 10 -> 12C1c
|
// 10 -> 12C1c
|
||||||
$shortCode = $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
|
$shortUrl = $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
|
||||||
$this->assertEquals('12C1c', $shortCode);
|
$this->assertEquals('12C1c', $shortUrl->getShortCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -11,10 +11,10 @@ use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||||
use Shlinkio\Shlink\Core\Model\CreateShortCodeData;
|
use Shlinkio\Shlink\Core\Model\CreateShortCodeData;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
||||||
|
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
|
||||||
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
||||||
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
||||||
use Zend\Diactoros\Response\JsonResponse;
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
use Zend\Diactoros\Uri;
|
|
||||||
use Zend\I18n\Translator\TranslatorInterface;
|
use Zend\I18n\Translator\TranslatorInterface;
|
||||||
|
|
||||||
abstract class AbstractCreateShortCodeAction extends AbstractRestAction
|
abstract class AbstractCreateShortCodeAction extends AbstractRestAction
|
||||||
|
@ -65,7 +65,7 @@ abstract class AbstractCreateShortCodeAction extends AbstractRestAction
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$shortCode = $this->urlShortener->urlToShortCode(
|
$shortUrl = $this->urlShortener->urlToShortCode(
|
||||||
$longUrl,
|
$longUrl,
|
||||||
$shortCodeData->getTags(),
|
$shortCodeData->getTags(),
|
||||||
$shortCodeMeta->getValidSince(),
|
$shortCodeMeta->getValidSince(),
|
||||||
|
@ -73,15 +73,9 @@ abstract class AbstractCreateShortCodeAction extends AbstractRestAction
|
||||||
$customSlug,
|
$customSlug,
|
||||||
$shortCodeMeta->getMaxVisits()
|
$shortCodeMeta->getMaxVisits()
|
||||||
);
|
);
|
||||||
$shortUrl = (new Uri())->withPath($shortCode)
|
$transformer = new ShortUrlDataTransformer($this->domainConfig);
|
||||||
->withScheme($this->domainConfig['schema'])
|
|
||||||
->withHost($this->domainConfig['hostname']);
|
|
||||||
|
|
||||||
return new JsonResponse([
|
return new JsonResponse($transformer->transform($shortUrl));
|
||||||
'longUrl' => (string) $longUrl,
|
|
||||||
'shortUrl' => (string) $shortUrl,
|
|
||||||
'shortCode' => $shortCode,
|
|
||||||
]);
|
|
||||||
} catch (InvalidUrlException $e) {
|
} catch (InvalidUrlException $e) {
|
||||||
$this->logger->warning('Provided Invalid URL.' . PHP_EOL . $e);
|
$this->logger->warning('Provided Invalid URL.' . PHP_EOL . $e);
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
|
|
|
@ -6,6 +6,7 @@ namespace ShlinkioTest\Shlink\Rest\Action\ShortCode;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
||||||
|
@ -50,7 +51,10 @@ class CreateShortCodeActionTest extends TestCase
|
||||||
public function properShortcodeConversionReturnsData()
|
public function properShortcodeConversionReturnsData()
|
||||||
{
|
{
|
||||||
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
|
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
|
||||||
->willReturn('abc123')
|
->willReturn(
|
||||||
|
(new ShortUrl())->setShortCode('abc123')
|
||||||
|
->setLongUrl('')
|
||||||
|
)
|
||||||
->shouldBeCalledTimes(1);
|
->shouldBeCalledTimes(1);
|
||||||
|
|
||||||
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
|
||||||
|
|
|
@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Psr\Http\Message\UriInterface;
|
use Psr\Http\Message\UriInterface;
|
||||||
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
||||||
use Shlinkio\Shlink\Rest\Action\ShortCode\SingleStepCreateShortCodeAction;
|
use Shlinkio\Shlink\Rest\Action\ShortCode\SingleStepCreateShortCodeAction;
|
||||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||||
|
@ -112,7 +113,7 @@ class SingleStepCreateShortCodeActionTest extends TestCase
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null
|
null
|
||||||
);
|
)->willReturn((new ShortUrl())->setLongUrl(''));
|
||||||
|
|
||||||
$resp = $this->action->handle($request);
|
$resp = $this->action->handle($request);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue