Passed
Push — main ( 565e18...f372ec )
by Chema
55s queued 13s
created

InvoiceController::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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