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

InvoiceController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 38
ccs 0
cts 19
cp 0
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 15 3
A error() 0 5 1
A json() 0 5 1
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