mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-24 08:00:13 -06:00
Add method to check if an API exists for a given name
This commit is contained in:
parent
6f95acc202
commit
4c1ff72438
20
module/Core/src/Repository/EntityRepositoryInterface.php
Normal file
20
module/Core/src/Repository/EntityRepositoryInterface.php
Normal 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;
|
||||
}
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user