Passed
Push — main ( e120fa...565e18 )
by Chema
53s queued 13s
created

InvoiceController::json()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 1
dl 0
loc 5
c 0
b 0
f 0
cc 1
ccs 0
cts 3
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 PhpLightning\Invoice\InvoiceFacade;
9
use Throwable;
10
11
/**
12
 * @method InvoiceFacade getFacade()
13
 */
14
final class InvoiceController
15
{
16
    use DocBlockResolverAwareTrait;
17
18
    /**
19
     * @psalm-suppress InternalMethod
20
     */
21
    public function __invoke(string $username = '', int $amount = 0): string
22
    {
23
        try {
24
            if ($amount === 0) {
25
                return $this->json(
26
                    $this->getFacade()->getCallbackUrl($username),
27
                );
28
            }
29
30
            return $this->json(
31
                $this->getFacade()->generateInvoice($username, $amount),
32
            );
33
        } catch (Throwable $e) {
34
            return $this->json([
35
                'status' => 'ERROR',
36
                'message' => $e->getMessage(),
37
            ]);
38
        }
39
    }
40
41
    private function json(array $json): string
42
    {
43
        header('Content-Type: application/json');
44
45
        return json_encode($json, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
46
    }
47
}
48