|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpLightning\Config; |
|
6
|
|
|
|
|
7
|
|
|
use JsonSerializable; |
|
8
|
|
|
use PhpLightning\Config\Backend\BackendConfigInterface; |
|
9
|
|
|
use PhpLightning\Config\Backend\LnBitsBackendConfig; |
|
10
|
|
|
use PhpLightning\Shared\Value\SendableRange; |
|
11
|
|
|
|
|
12
|
|
|
final class LightningConfig implements JsonSerializable |
|
13
|
|
|
{ |
|
14
|
|
|
private ?string $domain = null; |
|
15
|
|
|
private ?string $receiver = null; |
|
16
|
|
|
private ?SendableRange $sendableRange = null; |
|
17
|
|
|
private ?string $callbackUrl = null; |
|
18
|
|
|
private ?BackendsConfig $backends = null; |
|
19
|
|
|
|
|
20
|
4 |
|
public function setDomain(string $domain): self |
|
21
|
|
|
{ |
|
22
|
4 |
|
$parseUrl = parse_url($domain); |
|
23
|
4 |
|
$this->domain = $parseUrl['host'] ?? $domain; |
|
24
|
4 |
|
return $this; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
3 |
|
public function setReceiver(string $receiver): self |
|
28
|
|
|
{ |
|
29
|
3 |
|
$this->receiver = $receiver; |
|
30
|
3 |
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
3 |
|
public function setSendableRange(int $min, int $max): self |
|
34
|
|
|
{ |
|
35
|
3 |
|
$this->sendableRange = SendableRange::withMinMax($min, $max); |
|
36
|
3 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
public function setCallbackUrl(string $callbackUrl): self |
|
40
|
|
|
{ |
|
41
|
2 |
|
$this->callbackUrl = $callbackUrl; |
|
42
|
2 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array<string,BackendConfigInterface> $list |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setBackends(array $list): self |
|
49
|
|
|
{ |
|
50
|
|
|
$this->backends ??= new BackendsConfig(); |
|
51
|
|
|
foreach ($list as $username => $config) { |
|
52
|
|
|
$this->backends->add($username, $config); |
|
53
|
|
|
} |
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
public function addBackend(string $username, BackendConfigInterface $backendConfig): self |
|
58
|
|
|
{ |
|
59
|
3 |
|
$this->backends ??= new BackendsConfig(); |
|
60
|
3 |
|
$this->backends->add($username, $backendConfig); |
|
61
|
3 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public function addBackendsAsJson(string $path): self |
|
65
|
|
|
{ |
|
66
|
2 |
|
$jsonAsString = (string)file_get_contents($path); |
|
67
|
|
|
/** @var array<string, array{api_endpoint?:string, api_key?: string}> $json */ |
|
68
|
2 |
|
$json = json_decode($jsonAsString, true); |
|
69
|
|
|
|
|
70
|
2 |
|
foreach ($json as $user => $settings) { |
|
71
|
2 |
|
$this->addBackend( |
|
72
|
2 |
|
$user, |
|
73
|
2 |
|
LnBitsBackendConfig::withEndpointAndKey( |
|
74
|
2 |
|
$settings['api_endpoint'] ?? '', |
|
75
|
2 |
|
$settings['api_key'] ?? '', |
|
76
|
2 |
|
), |
|
77
|
2 |
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
8 |
|
public function jsonSerialize(): array |
|
84
|
|
|
{ |
|
85
|
8 |
|
$result = []; |
|
86
|
8 |
|
if ($this->backends !== null) { |
|
87
|
3 |
|
$result['backends'] = $this->backends->jsonSerialize(); |
|
88
|
|
|
} |
|
89
|
8 |
|
if ($this->domain !== null) { |
|
90
|
4 |
|
$result['domain'] = $this->domain; |
|
91
|
|
|
} |
|
92
|
8 |
|
if ($this->receiver !== null) { |
|
93
|
3 |
|
$result['receiver'] = $this->receiver; |
|
94
|
|
|
} |
|
95
|
8 |
|
if ($this->sendableRange !== null) { |
|
96
|
3 |
|
$result['sendable-range'] = $this->sendableRange; |
|
97
|
|
|
} |
|
98
|
8 |
|
if ($this->callbackUrl !== null) { |
|
99
|
2 |
|
$result['callback-url'] = $this->callbackUrl; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
8 |
|
return $result; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|