Skip to content

Commit 7cfa394

Browse files
Moved Secrets to own client (#275)
1 parent 7774066 commit 7cfa394

File tree

14 files changed

+325
-15
lines changed

14 files changed

+325
-15
lines changed

src/Account/Client.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,25 +283,31 @@ public function updateConfig(array $options): Config
283283
}
284284

285285
/**
286+
* @deprecated use the Vonage\Secrets\Client::list method
287+
*
286288
* @throws ClientExceptionInterface
287289
* @throws ClientException\Exception
288290
* @throws InvalidResponseException
289291
*/
290292
public function listSecrets(string $apiKey): SecretCollection
291293
{
294+
trigger_error('Vonage\Account\Client::listSecrets is deprecated, please use the Vonage\Secrets\Client::list method', E_USER_DEPRECATED);
292295
$api = $this->getSecretsAPI();
293296

294297
$data = $api->get($apiKey . '/secrets');
295298
return new SecretCollection($data['_embedded']['secrets'], $data['_links']);
296299
}
297300

298301
/**
302+
* @deprecated use the Vonage\Secrets\Client::get method
303+
*
299304
* @throws ClientExceptionInterface
300305
* @throws ClientException\Exception
301306
* @throws InvalidResponseException
302307
*/
303308
public function getSecret(string $apiKey, string $secretId): Secret
304309
{
310+
trigger_error('Vonage\Account\Client::getSecret is deprecated, please use the Vonage\Secrets\Client::get method', E_USER_DEPRECATED);
305311
$api = $this->getSecretsAPI();
306312

307313
$data = $api->get($apiKey . '/secrets/' . $secretId);
@@ -311,6 +317,8 @@ public function getSecret(string $apiKey, string $secretId): Secret
311317
/**
312318
* Create a new account secret
313319
*
320+
* @deprecated use the Vonage\Secrets\Client::create method
321+
*
314322
* @throws ClientExceptionInterface
315323
* @throws ClientRequestException
316324
* @throws ClientException\Exception
@@ -319,6 +327,7 @@ public function getSecret(string $apiKey, string $secretId): Secret
319327
*/
320328
public function createSecret(string $apiKey, string $newSecret): Secret
321329
{
330+
trigger_error('Vonage\Account\Client::createSecret is deprecated, please use the Vonage\Secrets\Client::create method', E_USER_DEPRECATED);
322331
$api = $this->getSecretsAPI();
323332
$api->setBaseUri('/accounts/' . $apiKey . '/secrets');
324333

@@ -345,11 +354,14 @@ public function createSecret(string $apiKey, string $newSecret): Secret
345354
}
346355

347356
/**
357+
* @deprecated use the Vonage\Secrets\Client::revoke method
358+
*
348359
* @throws ClientExceptionInterface
349360
* @throws ClientException\Exception
350361
*/
351362
public function deleteSecret(string $apiKey, string $secretId): void
352363
{
364+
trigger_error('Vonage\Account\Client::deleteSecret is deprecated, please use the Vonage\Secrets\Client::revoke method', E_USER_DEPRECATED);
353365
$api = $this->getSecretsAPI();
354366
$api->setBaseUri('/accounts/' . $apiKey . '/secrets');
355367
$api->delete($secretId);

src/Account/Secret.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use function get_class;
1919
use function trigger_error;
2020

21+
/**
22+
* @deprecated Use the Vonage\Secrets\Secret object instead
23+
*/
2124
class Secret implements ArrayAccess
2225
{
2326
protected $data;

src/Client.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use Vonage\Message\Client as MessageClient;
4848
use Vonage\Numbers\ClientFactory as NumbersClientFactory;
4949
use Vonage\Redact\ClientFactory as RedactClientFactory;
50+
use Vonage\Secrets\ClientFactory as SecretsClientFactory;
5051
use Vonage\SMS\ClientFactory as SMSClientFactory;
5152
use Vonage\User\Collection as UserCollection;
5253
use Vonage\Verify\ClientFactory as VerifyClientFactory;
@@ -83,6 +84,7 @@
8384
* @method Insights\Client insights()
8485
* @method Numbers\Client numbers()
8586
* @method Redact\Client redact()
87+
* @method Secrets\Client secrets()
8688
* @method SMS\Client sms()
8789
* @method Verify\Client verify()
8890
* @method Voice\Client voice()
@@ -212,6 +214,7 @@ public function __construct(CredentialsInterface $credentials, $options = [], ?C
212214
'insights' => InsightsClientFactory::class,
213215
'numbers' => NumbersClientFactory::class,
214216
'redact' => RedactClientFactory::class,
217+
'secrets' => SecretsClientFactory::class,
215218
'sms' => SMSClientFactory::class,
216219
'verify' => VerifyClientFactory::class,
217220
'voice' => VoiceClientFactory::class,

src/Client/Factory/FactoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ interface FactoryInterface
2222
public function hasApi(string $api): bool;
2323

2424
public function getApi(string $api);
25+
26+
public function make(string $key);
2527
}

src/Secrets/Client.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Vonage\Secrets;
4+
5+
use Vonage\Client\APIClient;
6+
use Vonage\Client\APIResource;
7+
use Vonage\Entity\Hydrator\ArrayHydrator;
8+
use Vonage\Entity\IterableAPICollection;
9+
10+
class Client implements APIClient
11+
{
12+
/**
13+
* @var APIResource
14+
*/
15+
protected $api;
16+
17+
public function __construct(APIResource $api)
18+
{
19+
$this->api = $api;
20+
}
21+
22+
public function getAPIResource(): APIResource
23+
{
24+
return $this->api;
25+
}
26+
27+
public function get(string $accountId, string $id): Secret
28+
{
29+
$data = $this->api->get("${accountId}/secrets/${id}");
30+
31+
return new Secret($data);
32+
}
33+
34+
public function list(string $accountId): IterableAPICollection
35+
{
36+
$collection = $this->api->search(null, "/accounts/${accountId}/secrets");
37+
$hydrator = new ArrayHydrator();
38+
$hydrator->setPrototype(new Secret());
39+
$collection->setHydrator($hydrator);
40+
41+
return $collection;
42+
}
43+
44+
public function create(string $accountId, string $secret): Secret
45+
{
46+
$response = $this->api->create(['secret' => $secret], "/${accountId}/secrets");
47+
return new Secret($response);
48+
}
49+
50+
public function revoke(string $accountId, string $id)
51+
{
52+
$this->api->delete("${accountId}/secrets/${id}");
53+
}
54+
}

src/Secrets/ClientFactory.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Vonage Client Library for PHP
5+
*
6+
* @copyright Copyright (c) 2016-2020 Vonage, Inc. (http://vonage.com)
7+
* @license https://github.com/Vonage/vonage-php-sdk-core/blob/master/LICENSE.txt Apache License 2.0
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Vonage\Secrets;
13+
14+
use Vonage\Client\APIResource;
15+
use Vonage\Client\Factory\FactoryInterface;
16+
17+
class ClientFactory
18+
{
19+
public function __invoke(FactoryInterface $container): Client
20+
{
21+
$api = $container->make(APIResource::class);
22+
$api->setBaseUri('/accounts')
23+
->setCollectionName('secrets');
24+
25+
return new Client($api);
26+
}
27+
}

src/Secrets/Secret.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Vonage\Secrets;
4+
5+
use DateTimeImmutable;
6+
use DateTimeInterface;
7+
use Vonage\Entity\Hydrator\ArrayHydrateInterface;
8+
9+
class Secret implements ArrayHydrateInterface
10+
{
11+
/**
12+
* @var DateTimeImmutable
13+
*/
14+
protected $createdAt;
15+
16+
/**
17+
* @var string
18+
*/
19+
protected $id;
20+
21+
public function __construct(array $data = [])
22+
{
23+
if (!empty($data)) {
24+
$this->fromArray($data);
25+
}
26+
}
27+
28+
public function getId(): string
29+
{
30+
return $this->id;
31+
}
32+
33+
public function getCreatedAt(): DateTimeInterface
34+
{
35+
return $this->createdAt;
36+
}
37+
38+
public function fromArray(array $data)
39+
{
40+
$this->id = $data['id'];
41+
$this->createdAt = new DateTimeImmutable($data['created_at']);
42+
}
43+
44+
public function toArray(): array
45+
{
46+
return [
47+
'id' => $this->id,
48+
'created_at' => $this->createdAt->format('Y-m-d\TH:i:s\Z'),
49+
];
50+
}
51+
}

test/Account/ClientTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ public function testListSecrets(): void
455455
return true;
456456
}))->shouldBeCalledTimes(1)->willReturn($this->getResponse('secret-management/list'));
457457

458-
$this->accountClient->listSecrets('abcd1234');
458+
@$this->accountClient->listSecrets('abcd1234');
459459
}
460460

461461
/**
@@ -471,7 +471,7 @@ public function testListSecretsServerError(): void
471471
Argument::any()
472472
)->willReturn($this->getGenericResponse('500', 500));
473473

474-
$this->accountClient->listSecrets('abcd1234');
474+
@$this->accountClient->listSecrets('abcd1234');
475475
}
476476

477477
/**
@@ -487,7 +487,7 @@ public function testListSecretsRequestError(): void
487487
Argument::any()
488488
)->willReturn($this->getGenericResponse('401', 401));
489489

490-
$this->accountClient->listSecrets('abcd1234');
490+
@$this->accountClient->listSecrets('abcd1234');
491491
}
492492

493493
/**
@@ -507,7 +507,7 @@ public function testGetSecret(): void
507507
return true;
508508
}))->shouldBeCalledTimes(1)->willReturn($this->getResponse('secret-management/get'));
509509

510-
$this->accountClient->getSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
510+
@$this->accountClient->getSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
511511
}
512512

513513
/**
@@ -523,7 +523,7 @@ public function testGetSecretsServerError(): void
523523
Argument::any()
524524
)->willReturn($this->getGenericResponse('500', 500));
525525

526-
$this->accountClient->getSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
526+
@$this->accountClient->getSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
527527
}
528528

529529
/**
@@ -539,7 +539,7 @@ public function testGetSecretsRequestError(): void
539539
Argument::any()
540540
)->willReturn($this->getGenericResponse('401', 401));
541541

542-
$this->accountClient->getSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
542+
@$this->accountClient->getSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
543543
}
544544

545545
/**
@@ -558,7 +558,7 @@ public function testCreateSecret(): void
558558
return true;
559559
}))->shouldBeCalledTimes(1)->willReturn($this->getResponse('secret-management/create'));
560560

561-
$this->accountClient->createSecret('abcd1234', 'example-4PI-secret');
561+
@$this->accountClient->createSecret('abcd1234', 'example-4PI-secret');
562562
}
563563

564564
/**
@@ -576,7 +576,7 @@ public function testCreateSecretsServerError(): void
576576
Argument::any()
577577
)->willReturn($this->getGenericResponse('500', 500));
578578

579-
$this->accountClient->createSecret('abcd1234', 'example-4PI-secret');
579+
@$this->accountClient->createSecret('abcd1234', 'example-4PI-secret');
580580
}
581581

582582
/**
@@ -592,7 +592,7 @@ public function testCreateSecretsRequestError(): void
592592

593593
$this->vonageClient->send(Argument::any())->willReturn($this->getGenericResponse('401', 401));
594594

595-
$this->accountClient->createSecret('abcd1234', 'example-4PI-secret');
595+
@$this->accountClient->createSecret('abcd1234', 'example-4PI-secret');
596596
}
597597

598598
/**
@@ -606,12 +606,12 @@ public function testCreateSecretsValidationError(): void
606606
try {
607607
$this->vonageClient->send(Argument::any())
608608
->willReturn($this->getResponse('secret-management/create-validation', 400));
609-
$this->accountClient->createSecret('abcd1234', 'example-4PI-secret');
609+
@$this->accountClient->createSecret('abcd1234', 'example-4PI-secret');
610610
} catch (ValidationException $e) {
611611
$this->assertEquals(
612612
'Bad Request: The request failed due to validation errors. ' .
613-
'See https://developer.nexmo.com/api-errors/account/secret-management#validation ' .
614-
'for more information',
613+
'See https://developer.nexmo.com/api-errors/account/secret-management#validation ' .
614+
'for more information',
615615
$e->getMessage()
616616
);
617617
$this->assertEquals(
@@ -642,7 +642,7 @@ public function testDeleteSecret(): void
642642
return true;
643643
}))->shouldBeCalledTimes(1)->willReturn($this->getResponse('secret-management/delete'));
644644

645-
$this->accountClient->deleteSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
645+
@$this->accountClient->deleteSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
646646
}
647647

648648
/**
@@ -653,7 +653,7 @@ public function testDeleteSecretsServerError(): void
653653
{
654654
$this->expectException(ClientException\Server::class);
655655
$this->vonageClient->send(Argument::any())->willReturn($this->getGenericResponse('500', 500));
656-
$this->accountClient->deleteSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
656+
@$this->accountClient->deleteSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
657657
}
658658

659659
/**
@@ -664,7 +664,7 @@ public function testDeleteSecretsRequestError(): void
664664
{
665665
$this->expectException(ClientException\Request::class);
666666
$this->vonageClient->send(Argument::any())->willReturn($this->getGenericResponse('401', 401));
667-
$this->accountClient->deleteSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
667+
@$this->accountClient->deleteSecret('abcd1234', 'ad6dc56f-07b5-46e1-a527-85530e625800');
668668
}
669669

670670
/**

test/HTTPTestTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace VonageTest;
4+
5+
use Laminas\Diactoros\Response;
6+
7+
trait HTTPTestTrait
8+
{
9+
use Psr7AssertionTrait;
10+
11+
/**
12+
* Get the API response we'd expect for a call to the API.
13+
*/
14+
protected function getResponse(string $type = 'success', int $status = 200): Response
15+
{
16+
return new Response(fopen($this->responsesDir . '/' . $type . '.json', 'rb'), $status);
17+
}
18+
}

0 commit comments

Comments
 (0)