ListedRfcController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\RfcLinc\Application\Web\Controllers;
6
7
use PhpCfdi\RfcLinc\DataGateway\FactoryInterface;
8
use PhpCfdi\RfcLinc\DataGateway\NotFoundException;
9
use PhpCfdi\RfcLinc\Domain\RfcLog;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
12
class ListedRfcController
13
{
14
    /** @var FactoryInterface */
15
    private $gateways;
16
17 2
    public function __construct(FactoryInterface $gateways)
18
    {
19 2
        $this->gateways = $gateways;
20 2
    }
21
22 2
    public function gateways(): FactoryInterface
23
    {
24 2
        return $this->gateways;
25
    }
26
27 2
    public function get(string $id): JsonResponse
28
    {
29
        try {
30 2
            $gateways = $this->gateways();
31 2
            $listedRfc = $gateways->listedRfc()->get($id);
32 1
            $rfclogs = $gateways->rfclog()->byRfc($id);
33
34 1
            return new JsonResponse([
35 1
                'rfc' => $listedRfc->rfc(),
36 1
                'since' => $listedRfc->since()->format(),
37 1
                'sncf' => $listedRfc->sncf(),
38 1
                'sub' => $listedRfc->sub(),
39 1
                'active' => ! $listedRfc->deleted(),
40 1
                'logs' => array_map(function (RfcLog $rfcLog) {
41
                    return [
42 1
                        'date' => $rfcLog->date()->format(),
43 1
                        'action' => $rfcLog->action(),
44
                    ];
45 1
                }, $rfclogs),
46 1
            ], JsonResponse::HTTP_OK);
47 1
        } catch (NotFoundException $exception) {
48 1
            return new JsonResponse(['error' => $exception->getMessage()], JsonResponse::HTTP_NOT_FOUND);
49
        } catch (\Throwable $exception) {
50
            return new JsonResponse(['error' => $exception->getMessage()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
51
        }
52
    }
53
}
54