Inject ShortUrlRepository in ShortCodeUniquenessHelper

This commit is contained in:
Alejandro Celaya
2024-11-09 09:47:47 +01:00
parent 3ec24e3c67
commit fca3891819
3 changed files with 16 additions and 24 deletions

View File

@@ -4,25 +4,21 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
class ShortCodeUniquenessHelper implements ShortCodeUniquenessHelperInterface
readonly class ShortCodeUniquenessHelper implements ShortCodeUniquenessHelperInterface
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly UrlShortenerOptions $options,
) {
public function __construct(private ShortUrlRepositoryInterface $repo, private UrlShortenerOptions $options)
{
}
public function ensureShortCodeUniqueness(ShortUrl $shortUrlToBeCreated, bool $hasCustomSlug): bool
{
/** @var ShortUrlRepository $repo */
$repo = $this->em->getRepository(ShortUrl::class);
$otherShortUrlsExist = $repo->shortCodeIsInUseWithLock(ShortUrlIdentifier::fromShortUrl($shortUrlToBeCreated));
$identifier = ShortUrlIdentifier::fromShortUrl($shortUrlToBeCreated);
$otherShortUrlsExist = $this->repo->shortCodeIsInUseWithLock($identifier);
if (! $otherShortUrlsExist) {
return true;