Url shortening demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

43 lines
1.1 KiB

<?php
declare(strict_types=1);
namespace App\Repository;
use App\Entity\Url;
use App\Exception\InvalidHashException;
use App\Hasher;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
final class UrlRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, private Hasher $hasher)
{
parent::__construct($registry, Url::class);
}
public function findOneByAny(string $string): ?Url
{
$found = $this->findOneByCustomString($string);
if ($found === null) {
$found = $this->findOneByHash($string);
}
return $found;
}
public function findOneByCustomString(string $customString): ?Url
{
return $this->findOneBy(['customString' => $customString]);
}
public function findOneByHash(string $hash): ?Url
{
try {
$id = $this->hasher->decode($hash);
return $this->find($id);
} catch (InvalidHashException $e) {
return null;
}
}
}