mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Moved code to convert a ShortUrl into a full link as string to the entity itself
This commit is contained in:
parent
7b1857dcda
commit
1085809fa5
@ -5,7 +5,6 @@ namespace ShlinkMigrations;
|
|||||||
|
|
||||||
use Doctrine\DBAL\Schema\Schema;
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
use Doctrine\DBAL\Schema\SchemaException;
|
use Doctrine\DBAL\Schema\SchemaException;
|
||||||
use Doctrine\DBAL\Schema\Table;
|
|
||||||
use Doctrine\DBAL\Types\Type;
|
use Doctrine\DBAL\Types\Type;
|
||||||
use Doctrine\Migrations\AbstractMigration;
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@ use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
|||||||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
||||||
use Shlinkio\Shlink\Core\Util\ShortUrlBuilderTrait;
|
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
@ -26,8 +25,6 @@ use function sprintf;
|
|||||||
|
|
||||||
class GenerateShortUrlCommand extends Command
|
class GenerateShortUrlCommand extends Command
|
||||||
{
|
{
|
||||||
use ShortUrlBuilderTrait;
|
|
||||||
|
|
||||||
public const NAME = 'short-url:generate';
|
public const NAME = 'short-url:generate';
|
||||||
private const ALIASES = ['shortcode:generate', 'short-code:generate'];
|
private const ALIASES = ['shortcode:generate', 'short-code:generate'];
|
||||||
|
|
||||||
@ -119,7 +116,7 @@ class GenerateShortUrlCommand extends Command
|
|||||||
$maxVisits = $input->getOption('maxVisits');
|
$maxVisits = $input->getOption('maxVisits');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$shortCode = $this->urlShortener->urlToShortCode(
|
$shortUrl = $this->urlShortener->urlToShortCode(
|
||||||
new Uri($longUrl),
|
new Uri($longUrl),
|
||||||
$tags,
|
$tags,
|
||||||
ShortUrlMeta::createFromParams(
|
ShortUrlMeta::createFromParams(
|
||||||
@ -129,12 +126,11 @@ class GenerateShortUrlCommand extends Command
|
|||||||
$maxVisits !== null ? (int) $maxVisits : null,
|
$maxVisits !== null ? (int) $maxVisits : null,
|
||||||
$input->getOption('findIfExists')
|
$input->getOption('findIfExists')
|
||||||
)
|
)
|
||||||
)->getShortCode();
|
);
|
||||||
$shortUrl = $this->buildShortUrl($this->domainConfig, $shortCode);
|
|
||||||
|
|
||||||
$io->writeln([
|
$io->writeln([
|
||||||
sprintf('Processed long URL: <info>%s</info>', $longUrl),
|
sprintf('Processed long URL: <info>%s</info>', $longUrl),
|
||||||
sprintf('Generated short URL: <info>%s</info>', $shortUrl),
|
sprintf('Generated short URL: <info>%s</info>', $shortUrl->toString($this->domainConfig)),
|
||||||
]);
|
]);
|
||||||
return ExitCodes::EXIT_SUCCESS;
|
return ExitCodes::EXIT_SUCCESS;
|
||||||
} catch (InvalidUrlException $e) {
|
} catch (InvalidUrlException $e) {
|
||||||
|
@ -8,6 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
|
|||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
||||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||||
|
use Zend\Diactoros\Uri;
|
||||||
|
|
||||||
use function count;
|
use function count;
|
||||||
|
|
||||||
@ -135,7 +136,14 @@ class ShortUrl extends AbstractEntity
|
|||||||
return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits;
|
return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function domain(string $fallback = ''): string
|
public function toString(array $domainConfig): string
|
||||||
|
{
|
||||||
|
return (string) (new Uri())->withPath($this->shortCode)
|
||||||
|
->withScheme($domainConfig['schema'] ?? 'http')
|
||||||
|
->withHost($this->resolveDomain($domainConfig['hostname'] ?? ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveDomain(string $fallback = ''): string
|
||||||
{
|
{
|
||||||
if ($this->domain === null) {
|
if ($this->domain === null) {
|
||||||
return $fallback;
|
return $fallback;
|
||||||
|
@ -5,15 +5,12 @@ 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\Util\ShortUrlBuilderTrait;
|
|
||||||
|
|
||||||
use function Functional\invoke;
|
use function Functional\invoke;
|
||||||
use function Functional\invoke_if;
|
use function Functional\invoke_if;
|
||||||
|
|
||||||
class ShortUrlDataTransformer implements DataTransformerInterface
|
class ShortUrlDataTransformer implements DataTransformerInterface
|
||||||
{
|
{
|
||||||
use ShortUrlBuilderTrait;
|
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $domainConfig;
|
private $domainConfig;
|
||||||
|
|
||||||
@ -23,21 +20,20 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ShortUrl $value
|
* @param ShortUrl $shortUrl
|
||||||
*/
|
*/
|
||||||
public function transform($value): array
|
public function transform($shortUrl): array
|
||||||
{
|
{
|
||||||
$longUrl = $value->getLongUrl();
|
$longUrl = $shortUrl->getLongUrl();
|
||||||
$shortCode = $value->getShortCode();
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'shortCode' => $shortCode,
|
'shortCode' => $shortUrl->getShortCode(),
|
||||||
'shortUrl' => $this->buildShortUrl($this->domainConfig, $shortCode),
|
'shortUrl' => $shortUrl->toString($this->domainConfig),
|
||||||
'longUrl' => $longUrl,
|
'longUrl' => $longUrl,
|
||||||
'dateCreated' => $value->getDateCreated()->toAtomString(),
|
'dateCreated' => $shortUrl->getDateCreated()->toAtomString(),
|
||||||
'visitsCount' => $value->getVisitsCount(),
|
'visitsCount' => $shortUrl->getVisitsCount(),
|
||||||
'tags' => invoke($value->getTags(), '__toString'),
|
'tags' => invoke($shortUrl->getTags(), '__toString'),
|
||||||
'meta' => $this->buildMeta($value),
|
'meta' => $this->buildMeta($shortUrl),
|
||||||
|
|
||||||
// Deprecated
|
// Deprecated
|
||||||
'originalUrl' => $longUrl,
|
'originalUrl' => $longUrl,
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
<?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'] ?? '');
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user