Passed
Push — main ( 756af1...e120fa )
by Chema
03:55 queued 01:19
created

InvoiceFactory::getBackendForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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