|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpLightning\Invoice; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\AbstractConfig; |
|
8
|
|
|
use PhpLightning\Invoice\Domain\Transfer\SendableRange; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
|
|
11
|
|
|
final class InvoiceConfig extends AbstractConfig |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var int 100 Minimum in msat (sat/1000) */ |
|
14
|
|
|
private const DEFAULT_MIN_SENDABLE_IN_MILLISATS = 100_000; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** @var int 10 000 000 Max in msat (sat/1000) */ |
|
17
|
|
|
private const DEFAULT_MAX_SENDABLE_IN_MILLISATS = 10_000_000_000; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
2 |
|
public function getCallback(): string |
|
20
|
|
|
{ |
|
21
|
2 |
|
return sprintf('https://%s/%s', $this->getDomain(), $this->getReceiver()); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
2 |
|
public function getLnAddress(): string |
|
25
|
|
|
{ |
|
26
|
2 |
|
return sprintf('%s@%s', $this->getReceiver(), $this->getDomain()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return array{ |
|
|
|
|
|
|
31
|
|
|
* api_endpoint: string, |
|
32
|
|
|
* api_key: string, |
|
33
|
|
|
* } |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getBackendOptionsFor(string $backend): array |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var array{api_endpoint?: string, api_key?: string} $result */ |
|
38
|
|
|
$result = $this->get('backends')[$backend] ?? []; // @phpstan-ignore-line |
|
39
|
|
|
|
|
40
|
|
|
if (!isset($result['api_endpoint'], $result['api_key'])) { |
|
41
|
|
|
throw new RuntimeException('Missing backend options for ' . $backend); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $result; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function getSendableRange(): SendableRange |
|
48
|
|
|
{ |
|
49
|
2 |
|
return SendableRange::withMinMax( |
|
50
|
2 |
|
(int)$this->get('min-sendable', self::DEFAULT_MIN_SENDABLE_IN_MILLISATS), |
|
51
|
2 |
|
(int)$this->get('max-sendable', self::DEFAULT_MAX_SENDABLE_IN_MILLISATS), |
|
52
|
2 |
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
private function getDomain(): string |
|
56
|
|
|
{ |
|
57
|
2 |
|
return (string)$this->get('domain', $_SERVER['HTTP_HOST'] ?? 'localhost'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
private function getReceiver(): string |
|
61
|
|
|
{ |
|
62
|
2 |
|
return (string)$this->get('receiver', $_SERVER['REQUEST_URI'] ?? 'unknown-receiver'); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|