InvoiceController::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 2
b 0
f 0
nc 5
nop 1
dl 0
loc 19
ccs 0
cts 14
cp 0
crap 12
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Invoice\Infrastructure\Controller;
6
7
use Gacela\Framework\DocBlockResolverAwareTrait;
8
use Gacela\Router\Entities\JsonResponse;
9
use Gacela\Router\Entities\Request;
10
use PhpLightning\Invoice\InvoiceFacade;
11
use Throwable;
12
13
/**
14
 * @method InvoiceFacade getFacade()
15
 */
16
final class InvoiceController
17
{
18
    use DocBlockResolverAwareTrait;
19
20
    public function __construct(
21
        private Request $request,
22
    ) {
23
    }
24
25
    /**
26
     * @psalm-suppress InternalMethod
27
     */
28
    public function __invoke(string $username = ''): JsonResponse
29
    {
30
        try {
31
            $amount = (int)$this->request->get('amount');
32
33
            if ($amount === 0) {
34
                return new JsonResponse(
35
                    $this->getFacade()->getCallbackUrl($username),
36
                );
37
            }
38
39
            return new JsonResponse(
40
                $this->getFacade()->generateInvoice($username, $amount),
41
            );
42
        } catch (Throwable $e) {
43
            dump($e);
44
            return new JsonResponse([
45
                'status' => 'ERROR',
46
                'message' => $e->getMessage(),
47
            ]);
48
        }
49
    }
50
}
51