Skip to content

Commit

Permalink
Merge pull request #21 from php-lightning/allow-add-backends-as-json
Browse files Browse the repository at this point in the history
Allow add backends as json
  • Loading branch information
Chemaclass committed Apr 27, 2023
2 parents 7308241 + 64efb2c commit 30ce880
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
8 changes: 2 additions & 6 deletions lightning-config.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
->setSendableRange(min: 100_000, max: 10_000_000_000)
->setCallbackUrl('localhost:8000/callback')
->setBackends([
'user-1' => (new LnBitsBackendConfig())
->setApiEndpoint('http://localhost:5000')
->setApiKey('api_key-1'),
'user-2' => (new LnBitsBackendConfig())
->setApiEndpoint('http://localhost:5000')
->setApiKey('api_key-2')
'user-1' => LnBitsBackendConfig::withEndpointAndKey('http://localhost:5000', 'api_key-1'),
'user-2' => LnBitsBackendConfig::withEndpointAndKey('http://localhost:5000', 'api_key-2'),
]);
23 changes: 17 additions & 6 deletions src/Config/Backend/LnBitsBackendConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ final class LnBitsBackendConfig implements BackendConfigInterface
private string $apiEndpoint = 'http://localhost:5000';
private string $apiKey = '';

public function setApiEndpoint(string $apiEndpoint): self
private function __construct()
{
$this->apiEndpoint = rtrim($apiEndpoint, '/');
return $this;
}

public function setApiKey(string $apiKey): self
public static function withEndpointAndKey(string $endpoint, string $key): self
{
$this->apiKey = $apiKey;
return $this;
return (new self())
->setApiEndpoint($endpoint)
->setApiKey($key);
}

/**
Expand All @@ -34,4 +33,16 @@ public function jsonSerialize(): array
'api_key' => $this->apiKey,
];
}

private function setApiEndpoint(string $apiEndpoint): self
{
$this->apiEndpoint = rtrim($apiEndpoint, '/');
return $this;
}

private function setApiKey(string $apiKey): self
{
$this->apiKey = $apiKey;
return $this;
}
}
20 changes: 20 additions & 0 deletions src/Config/LightningConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use JsonSerializable;
use PhpLightning\Config\Backend\BackendConfigInterface;
use PhpLightning\Config\Backend\LnBitsBackendConfig;
use PhpLightning\Shared\Value\SendableRange;

final class LightningConfig implements JsonSerializable
Expand Down Expand Up @@ -60,6 +61,25 @@ public function addBackend(string $username, BackendConfigInterface $backendConf
return $this;
}

public function addBackendsAsJson(string $path): self
{
$jsonAsString = (string)file_get_contents($path);
/** @var array<string, array{api_endpoint?:string, api_key?: string}> $json */
$json = json_decode($jsonAsString, true);

foreach ($json as $user => $settings) {
$this->addBackend(
$user,
LnBitsBackendConfig::withEndpointAndKey(
$settings['api_endpoint'] ?? '',
$settings['api_key'] ?? '',
),
);
}

return $this;
}

public function jsonSerialize(): array
{
$result = [];
Expand Down
15 changes: 5 additions & 10 deletions tests/Feature/InvoiceFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal;
use Gacela\Framework\Container\Container;
use Gacela\Framework\Gacela;
use PhpLightning\Config\Backend\LnBitsBackendConfig;
use PhpLightning\Config\LightningConfig;
use PhpLightning\Invoice\InvoiceDependencyProvider;
use PhpLightning\Invoice\InvoiceFacade;
Expand All @@ -30,13 +29,13 @@ public function test_get_get_callback_url(): void
$this->bootstrapGacela();
$this->mockLnPaymentRequest();

$json = $this->facade->getCallbackUrl('username');
$json = $this->facade->getCallbackUrl('bob');

self::assertEquals([
'callback' => 'https://callback.url/receiver',
'maxSendable' => 10_000,
'minSendable' => 1_000,
'metadata' => '[["text/plain","Pay to username@domain.com"],["text/identifier","username@domain.com"]]',
'metadata' => '[["text/plain","Pay to bob@domain.com"],["text/identifier","bob@domain.com"]]',
'tag' => 'payRequest',
'commentAllowed' => false,
], $json);
Expand All @@ -47,7 +46,7 @@ public function test_ln_bits_feature(): void
$this->bootstrapGacela();
$this->mockLnPaymentRequest();

$json = $this->facade->generateInvoice('username', 2_000, 'lnbits');
$json = $this->facade->generateInvoice('alice', 2_000, 'lnbits');

self::assertEquals([
'pr' => 'lnbc10u1pjzh489...fake payment_request',
Expand All @@ -72,12 +71,8 @@ private function bootstrapGacela(): void
->setDomain('domain.com')
->setReceiver('receiver')
->setSendableRange(1_000, 10_000)
->addBackend(
'username',
(new LnBitsBackendConfig())
->setApiEndpoint('http://localhost:5000')
->setApiKey('XYZ'),
)->jsonSerialize(),
->addBackendsAsJson(__DIR__ . DIRECTORY_SEPARATOR . 'nostr.json')
->jsonSerialize(),
);
});
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/nostr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"bob": {
"api_key": "abc...123",
"api_endpoint": "http://localhost:5000"
},
"alice": {
"api_key": "def...456",
"api_endpoint": "http://localhost:5000"
}
}
4 changes: 1 addition & 3 deletions tests/Unit/Config/LightningConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ public function test_ln_bits_backend(): void
$config = (new LightningConfig())
->addBackend(
'username',
(new LnBitsBackendConfig())
->setApiEndpoint('http://localhost:5000/')
->setApiKey('XYZ'),
LnBitsBackendConfig::withEndpointAndKey('http://localhost:5000/', 'XYZ'),
);

self::assertEquals([
Expand Down

0 comments on commit 30ce880

Please sign in to comment.