mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-23 21:27:44 +03:00
Merge pull request #194 from acelaya/feature/create-url-resp
Updated short URL creation responses to include more information
This commit is contained in:
commit
c32e2053c3
11 changed files with 52 additions and 34 deletions
|
@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\CLI\Command\Shortcode;
|
|||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
||||
use Shlinkio\Shlink\Core\Util\ShortUrlBuilderTrait;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
|
@ -17,7 +18,9 @@ use Zend\I18n\Translator\TranslatorInterface;
|
|||
|
||||
class GenerateShortcodeCommand extends Command
|
||||
{
|
||||
const NAME = 'shortcode:generate';
|
||||
use ShortUrlBuilderTrait;
|
||||
|
||||
public const NAME = 'shortcode:generate';
|
||||
|
||||
/**
|
||||
* @var UrlShortenerInterface
|
||||
|
@ -115,10 +118,8 @@ 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 = $this->buildShortUrl($this->domainConfig, $shortCode);
|
||||
|
||||
$io->writeln([
|
||||
\sprintf('%s <info>%s</info>', $this->translator->translate('Processed long URL:'), $longUrl),
|
||||
|
|
|
@ -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]];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -69,7 +69,6 @@ class UrlShortener implements UrlShortenerInterface
|
|||
* @param \DateTime|null $validUntil
|
||||
* @param string|null $customSlug
|
||||
* @param int|null $maxVisits
|
||||
* @return string
|
||||
* @throws NonUniqueSlugException
|
||||
* @throws InvalidUrlException
|
||||
* @throws RuntimeException
|
||||
|
@ -81,7 +80,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 +107,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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -6,9 +6,12 @@ namespace Shlinkio\Shlink\Core\Transformer;
|
|||
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Entity\Tag;
|
||||
use Shlinkio\Shlink\Core\Util\ShortUrlBuilderTrait;
|
||||
|
||||
class ShortUrlDataTransformer implements DataTransformerInterface
|
||||
{
|
||||
use ShortUrlBuilderTrait;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
|
@ -31,12 +34,7 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
|||
|
||||
return [
|
||||
'shortCode' => $shortCode,
|
||||
'shortUrl' => \sprintf(
|
||||
'%s://%s/%s',
|
||||
$this->domainConfig['schema'] ?? 'http',
|
||||
$this->domainConfig['hostname'] ?? '',
|
||||
$shortCode
|
||||
),
|
||||
'shortUrl' => $this->buildShortUrl($this->domainConfig, $shortCode),
|
||||
'longUrl' => $longUrl,
|
||||
'dateCreated' => $dateCreated !== null ? $dateCreated->format(\DateTime::ATOM) : null,
|
||||
'visitsCount' => $value->getVisitsCount(),
|
||||
|
|
16
module/Core/src/Util/ShortUrlBuilderTrait.php
Normal file
16
module/Core/src/Util/ShortUrlBuilderTrait.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Util;
|
||||
|
||||
use Zend\Diactoros\Uri;
|
||||
|
||||
trait ShortUrlBuilderTrait
|
||||
{
|
||||
private function buildShortUrl(array $domainConfig, string $shortCode): string
|
||||
{
|
||||
return (string) (new Uri())->withPath($shortCode)
|
||||
->withScheme($domainConfig['schema'] ?? 'http')
|
||||
->withHost($domainConfig['hostname'] ?? '');
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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([
|
||||
|
|
|
@ -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([
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue