Add method to check if an API exists for a given name

This commit is contained in:
Alejandro Celaya 2024-11-07 09:55:06 +01:00
parent 6f95acc202
commit 4c1ff72438
5 changed files with 46 additions and 6 deletions

View File

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Repository;
use Doctrine\Persistence\ObjectRepository;
/**
* @template T of object
* @extends ObjectRepository<T>
*/
interface EntityRepositoryInterface extends ObjectRepository
{
/**
* @todo This should be part of ObjectRepository, so adding here until that interface defines it.
* EntityRepository already implements the method, so classes extending it won't have to add anything.
*/
public function count(array $criteria = []): int;
}

View File

@ -4,14 +4,14 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\ApiKey\Repository;
use Doctrine\Persistence\ObjectRepository;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepositoryInterface;
use Shlinkio\Shlink\Core\Repository\EntityRepositoryInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
/**
* @extends ObjectRepository<ApiKey>
* @extends EntityRepositoryInterface<ApiKey>
*/
interface ApiKeyRepositoryInterface extends ObjectRepository, EntitySpecificationRepositoryInterface
interface ApiKeyRepositoryInterface extends EntityRepositoryInterface, EntitySpecificationRepositoryInterface
{
/**
* Will create provided API key only if there's no API keys yet

View File

@ -42,7 +42,7 @@ readonly class ApiKeyService implements ApiKeyServiceInterface
*/
public function disableByName(string $apiKeyName): ApiKey
{
return $this->disableApiKey($this->findByName($apiKeyName));
return $this->disableApiKey($this->repo->findOneBy(['name' => $apiKeyName]));
}
/**
@ -79,8 +79,11 @@ readonly class ApiKeyService implements ApiKeyServiceInterface
return $this->repo->findOneBy(['key' => ApiKey::hashKey($key)]);
}
private function findByName(string $name): ApiKey|null
/**
* @inheritDoc
*/
public function existsWithName(string $apiKeyName): bool
{
return $this->repo->findOneBy(['name' => $name]);
return $this->repo->count(['name' => $apiKeyName]) > 0;
}
}

View File

@ -31,4 +31,9 @@ interface ApiKeyServiceInterface
* @return ApiKey[]
*/
public function listKeys(bool $enabledOnly = false): array;
/**
* Check if an API key exists for provided name
*/
public function existsWithName(string $apiKeyName): bool;
}

View File

@ -8,6 +8,7 @@ use Cake\Chronos\Chronos;
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
@ -176,4 +177,15 @@ class ApiKeyServiceTest extends TestCase
yield 'first api key' => [ApiKey::create()];
yield 'existing api keys' => [null];
}
#[Test]
#[TestWith([0, false])]
#[TestWith([1, true])]
#[TestWith([27, true])]
public function existsWithNameCountsEntriesInRepository(int $count, bool $expected): void
{
$name = 'the_key';
$this->repo->expects($this->once())->method('count')->with(['name' => $name])->willReturn($count);
self::assertEquals($this->service->existsWithName($name), $expected);
}
}