From 8f2e78c9466fd896d88a77b93a9639f6ff753c89 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 11 Oct 2019 09:35:09 +0200 Subject: [PATCH] Moved logic to generate random short codes to external function --- composer.json | 5 ++++- module/Core/functions/functions.php | 18 ++++++++++++++++++ module/Core/src/Entity/ShortUrl.php | 11 ++--------- 3 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 module/Core/functions/functions.php diff --git a/composer.json b/composer.json index f488c757..9869ac4f 100644 --- a/composer.json +++ b/composer.json @@ -78,7 +78,10 @@ "Shlinkio\\Shlink\\Rest\\": "module/Rest/src", "Shlinkio\\Shlink\\Core\\": "module/Core/src", "Shlinkio\\Shlink\\PreviewGenerator\\": "module/PreviewGenerator/src/" - } + }, + "files": [ + "module/Core/functions/functions.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/module/Core/functions/functions.php b/module/Core/functions/functions.php new file mode 100644 index 00000000..92d40fe1 --- /dev/null +++ b/module/Core/functions/functions.php @@ -0,0 +1,18 @@ +generate($length, $alphabet)->serialize(); +} diff --git a/module/Core/src/Entity/ShortUrl.php b/module/Core/src/Entity/ShortUrl.php index 99e7c00a..cebe1785 100644 --- a/module/Core/src/Entity/ShortUrl.php +++ b/module/Core/src/Entity/ShortUrl.php @@ -7,7 +7,6 @@ namespace Shlinkio\Shlink\Core\Entity; use Cake\Chronos\Chronos; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; -use PUGX\Shortid\Factory as ShortIdFactory; use Shlinkio\Shlink\Common\Entity\AbstractEntity; use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface; use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver; @@ -18,11 +17,10 @@ use function array_reduce; use function count; use function Functional\contains; use function Functional\invoke; +use function Shlinkio\Shlink\Core\generateRandomShortCode; class ShortUrl extends AbstractEntity { - private const BASE62 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - /** @var string */ private $longUrl; /** @var string */ @@ -56,15 +54,10 @@ class ShortUrl extends AbstractEntity $this->validSince = $meta->getValidSince(); $this->validUntil = $meta->getValidUntil(); $this->maxVisits = $meta->getMaxVisits(); - $this->shortCode = $meta->getCustomSlug() ?? $this->generateShortCode(); + $this->shortCode = $meta->getCustomSlug() ?? generateRandomShortCode(); $this->domain = ($domainResolver ?? new SimpleDomainResolver())->resolveDomain($meta->getDomain()); } - private function generateShortCode(): string - { - return (new ShortIdFactory())->generate(6, self::BASE62)->serialize(); - } - public function getLongUrl(): string { return $this->longUrl;