-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazyWithClientAsClassMemberFactory.php
More file actions
40 lines (32 loc) · 1.65 KB
/
LazyWithClientAsClassMemberFactory.php
File metadata and controls
40 lines (32 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php declare(strict_types=1);
namespace de\codenamephp\platform\secretsManager\base\Secret\Proxy\String\Factory;
use Closure;
use de\codenamephp\platform\secretsManager\base\Client\ClientInterface;
use de\codenamephp\platform\secretsManager\base\Secret\Proxy\String\StringProxyInterface;
use de\codenamephp\platform\secretsManager\base\Secret\SecretInterface;
/**
* Wrapper for the WithClientAsClassMemberFactory that uses a callback to create the client lazy instead of passing it in the constructor. This makes it
* possible to only create the client when it is actually needed which is useful when some users never need to use anything that needs the client. So they
* don't even need to install the client library with auth etc.
*/
final class LazyWithClientAsClassMemberFactory implements StringProxyFactoryInterface {
public WithClientAsClassMemberFactory $proxyFactory;
/**
* @param Closure():ClientInterface $clientFactory A closure that returns the client when called. This is used to create the client lazy instead of passing it
*/
public function __construct(
public readonly Closure $clientFactory,
) {}
/**
* @psalm-suppress RedundantPropertyInitializationCheck that's what makes it lazy!
*/
public function getProxyFactory() : WithClientAsClassMemberFactory {
return $this->proxyFactory ??= new WithClientAsClassMemberFactory(($this->clientFactory)());
}
public function build(SecretInterface $secret) : StringProxyInterface {
return $this->getProxyFactory()->build($secret);
}
public function buildMultiple(SecretInterface ...$secrets) : array {
return $this->getProxyFactory()->buildMultiple(...$secrets);
}
}