From dd1bc49b7927c0e5587d82a4f71918d81557f089 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 6 Aug 2016 18:08:09 +0200 Subject: [PATCH] Added method to ApiKeyService to list api keys --- module/Rest/src/Service/ApiKeyService.php | 12 +++++++++ .../src/Service/ApiKeyServiceInterface.php | 8 ++++++ .../Rest/test/Service/ApiKeyServiceTest.php | 26 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/module/Rest/src/Service/ApiKeyService.php b/module/Rest/src/Service/ApiKeyService.php index 3aa13b62..00fddfad 100644 --- a/module/Rest/src/Service/ApiKeyService.php +++ b/module/Rest/src/Service/ApiKeyService.php @@ -82,4 +82,16 @@ class ApiKeyService implements ApiKeyServiceInterface $this->em->flush(); return $apiKey; } + + /** + * Lists all existing appi keys + * + * @param bool $enabledOnly Tells if only enabled keys should be returned + * @return ApiKey[] + */ + public function listKeys($enabledOnly = false) + { + $conditions = $enabledOnly ? ['enabled' => true] : []; + return $this->em->getRepository(ApiKey::class)->findBy($conditions); + } } diff --git a/module/Rest/src/Service/ApiKeyServiceInterface.php b/module/Rest/src/Service/ApiKeyServiceInterface.php index 0c1f526b..84c856ce 100644 --- a/module/Rest/src/Service/ApiKeyServiceInterface.php +++ b/module/Rest/src/Service/ApiKeyServiceInterface.php @@ -28,4 +28,12 @@ interface ApiKeyServiceInterface * @return ApiKey */ public function disable($key); + + /** + * Lists all existing appi keys + * + * @param bool $enabledOnly Tells if only enabled keys should be returned + * @return ApiKey[] + */ + public function listKeys($enabledOnly = false); } diff --git a/module/Rest/test/Service/ApiKeyServiceTest.php b/module/Rest/test/Service/ApiKeyServiceTest.php index 7375c83d..7ab46432 100644 --- a/module/Rest/test/Service/ApiKeyServiceTest.php +++ b/module/Rest/test/Service/ApiKeyServiceTest.php @@ -139,4 +139,30 @@ class ApiKeyServiceTest extends TestCase $this->assertFalse($key->isEnabled()); $this->assertSame($key, $returnedKey); } + + /** + * @test + */ + public function listFindsAllApiKeys() + { + $repo = $this->prophesize(EntityRepository::class); + $repo->findBy([])->willReturn([]) + ->shouldBeCalledTimes(1); + $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); + + $this->service->listKeys(); + } + + /** + * @test + */ + public function listEnabledFindsOnlyEnabledApiKeys() + { + $repo = $this->prophesize(EntityRepository::class); + $repo->findBy(['enabled' => true])->willReturn([]) + ->shouldBeCalledTimes(1); + $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); + + $this->service->listKeys(true); + } }