Skip to content

Commit 325d58b

Browse files
author
Bastian Schwarz
committed
Created factory
1 parent 3aebeca commit 325d58b

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace de\codenamephp\platform\secretsManager\base\Secret\Proxy\String\Factory;
4+
5+
use de\codenamephp\platform\secretsManager\base\Secret\Proxy\String\StringProxyInterface;
6+
use de\codenamephp\platform\secretsManager\base\Secret\SecretInterface;
7+
8+
/**
9+
* Interface for factories that create string proxy instances
10+
*/
11+
interface StringProxyFactoryInterface {
12+
13+
/**
14+
* Builds a single proxy instance using the given secret
15+
*
16+
* @param SecretInterface $secret The secret to build the proxy for
17+
* @return StringProxyInterface The built proxy
18+
*/
19+
public function build(SecretInterface $secret) : StringProxyInterface;
20+
21+
/**
22+
* Builds multiple proxy instances using the given secrets. Implementations MUST preserve the order and keys (if given) of the secrets.
23+
* Reminder you can use named parameters in variadic to use as array keys.
24+
*
25+
* @param SecretInterface ...$secrets The secrets to build the proxies for
26+
* @return array<StringProxyInterface> The built proxies
27+
*/
28+
public function buildMultiple(SecretInterface ...$secrets) : array;
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace de\codenamephp\platform\secretsManager\base\Secret\Proxy\String\Factory;
4+
5+
use de\codenamephp\platform\secretsManager\base\Client\ClientInterface;
6+
use de\codenamephp\platform\secretsManager\base\Secret\Proxy\String\StringProxyInterface;
7+
use de\codenamephp\platform\secretsManager\base\Secret\Proxy\String\WithClientAsClassMember;
8+
use de\codenamephp\platform\secretsManager\base\Secret\SecretInterface;
9+
10+
/**
11+
* Uses the client from the constructor and creates instances of WithClientAsClassMember. The multiple just maps the array of secrets to the build method.
12+
*/
13+
final class WithClientAsClassMemberFactory implements StringProxyFactoryInterface {
14+
15+
public function __construct(
16+
public readonly ClientInterface $client,
17+
) {}
18+
19+
public function build(SecretInterface $secret) : StringProxyInterface {
20+
return new WithClientAsClassMember($this->client, $secret);
21+
}
22+
23+
public function buildMultiple(SecretInterface ...$secrets) : array {
24+
return array_map(fn(SecretInterface $secret) => $this->build($secret), $secrets);
25+
}
26+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace de\codenamephp\platform\secretsManager\base\test\Secret\Proxy\String\Factory;
4+
5+
use de\codenamephp\platform\secretsManager\base\Client\ClientInterface;
6+
use de\codenamephp\platform\secretsManager\base\Secret\Proxy\String\Factory\WithClientAsClassMemberFactory;
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 WithClientAsClassMemberFactoryTest extends TestCase {
12+
13+
public function test__construct() : void {
14+
$client = $this->createMock(ClientInterface::class);
15+
16+
$sut = new WithClientAsClassMemberFactory($client);
17+
18+
self::assertSame($client, $sut->client);
19+
}
20+
21+
public function testBuild() : void {
22+
$client = $this->createMock(ClientInterface::class);
23+
$secret = $this->createMock(SecretInterface::class);
24+
25+
$sut = new WithClientAsClassMemberFactory($client);
26+
27+
$result = $sut->build($secret);
28+
29+
self::assertInstanceOf(WithClientAsClassMember::class, $result);
30+
self::assertSame($client, $result->client);
31+
self::assertSame($secret, $result->secret);
32+
}
33+
34+
public function testBuildMultiple() : void {
35+
$client = $this->createMock(ClientInterface::class);
36+
$secret1 = $this->createMock(SecretInterface::class);
37+
$secret2 = $this->createMock(SecretInterface::class);
38+
39+
$sut = new WithClientAsClassMemberFactory($client);
40+
41+
$result = $sut->buildMultiple($secret1, $secret2);
42+
43+
self::assertIsArray($result);
44+
self::assertCount(2, $result);
45+
self::assertInstanceOf(WithClientAsClassMember::class, $result[0]);
46+
self::assertSame($client, $result[0]->client);
47+
self::assertSame($secret1, $result[0]->secret);
48+
self::assertInstanceOf(WithClientAsClassMember::class, $result[1]);
49+
self::assertSame($client, $result[1]->client);
50+
self::assertSame($secret2, $result[1]->secret);
51+
}
52+
}

0 commit comments

Comments
 (0)