|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Threema GmbH |
|
4
|
|
|
* @copyright Copyright (c) 2015-2016 Threema GmbH |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
namespace Threema\MsgApi; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Interface PublicKeyStore |
|
12
|
|
|
* Store the fetched Public Keys |
|
13
|
|
|
* |
|
14
|
|
|
* @package Threema\MsgApi |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class PublicKeyStore { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* threemaId => publicKey cache |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $cache = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* return null if the public key not found in the store |
|
26
|
|
|
* @param string $threemaId |
|
27
|
|
|
* @return string|null |
|
28
|
|
|
*/ |
|
29
|
|
|
public final function getPublicKey($threemaId) { |
|
30
|
|
|
if(array_key_exists($threemaId, $this->cache)) { |
|
31
|
|
|
return $this->cache[$threemaId]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$publicKey = $this->findPublicKey($threemaId); |
|
35
|
|
|
if(null !== $publicKey) { |
|
36
|
|
|
$this->cache[$threemaId] = $publicKey; |
|
37
|
|
|
} |
|
38
|
|
|
return $publicKey; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* return null if the public key not found in the store |
|
43
|
|
|
* @param string $threemaId |
|
44
|
|
|
* @return string|null |
|
45
|
|
|
*/ |
|
46
|
|
|
abstract protected function findPublicKey($threemaId); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* set and save a public key |
|
50
|
|
|
* @param string $threemaId |
|
51
|
|
|
* @param string $publicKey |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
final public function setPublicKey($threemaId, $publicKey) { |
|
55
|
|
|
$this->cache[$threemaId] = $publicKey; |
|
56
|
|
|
return $this->savePublicKey($threemaId, $publicKey); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* save a public key |
|
61
|
|
|
* @param string $threemaId |
|
62
|
|
|
* @param string $publicKey |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
abstract protected function savePublicKey($threemaId, $publicKey); |
|
66
|
|
|
} |
|
67
|
|
|
|