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

InvoiceController::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 1
dl 0
loc 22
ccs 0
cts 15
cp 0
crap 12
rs 9.8333
c 0
b 0
f 0
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