Passed
Pull Request — main (#18)
by Chema
03:40 queued 01:03
created

InvoiceController::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 10
nc 4
nop 1
dl 0
loc 19
c 0
b 0
f 0
cc 3
ccs 0
cts 12
cp 0
crap 12
rs 9.9332
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
            dd($e); # temporal for debugging purposes...
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Symfony\Component\HttpFoundation\Response. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
43
        }
44
    }
45
}
46