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
|
|
|
|