1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Interfaces\Http; |
6
|
|
|
|
7
|
|
|
use App\Domain\Model\ObjectEntry; |
8
|
|
|
use App\Domain\Service\ObjectStoreInterface; |
9
|
|
|
use DateTime; |
10
|
|
|
use Swagger\Annotations as SWG; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
16
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
17
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @Route("/object") |
21
|
|
|
* @SWG\Tag(name="Object Store") |
22
|
|
|
*/ |
23
|
|
|
class ApiController extends AbstractController |
24
|
|
|
{ |
25
|
|
|
protected ObjectStoreInterface $objectStorage; |
26
|
|
|
|
27
|
8 |
|
public function __construct(ObjectStoreInterface $objectStorage) |
28
|
|
|
{ |
29
|
8 |
|
$this->objectStorage = $objectStorage; |
30
|
8 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @SWG\Parameter( |
34
|
|
|
* name="body", |
35
|
|
|
* in="body", |
36
|
|
|
* description="Key-value pairs to store", |
37
|
|
|
* @SWG\Schema( |
38
|
|
|
* type="object", |
39
|
|
|
* additionalProperties=true, |
40
|
|
|
* example={"key": "value"} |
41
|
|
|
* ) |
42
|
|
|
* ) |
43
|
|
|
* @SWG\Response( |
44
|
|
|
* response="201", |
45
|
|
|
* description="On success", |
46
|
|
|
* headers={@SWG\Header(header="X-Timestamp", type="number", description="Current server timestamp")} |
47
|
|
|
* ) |
48
|
|
|
* @SWG\Response(response="400", description="When provided object is not valid") |
49
|
|
|
* |
50
|
|
|
* @Route("", name="api_upsert_object", methods={"POST"}, defaults={"_format": "json"}) |
51
|
|
|
*/ |
52
|
4 |
|
public function storeValue(Request $request) |
53
|
|
|
{ |
54
|
4 |
|
$objects = json_decode($request->getContent(), true); |
55
|
|
|
|
56
|
4 |
|
if (!is_iterable($objects)) { |
57
|
3 |
|
throw new BadRequestHttpException(); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
foreach ($objects as $key => $object) { |
61
|
1 |
|
$entry = new ObjectEntry($key, $object); |
62
|
1 |
|
$this->objectStorage->store($entry, new DateTime()); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return new JsonResponse( |
66
|
1 |
|
null, |
67
|
1 |
|
Response::HTTP_CREATED, |
68
|
1 |
|
['X-Timestamp' => time()] |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @SWG\Parameter(name="key", in="path", description="Key to display value for", type="string") |
74
|
|
|
* @SWG\Parameter(name="timestamp", in="query", description="Timestamp of the value snapshot", type="integer", required=false) |
75
|
|
|
* @SWG\Response( |
76
|
|
|
* response="200", |
77
|
|
|
* description="Value for the key at given time (or now)", |
78
|
|
|
* @SWG\Schema(type="object", example="test-value"), |
79
|
|
|
* headers={@SWG\Header(header="X-Timestamp", type="number", description="Current server timestamp")} |
80
|
|
|
* ) |
81
|
|
|
* @SWG\Response(response="400", description="When provided timestamp is invalid") |
82
|
|
|
* @SWG\Response(response="404", description="When key is not found (for provided time)") |
83
|
|
|
* @Route("/{key}", name="api_get_value", methods={"GET"}, defaults={"_format": "json"}) |
84
|
|
|
*/ |
85
|
4 |
|
public function getValue(string $key, Request $request) |
86
|
|
|
{ |
87
|
4 |
|
$date = DateTime::createFromFormat('U', (string) $request->get('timestamp', time())); |
88
|
|
|
|
89
|
4 |
|
if (!$date instanceof DateTime) { |
90
|
1 |
|
throw new BadRequestHttpException('Invalid time'); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
$value = $this->objectStorage->get($key, $date); |
94
|
|
|
|
95
|
3 |
|
if (!$value) { |
96
|
1 |
|
throw new NotFoundHttpException(); |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
return new JsonResponse( |
100
|
2 |
|
$value->getValue(), |
101
|
2 |
|
Response::HTTP_OK, |
102
|
2 |
|
['X-Timestamp' => time()] |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|