Skip to content

Commit 3aebeca

Browse files
author
Bastian Schwarz
committed
Created proxy interface and implementation
1 parent 1b437a3 commit 3aebeca

3 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace de\codenamephp\platform\secretsManager\base\Secret\Proxy\String;
4+
5+
use de\codenamephp\platform\secretsManager\base\Secret\SecretInterface;
6+
use Stringable;
7+
8+
/**
9+
* Interface to proxy a secret that is a string and load it once lazily.
10+
*
11+
* Implementations should hold a client and a secret and fetch the payload once when it is needed and cache it.
12+
*/
13+
interface StringProxyInterface extends SecretInterface, Stringable
14+
{
15+
16+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace de\codenamephp\platform\secretsManager\base\Secret\Proxy\String;
4+
5+
use de\codenamephp\platform\secretsManager\base\Client\ClientInterface;
6+
use de\codenamephp\platform\secretsManager\base\Secret\Payload\PayloadInterface;
7+
use de\codenamephp\platform\secretsManager\base\Secret\SecretInterface;
8+
9+
/**
10+
* Proxy that just holds the secret, the client and the payload as members. When the payload is needed and is null
11+
* it is resolved with the client and set. All further calls to the payload will therefore use the resolved payload.
12+
*
13+
* Everything else is just delegated to the secret.
14+
*/
15+
final class WithClientAsClassMember implements StringProxyInterface {
16+
17+
public ?PayloadInterface $payload = null;
18+
19+
public function __construct(
20+
public readonly ClientInterface $client,
21+
public readonly SecretInterface $secret
22+
) {}
23+
24+
public function __toString() : string {
25+
return $this->fetchPayload()->getContent();
26+
}
27+
28+
public function getName() : string {
29+
return $this->secret->getName();
30+
}
31+
32+
public function getProject() : string {
33+
return $this->secret->getProject();
34+
}
35+
36+
public function getVersion() : string {
37+
return $this->secret->getVersion();
38+
}
39+
40+
public function fetchPayload() : PayloadInterface {
41+
return $this->payload ??= $this->client->fetchPayload($this->secret);
42+
}
43+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace de\codenamephp\platform\secretsManager\base\test\Secret\Proxy\String;
4+
5+
use de\codenamephp\platform\secretsManager\base\Client\ClientInterface;
6+
use de\codenamephp\platform\secretsManager\base\Secret\Payload\PayloadInterface;
7+
use de\codenamephp\platform\secretsManager\base\Secret\Proxy\String\WithClientAsClassMember;
8+
use de\codenamephp\platform\secretsManager\base\Secret\SecretInterface;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class WithClientAsClassMemberTest extends TestCase {
12+
13+
public function test__construct() : void {
14+
$secret = $this->createMock(SecretInterface::class);
15+
$client = $this->createMock(ClientInterface::class);
16+
17+
$sut = new WithClientAsClassMember($client, $secret);
18+
19+
self::assertSame($secret, $sut->secret);
20+
self::assertSame($client, $sut->client);
21+
}
22+
23+
public function testGetName() : void {
24+
$secret = $this->createMock(SecretInterface::class);
25+
$secret->expects(self::once())->method('getName')->willReturn('name');
26+
27+
$sut = new WithClientAsClassMember($this->createMock(ClientInterface::class), $secret);
28+
29+
self::assertSame('name', $sut->getName());
30+
}
31+
32+
public function testGetVersion() : void {
33+
$secret = $this->createMock(SecretInterface::class);
34+
$secret->expects(self::once())->method('getVersion')->willReturn('version');
35+
36+
$sut = new WithClientAsClassMember($this->createMock(ClientInterface::class), $secret);
37+
38+
self::assertSame('version', $sut->getVersion());
39+
}
40+
41+
public function testFetchPayload() : void {
42+
$secret = $this->createMock(SecretInterface::class);
43+
44+
$payload = $this->createMock(PayloadInterface::class);
45+
46+
$client = $this->createMock(ClientInterface::class);
47+
$client->expects(self::once())->method('fetchPayload')->with($secret)->willReturn($payload);
48+
49+
$sut = new WithClientAsClassMember($client, $secret);
50+
51+
self::assertSame($payload, $sut->fetchPayload());
52+
self::assertSame($payload, $sut->fetchPayload());
53+
self::assertSame($payload, $sut->fetchPayload());
54+
}
55+
56+
public function testGetProject() : void {
57+
$secret = $this->createMock(SecretInterface::class);
58+
$secret->expects(self::once())->method('getProject')->willReturn('project');
59+
60+
$sut = new WithClientAsClassMember($this->createMock(ClientInterface::class), $secret);
61+
62+
self::assertSame('project', $sut->getProject());
63+
}
64+
65+
public function test__toString() : void {
66+
$secret = $this->createMock(SecretInterface::class);
67+
68+
$payload = $this->createMock(PayloadInterface::class);
69+
$payload->expects(self::once())->method('getContent')->willReturn('content');
70+
71+
$client = $this->createMock(ClientInterface::class);
72+
$client->expects(self::once())->method('fetchPayload')->with($secret)->willReturn($payload);
73+
74+
$sut = new WithClientAsClassMember($client, $secret);
75+
76+
self::assertSame('content', (string) $sut);
77+
}
78+
}

0 commit comments

Comments
 (0)