Updated short URL creation responses to include more information

This commit is contained in:
Alejandro Celaya 2018-09-12 20:32:58 +02:00
parent 17779dbbc6
commit f3c92f4110
10 changed files with 39 additions and 32 deletions

View file

@ -115,10 +115,10 @@ class GenerateShortcodeCommand extends Command
$this->getOptionalDate($input, 'validUntil'),
$customSlug,
$maxVisits !== null ? (int) $maxVisits : null
);
$shortUrl = (new Uri())->withPath($shortCode)
->withScheme($this->domainConfig['schema'])
->withHost($this->domainConfig['hostname']);
)->getShortCode();
$shortUrl = (string) (new Uri())->withPath($shortCode)
->withScheme($this->domainConfig['schema'])
->withHost($this->domainConfig['hostname']);
$io->writeln([
\sprintf('%s <info>%s</info>', $this->translator->translate('Processed long URL:'), $longUrl),

View file

@ -117,7 +117,7 @@ class ListShortcodesCommand extends Command
foreach ($result as $row) {
$shortUrl = $transformer->transform($row);
if ($showTags) {
$shortUrl['tags'] = implode(', ', $shortUrl['tags']);
$shortUrl['tags'] = \implode(', ', $shortUrl['tags']);
} else {
unset($shortUrl['tags']);
}
@ -146,7 +146,7 @@ class ListShortcodesCommand extends Command
return null;
}
$orderBy = explode(',', $orderBy);
return count($orderBy) === 1 ? $orderBy[0] : [$orderBy[0] => $orderBy[1]];
$orderBy = \explode(',', $orderBy);
return \count($orderBy) === 1 ? $orderBy[0] : [$orderBy[0] => $orderBy[1]];
}
}

View file

@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\Shortcode\GenerateShortcodeCommand;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Symfony\Component\Console\Application;
@ -41,8 +42,12 @@ class GenerateShortcodeCommandTest extends TestCase
*/
public function properShortCodeIsCreatedIfLongUrlIsCorrect()
{
$this->urlShortener->urlToShortCode(Argument::cetera())->willReturn('abc123')
->shouldBeCalledTimes(1);
$this->urlShortener->urlToShortCode(Argument::cetera())
->willReturn(
(new ShortUrl())->setShortCode('abc123')
->setLongUrl('')
)
->shouldBeCalledTimes(1);
$this->commandTester->execute([
'command' => 'shortcode:generate',

View file

@ -81,7 +81,7 @@ class UrlShortener implements UrlShortenerInterface
\DateTime $validUntil = null,
string $customSlug = null,
int $maxVisits = null
): string {
): ShortUrl {
// If the URL validation is enabled, check that the URL actually exists
if ($this->urlValidationEnabled) {
$this->checkUrlExists($url);
@ -108,7 +108,7 @@ class UrlShortener implements UrlShortenerInterface
$this->em->flush();
$this->em->commit();
return $shortCode;
return $shortUrl;
} catch (\Throwable $e) {
if ($this->em->getConnection()->isTransactionActive()) {
$this->em->rollback();

View file

@ -33,7 +33,7 @@ interface UrlShortenerInterface
\DateTime $validUntil = null,
string $customSlug = null,
int $maxVisits = null
): string;
): ShortUrl;
/**
* Tries to find the mapped URL for provided short code. Returns null if not found

View file

@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Transformer;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Tag;
use Zend\Diactoros\Uri;
class ShortUrlDataTransformer implements DataTransformerInterface
{
@ -31,12 +32,7 @@ class ShortUrlDataTransformer implements DataTransformerInterface
return [
'shortCode' => $shortCode,
'shortUrl' => \sprintf(
'%s://%s/%s',
$this->domainConfig['schema'] ?? 'http',
$this->domainConfig['hostname'] ?? '',
$shortCode
),
'shortUrl' => $this->buildShortUrl($shortCode),
'longUrl' => $longUrl,
'dateCreated' => $dateCreated !== null ? $dateCreated->format(\DateTime::ATOM) : null,
'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
{
return $tag->getName();

View file

@ -85,8 +85,8 @@ class UrlShortenerTest extends TestCase
public function urlIsProperlyShortened()
{
// 10 -> 12C1c
$shortCode = $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
$this->assertEquals('12C1c', $shortCode);
$shortUrl = $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
$this->assertEquals('12C1c', $shortUrl->getShortCode());
}
/**

View file

@ -11,10 +11,10 @@ use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Model\CreateShortCodeData;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\Uri;
use Zend\I18n\Translator\TranslatorInterface;
abstract class AbstractCreateShortCodeAction extends AbstractRestAction
@ -65,7 +65,7 @@ abstract class AbstractCreateShortCodeAction extends AbstractRestAction
}
try {
$shortCode = $this->urlShortener->urlToShortCode(
$shortUrl = $this->urlShortener->urlToShortCode(
$longUrl,
$shortCodeData->getTags(),
$shortCodeMeta->getValidSince(),
@ -73,15 +73,9 @@ abstract class AbstractCreateShortCodeAction extends AbstractRestAction
$customSlug,
$shortCodeMeta->getMaxVisits()
);
$shortUrl = (new Uri())->withPath($shortCode)
->withScheme($this->domainConfig['schema'])
->withHost($this->domainConfig['hostname']);
$transformer = new ShortUrlDataTransformer($this->domainConfig);
return new JsonResponse([
'longUrl' => (string) $longUrl,
'shortUrl' => (string) $shortUrl,
'shortCode' => $shortCode,
]);
return new JsonResponse($transformer->transform($shortUrl));
} catch (InvalidUrlException $e) {
$this->logger->warning('Provided Invalid URL.' . PHP_EOL . $e);
return new JsonResponse([

View file

@ -6,6 +6,7 @@ namespace ShlinkioTest\Shlink\Rest\Action\ShortCode;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Service\UrlShortener;
@ -50,7 +51,10 @@ class CreateShortCodeActionTest extends TestCase
public function properShortcodeConversionReturnsData()
{
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
->willReturn('abc123')
->willReturn(
(new ShortUrl())->setShortCode('abc123')
->setLongUrl('')
)
->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withParsedBody([

View file

@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\UriInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Rest\Action\ShortCode\SingleStepCreateShortCodeAction;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
@ -112,7 +113,7 @@ class SingleStepCreateShortCodeActionTest extends TestCase
null,
null,
null
);
)->willReturn((new ShortUrl())->setLongUrl(''));
$resp = $this->action->handle($request);