Passed
Push — main ( 2eea92...febc1c )
by Chema
11:04
created

InvoiceController::json()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
            return new JsonResponse([
44
                'status' => 'ERROR',
45
                'message' => $e->getMessage(),
46
            ]);
47
        }
48
    }
49
}
50