Passed
Push — main ( 328362...5892c7 )
by Chema
02:33
created

InvoiceFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 48
ccs 28
cts 28
cp 1
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHttpApi() 0 3 1
A createInvoiceGenerator() 0 6 1
A createLnAddressGenerator() 0 5 1
A getBackendForUser() 0 5 1
A validateUserExists() 0 3 1
A createCallbackUrl() 0 10 2
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 1
    public function createCallbackUrl(string $username): CallbackUrlInterface
23
    {
24 1
        if ($username !== '') {
25 1
            $this->validateUserExists($username);
26
        }
27
28 1
        return new CallbackUrl(
29 1
            $this->getConfig()->getSendableRange(),
30 1
            $this->createLnAddressGenerator(),
31 1
            $this->getConfig()->getCallback(),
32 1
        );
33
    }
34
35 1
    public function createInvoiceGenerator(string $username): InvoiceGenerator
36
    {
37 1
        return new InvoiceGenerator(
38 1
            $this->getBackendForUser($username),
39 1
            $this->getConfig()->getSendableRange(),
40 1
            $this->getConfig()->getDefaultLnAddress(),
41 1
        );
42
    }
43
44 1
    private function createLnAddressGenerator(): LnAddressGeneratorInterface
45
    {
46 1
        return new LnAddressGenerator(
47 1
            $this->getConfig()->getDefaultLnAddress(),
48 1
            $this->getConfig()->getDomain(),
49 1
        );
50
    }
51
52 1
    private function getBackendForUser(string $username): BackendInvoiceInterface
53
    {
54 1
        return new LnbitsBackendInvoice(
55 1
            $this->getHttpApi(),
56 1
            $this->getConfig()->getBackendOptionsFor($username),
57 1
        );
58
    }
59
60 1
    private function getHttpApi(): HttpApiInterface
61
    {
62 1
        return $this->getProvidedDependency(InvoiceDependencyProvider::HTTP_API);
63
    }
64
65 1
    private function validateUserExists(string $username): void
66
    {
67 1
        $this->getConfig()->getBackendOptionsFor($username);
68
    }
69
}
70