Skip to content

Commit

Permalink
Inject ApiKeyRepository in ApiKeyService
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Nov 7, 2024
1 parent bd73362 commit 6f95acc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
5 changes: 4 additions & 1 deletion module/Rest/config/dependencies.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Mezzio\ProblemDetails\ProblemDetailsResponseFactory;
use Mezzio\Router\Middleware\ImplicitOptionsMiddleware;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Doctrine\EntityRepositoryFactory;
use Shlinkio\Shlink\Common\Mercure\LcobucciJwtProvider;
use Shlinkio\Shlink\Core\Config;
use Shlinkio\Shlink\Core\Domain\DomainService;
Expand All @@ -17,13 +18,15 @@
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
use Shlinkio\Shlink\Core\Tag\TagService;
use Shlinkio\Shlink\Core\Visit;
use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepository;
use Shlinkio\Shlink\Rest\Service\ApiKeyService;

return [

'dependencies' => [
'factories' => [
ApiKeyService::class => ConfigAbstractFactory::class,
ApiKeyRepository::class => [EntityRepositoryFactory::class, Entity\ApiKey::class],

Action\HealthAction::class => ConfigAbstractFactory::class,
Action\MercureInfoAction::class => ConfigAbstractFactory::class,
Expand Down Expand Up @@ -62,7 +65,7 @@
],

ConfigAbstractFactory::class => [
ApiKeyService::class => ['em'],
ApiKeyService::class => ['em', ApiKeyRepository::class],

Action\HealthAction::class => ['em', Config\Options\AppOptions::class],
Action\MercureInfoAction::class => [LcobucciJwtProvider::class, 'config.mercure'],
Expand Down
27 changes: 13 additions & 14 deletions module/Rest/src/Service/ApiKeyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

readonly class ApiKeyService implements ApiKeyServiceInterface
{
public function __construct(private EntityManagerInterface $em)
public function __construct(private EntityManagerInterface $em, private ApiKeyRepositoryInterface $repo)
{
}

Expand All @@ -28,14 +28,12 @@ public function create(ApiKeyMeta $apiKeyMeta): ApiKey

public function createInitial(string $key): ApiKey|null
{
/** @var ApiKeyRepositoryInterface $repo */
$repo = $this->em->getRepository(ApiKey::class);
return $repo->createInitialApiKey($key);
return $this->repo->createInitialApiKey($key);
}

public function check(string $key): ApiKeyCheckResult
{
$apiKey = $this->getByKey($key);
$apiKey = $this->findByKey($key);
return new ApiKeyCheckResult($apiKey);
}

Expand All @@ -44,17 +42,15 @@ public function check(string $key): ApiKeyCheckResult
*/
public function disableByName(string $apiKeyName): ApiKey
{
return $this->disableApiKey($this->em->getRepository(ApiKey::class)->findOneBy([
'name' => $apiKeyName,
]));
return $this->disableApiKey($this->findByName($apiKeyName));
}

/**
* @inheritDoc
*/
public function disableByKey(string $key): ApiKey
{
return $this->disableApiKey($this->getByKey($key));
return $this->disableApiKey($this->findByKey($key));
}

private function disableApiKey(ApiKey|null $apiKey): ApiKey
Expand All @@ -75,13 +71,16 @@ private function disableApiKey(ApiKey|null $apiKey): ApiKey
public function listKeys(bool $enabledOnly = false): array
{
$conditions = $enabledOnly ? ['enabled' => true] : [];
return $this->em->getRepository(ApiKey::class)->findBy($conditions);
return $this->repo->findBy($conditions);
}

private function getByKey(string $key): ApiKey|null
private function findByKey(string $key): ApiKey|null
{
return $this->em->getRepository(ApiKey::class)->findOneBy([
'key' => ApiKey::hashKey($key),
]);
return $this->repo->findOneBy(['key' => ApiKey::hashKey($key)]);
}

private function findByName(string $name): ApiKey|null
{
return $this->repo->findOneBy(['name' => $name]);
}
}
15 changes: 4 additions & 11 deletions module/Rest/test/Service/ApiKeyServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepository;
use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepositoryInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyService;

Expand All @@ -24,13 +24,13 @@ class ApiKeyServiceTest extends TestCase
{
private ApiKeyService $service;
private MockObject & EntityManager $em;
private MockObject & ApiKeyRepository $repo;
private MockObject & ApiKeyRepositoryInterface $repo;

protected function setUp(): void
{
$this->em = $this->createMock(EntityManager::class);
$this->repo = $this->createMock(ApiKeyRepository::class);
$this->service = new ApiKeyService($this->em);
$this->repo = $this->createMock(ApiKeyRepositoryInterface::class);
$this->service = new ApiKeyService($this->em, $this->repo);
}

/**
Expand Down Expand Up @@ -77,7 +77,6 @@ public function checkReturnsFalseForInvalidApiKeys(ApiKey|null $invalidKey): voi
$this->repo->expects($this->once())->method('findOneBy')->with(['key' => ApiKey::hashKey('12345')])->willReturn(
$invalidKey,
);
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);

$result = $this->service->check('12345');

Expand All @@ -102,7 +101,6 @@ public function checkReturnsTrueWhenConditionsAreFavorable(): void
$this->repo->expects($this->once())->method('findOneBy')->with(['key' => ApiKey::hashKey('12345')])->willReturn(
$apiKey,
);
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);

$result = $this->service->check('12345');

Expand All @@ -114,7 +112,6 @@ public function checkReturnsTrueWhenConditionsAreFavorable(): void
public function disableThrowsExceptionWhenNoApiKeyIsFound(string $disableMethod, array $findOneByArg): void
{
$this->repo->expects($this->once())->method('findOneBy')->with($findOneByArg)->willReturn(null);
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);

$this->expectException(InvalidArgumentException::class);

Expand All @@ -126,7 +123,6 @@ public function disableReturnsDisabledApiKeyWhenFound(string $disableMethod, arr
{
$key = ApiKey::create();
$this->repo->expects($this->once())->method('findOneBy')->with($findOneByArg)->willReturn($key);
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
$this->em->expects($this->once())->method('flush');

self::assertTrue($key->isEnabled());
Expand All @@ -147,7 +143,6 @@ public function listFindsAllApiKeys(): void
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];

$this->repo->expects($this->once())->method('findBy')->with([])->willReturn($expectedApiKeys);
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);

$result = $this->service->listKeys();

Expand All @@ -160,7 +155,6 @@ public function listEnabledFindsOnlyEnabledApiKeys(): void
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];

$this->repo->expects($this->once())->method('findBy')->with(['enabled' => true])->willReturn($expectedApiKeys);
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);

$result = $this->service->listKeys(enabledOnly: true);

Expand All @@ -171,7 +165,6 @@ public function listEnabledFindsOnlyEnabledApiKeys(): void
public function createInitialDelegatesToRepository(ApiKey|null $apiKey): void
{
$this->repo->expects($this->once())->method('createInitialApiKey')->with('the_key')->willReturn($apiKey);
$this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);

$result = $this->service->createInitial('the_key');

Expand Down

0 comments on commit 6f95acc

Please sign in to comment.