Passed
Pull Request — main (#18)
by Chema
02:34
created

InvoiceFactory::getHttpApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
cc 1
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice;
6
7
use Gacela\Framework\AbstractFactory;
8
use PhpLightning\Invoice\Domain\BackendInvoice\BackendInvoiceInterface;
9
use PhpLightning\Invoice\Domain\BackendInvoice\LnbitsBackendInvoice;
10
use PhpLightning\Invoice\Domain\CallbackUrl\CallbackUrl;
11
use PhpLightning\Invoice\Domain\CallbackUrl\CallbackUrlInterface;
12
use PhpLightning\Invoice\Domain\CallbackUrl\LnAddressGenerator;
13
use PhpLightning\Invoice\Domain\CallbackUrl\LnAddressGeneratorInterface;
14
use PhpLightning\Invoice\Domain\Http\HttpApiInterface;
15
use PhpLightning\Invoice\Domain\LnAddress\InvoiceGenerator;
16
17
/**
18
 * @method InvoiceConfig getConfig()
19
 */
20
final class InvoiceFactory extends AbstractFactory
21
{
22
    public function createCallbackUrl(): CallbackUrlInterface
23
    {
24
        return new CallbackUrl(
25
            $this->getConfig()->getSendableRange(),
26
            $this->createLnAddressGenerator(),
27
            $this->getConfig()->getCallback(),
28
        );
29
    }
30
31
    public function createInvoiceGenerator(string $username): InvoiceGenerator
32
    {
33
        return new InvoiceGenerator(
34
            $this->getBackendForUser($username),
35
            $this->getConfig()->getSendableRange(),
36
            $this->getConfig()->getDefaultLnAddress(),
37
        );
38
    }
39
40
    private function createLnAddressGenerator(): LnAddressGeneratorInterface
41
    {
42
        return new LnAddressGenerator(
43
            $this->getConfig()->getDefaultLnAddress(),
44
            $this->getConfig()->getDomain(),
45
        );
46
    }
47
48
    private function getBackendForUser(string $username): BackendInvoiceInterface
49
    {
50
        return new LnbitsBackendInvoice(
51
            $this->getHttpApi(),
52
            $this->getConfig()->getBackendOptionsFor($username),
53
        );
54
    }
55
56
    private function getHttpApi(): HttpApiInterface
57
    {
58
        return $this->getProvidedDependency(InvoiceDependencyProvider::HTTP_API);
59
    }
60
}
61