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: /** @var array<string, string> */
14: private $secretKeys;
15:
16: /**
17: * @param array<string, string> $secretKeys
18: */
19: public function __construct($secretKeys = array())
20: {
21: $this->secretKeys = $secretKeys;
22: }
23:
24: /**
25: * @param string $keyId
26: * @return string
27: * @throws SecretKeyNotAvailableException
28: */
29: public function getSecretKey($keyId)
30: {
31: if (!isset($this->secretKeys[$keyId]) || is_null($this->secretKeys[$keyId])) {
32: throw new SecretKeyNotAvailableException($keyId, "could not find secret key for key id $keyId");
33: }
34: return $this->secretKeys[$keyId];
35: }
36:
37: /**
38: * Stores the given secret key for the given key id.
39: * @param string $keyId
40: * @param string $secretKey
41: */
42: public function storeSecretKey($keyId, $secretKey)
43: {
44: if (is_null($keyId) || strlen(trim($keyId)) == 0) {
45: throw new UnexpectedValueException("keyId is required");
46: }
47: if (is_null($secretKey) || strlen(trim($secretKey)) == 0) {
48: throw new UnexpectedValueException("secretKey is required");
49: }
50: $this->secretKeys[$keyId] = $secretKey;
51: }
52:
53: /**
54: * Removes the secret key for the given key id.
55: * @param string $keyId
56: */
57: public function removeSecretKey($keyId)
58: {
59: unset($this->secretKeys[$keyId]);
60: }
61:
62: /**
63: * Removes all stored secret keys.
64: */
65: public function clear()
66: {
67: unset($this->secretKeys);
68: $this->secretKeys = array();
69: }
70: }
71: