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

InvoiceController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 29
ccs 0
cts 15
cp 0
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 22 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice\Infrastructure\Controller;
6
7
use Gacela\Framework\DocBlockResolverAwareTrait;
8
use PhpLightning\Invoice\InvoiceFacade;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Throwable;
13
14
/**
15
 * @method InvoiceFacade getFacade()
16
 */
17
final class InvoiceController
18
{
19
    use DocBlockResolverAwareTrait;
20
21
    /**
22
     * @psalm-suppress InternalMethod
23
     */
24
    public function __invoke(Request $request): Response
25
    {
26
        $username = (string)$request->get('username');
27
        $milliSats = (int)$request->get('amount', 0);
28
        //        TODO: Make it customizable
29
        //        $backend = (string)$request->get('backend', 'lnbits');
30
31
        try {
32
            if ($milliSats === 0) {
33
                return new JsonResponse(
34
                    $this->getFacade()->getCallbackUrl($username),
35
                );
36
            }
37
38
            return new JsonResponse(
39
                $this->getFacade()->generateInvoice($username, $milliSats),
40
            );
41
        } catch (Throwable $e) {
42
            return new JsonResponse([
43
                'status' => 'ERROR',
44
                'message' => $e->getMessage(),
45
            ], Response::HTTP_BAD_REQUEST);
46
        }
47
    }
48
}
49