Passed
Pull Request — main (#3)
by Chema
02:25
created

InvoiceFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 30.76%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 44
ccs 8
cts 26
cp 0.3076
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createInvoiceGenerator() 0 6 1
A createLnBitsBackendInvoice() 0 5 1
A getHttpFacade() 0 3 1
A createEmptyBackendInvoice() 0 3 1
A createBackend() 0 5 1
A createCallbackUrl() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice;
6
7
use Gacela\Framework\AbstractFactory;
8
use PhpLightning\Http\HttpFacadeInterface;
9
use PhpLightning\Invoice\Domain\BackendInvoice\BackendInvoiceInterface;
10
use PhpLightning\Invoice\Domain\BackendInvoice\EmptyBackendInvoice;
11
use PhpLightning\Invoice\Domain\BackendInvoice\LnbitsBackendInvoice;
12
use PhpLightning\Invoice\Domain\CallbackUrl\CallbackUrl;
13
use PhpLightning\Invoice\Domain\CallbackUrl\CallbackUrlInterface;
14
use PhpLightning\Invoice\Domain\LnAddress\InvoiceGenerator;
15
16
/**
17
 * @method InvoiceConfig getConfig()
18
 */
19
final class InvoiceFactory extends AbstractFactory
20
{
21 1
    public function createCallbackUrl(): CallbackUrlInterface
22
    {
23 1
        return new CallbackUrl(
24 1
            $this->getHttpFacade(),
25 1
            $this->getConfig()->getLnAddress(),
26 1
            $this->getConfig()->getCallback(),
27 1
        );
28
    }
29
30
    public function createInvoiceGenerator(string $backend): InvoiceGenerator
31
    {
32
        return new InvoiceGenerator(
33
            $this->createBackend($backend),
34
            $this->getHttpFacade(),
35
            $this->getConfig()->getLnAddress(),
36
        );
37
    }
38
39
    private function createBackend(string $backend): BackendInvoiceInterface
40
    {
41
        return match ($backend) {
42
            'lnbits' => $this->createLnBitsBackendInvoice($backend),
43
            default => $this->createEmptyBackendInvoice($backend),
44
        };
45
    }
46
47
    private function createLnBitsBackendInvoice(string $backend): LnbitsBackendInvoice
48
    {
49
        return new LnbitsBackendInvoice(
50
            $this->getHttpFacade(),
51
            $this->getConfig()->getBackendOptionsFor($backend),
52
        );
53
    }
54
55
    private function createEmptyBackendInvoice(string $backend): EmptyBackendInvoice
56
    {
57
        return new EmptyBackendInvoice($backend);
58
    }
59
60 1
    private function getHttpFacade(): HttpFacadeInterface
61
    {
62 1
        return $this->getProvidedDependency(InvoiceDependencyProvider::FACADE_HTTP);
63
    }
64
}
65