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

@@ -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();