mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-26 23:18:37 +03:00
Moved logic to generate random short codes to external function
This commit is contained in:
parent
2f09ff456c
commit
8f2e78c946
3 changed files with 24 additions and 10 deletions
|
@ -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": {
|
||||
|
|
18
module/Core/functions/functions.php
Normal file
18
module/Core/functions/functions.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core;
|
||||
|
||||
use PUGX\Shortid\Factory as ShortIdFactory;
|
||||
|
||||
function generateRandomShortCode(int $length = 5): string
|
||||
{
|
||||
static $shortIdFactory;
|
||||
if ($shortIdFactory === null) {
|
||||
$shortIdFactory = new ShortIdFactory();
|
||||
}
|
||||
|
||||
$alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
return $shortIdFactory->generate($length, $alphabet)->serialize();
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue