1: <?php
2: namespace Worldline\Connect\Sdk\Webhooks;
3:
4: use UnexpectedValueException;
5:
6: /**
7: * Class InMemorySecretKeyStore
8: *
9: * @package Worldline\Connect\Sdk\Webhooks
10: */
11: class InMemorySecretKeyStore implements SecretKeyStore
12: {
13: /**
14: * @var array<string, string>
15: */
16: private array $secretKeys;
17:
18: /**
19: * @param array<string, string> $secretKeys
20: */
21: public function __construct(array $secretKeys = array())
22: {
23: $this->secretKeys = $secretKeys;
24: }
25:
26: /**
27: * @param string $keyId
28: *
29: * @return string
30: * @throws SecretKeyNotAvailableException
31: */
32: public function getSecretKey(string $keyId): string
33: {
34: if (!isset($this->secretKeys[$keyId]) || is_null($this->secretKeys[$keyId])) {
35: throw new SecretKeyNotAvailableException($keyId, "could not find secret key for key id $keyId");
36: }
37: return $this->secretKeys[$keyId];
38: }
39:
40: /**
41: * Stores the given secret key for the given key id.
42: *
43: * @param string $keyId
44: * @param string $secretKey
45: *
46: * @return void
47: */
48: public function storeSecretKey(string $keyId, string $secretKey): void
49: {
50: if (strlen(trim($keyId)) == 0) {
51: throw new UnexpectedValueException("keyId is required");
52: }
53: if (strlen(trim($secretKey)) == 0) {
54: throw new UnexpectedValueException("secretKey is required");
55: }
56: $this->secretKeys[$keyId] = $secretKey;
57: }
58:
59: /**
60: * Removes the secret key for the given key id.
61: *
62: * @param string $keyId
63: *
64: * @return void
65: */
66: public function removeSecretKey(string $keyId): void
67: {
68: unset($this->secretKeys[$keyId]);
69: }
70:
71: /**
72: * Removes all stored secret keys.
73: *
74: * @return void
75: */
76: public function clear(): void
77: {
78: unset($this->secretKeys);
79: $this->secretKeys = array();
80: }
81: }
82: