Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ProtectController | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| json | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| add | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | use Psr\Http\Message\ResponseInterface as Response; |
| 6 | use Psr\Http\Message\ServerRequestInterface as Request; |
| 7 | use UppServices\ProtectService; |
| 8 | |
| 9 | /** |
| 10 | * Slim controller for protected data endpoints. |
| 11 | * Delegates to ProtectService. |
| 12 | */ |
| 13 | class ProtectController |
| 14 | { |
| 15 | private readonly ProtectService $protectService; |
| 16 | |
| 17 | public function __construct(ProtectService $protectService) |
| 18 | { |
| 19 | $this->protectService = $protectService; |
| 20 | } |
| 21 | |
| 22 | private function json(Response $response, array $data, int $status = 200): Response |
| 23 | { |
| 24 | $response->getBody()->write(json_encode($data)); |
| 25 | return $response->withHeader('Content-Type', 'application/json')->withStatus($status); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * GET /protect/get - read a protected field value. |
| 30 | * Query params: device, session, place, target, entry, field. |
| 31 | * Returns HTTP 200 with JSON body containing errorcode (and value on success). |
| 32 | */ |
| 33 | public function get(Request $request, Response $response): Response |
| 34 | { |
| 35 | $result = $this->protectService->get($request->getQueryParams()); |
| 36 | return $this->json($response, $result); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * POST /protect/add - write a protected field value. |
| 41 | * Query params: device, session, place. |
| 42 | * Body: { target, entry, field, value }. |
| 43 | * Returns HTTP 200 with JSON body containing errorcode. |
| 44 | */ |
| 45 | public function add(Request $request, Response $response): Response |
| 46 | { |
| 47 | $body = $request->getBody()->getContents(); |
| 48 | $data = $body !== '' ? (json_decode($body, true) ?? []) : []; |
| 49 | $result = $this->protectService->add($request->getQueryParams(), $data); |
| 50 | return $this->json($response, $result); |
| 51 | } |
| 52 | } |