Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
35 / 35 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| ApiTranslationController | |
100.00% |
35 / 35 |
|
100.00% |
6 / 6 |
14 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| list | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| save | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| delete | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| jsonResponse | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Web\Controller\Api; |
| 6 | |
| 7 | use App\Domain\Translation\Entity\TranslationRecord; |
| 8 | use App\Domain\Translation\Repository\TranslationRecordRepository; |
| 9 | use Psr\Http\Message\ResponseFactoryInterface; |
| 10 | use Psr\Http\Message\ResponseInterface; |
| 11 | use Psr\Http\Message\ServerRequestInterface; |
| 12 | |
| 13 | class ApiTranslationController |
| 14 | { |
| 15 | public function __construct( |
| 16 | private TranslationRecordRepository $translationRecordRepository, |
| 17 | private ResponseFactoryInterface $responseFactory |
| 18 | ) {} |
| 19 | |
| 20 | public function list(): ResponseInterface |
| 21 | { |
| 22 | $translations = $this->translationRecordRepository->findAll(); |
| 23 | $data = []; |
| 24 | foreach ($translations as $t) { |
| 25 | $data[] = [ |
| 26 | 'id' => $t->getId(), |
| 27 | 'language_code' => $t->getLanguageCode(), |
| 28 | 'category' => $t->getCategory(), |
| 29 | 'message_key' => $t->getMessageKey(), |
| 30 | 'message_content' => $t->getMessageContent(), |
| 31 | 'created_at' => $t->getCreatedAt(), |
| 32 | 'updated_at' => $t->getUpdatedAt(), |
| 33 | ]; |
| 34 | } |
| 35 | |
| 36 | return $this->jsonResponse(['success' => true, 'data' => $data]); |
| 37 | } |
| 38 | |
| 39 | public function save(ServerRequestInterface $request): ResponseInterface |
| 40 | { |
| 41 | $parsedBody = $request->getParsedBody(); |
| 42 | $parsed = is_array($parsedBody) ? $parsedBody : json_decode((string) $request->getBody(), true); |
| 43 | /** @var array<string, mixed> $body */ |
| 44 | $body = is_array($parsed) ? $parsed : []; |
| 45 | |
| 46 | $id = $this->getString($body, 'id', bin2hex(random_bytes(18))); |
| 47 | $languageCode = $this->getString($body, 'language_code', 'pl'); |
| 48 | $category = $this->getString($body, 'category', 'app'); |
| 49 | $messageKey = $this->getString($body, 'message_key', ''); |
| 50 | $messageContent = $this->getString($body, 'message_content', ''); |
| 51 | |
| 52 | $record = new TranslationRecord($id, $languageCode, $category, $messageKey, $messageContent, time(), time()); |
| 53 | $this->translationRecordRepository->save($record); |
| 54 | |
| 55 | return $this->jsonResponse(['success' => true, 'id' => $id]); |
| 56 | } |
| 57 | |
| 58 | public function delete(ServerRequestInterface $request): ResponseInterface |
| 59 | { |
| 60 | /** @var array<string, mixed> $queryParams */ |
| 61 | $queryParams = $request->getQueryParams(); |
| 62 | $id = isset($queryParams['id']) && is_string($queryParams['id']) ? $queryParams['id'] : ''; |
| 63 | |
| 64 | if ($id !== '') { |
| 65 | $this->translationRecordRepository->delete($id); |
| 66 | } |
| 67 | |
| 68 | return $this->jsonResponse(['success' => true]); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param array<string, mixed> $data |
| 73 | */ |
| 74 | private function jsonResponse(array $data): ResponseInterface |
| 75 | { |
| 76 | $response = $this->responseFactory->createResponse(200) |
| 77 | ->withHeader('Content-Type', 'application/json'); |
| 78 | $response->getBody()->write((string) json_encode($data)); |
| 79 | return $response; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param array<string, mixed> $data |
| 84 | */ |
| 85 | private function getString(array $data, string $key, string $default): string |
| 86 | { |
| 87 | return isset($data[$key]) && is_string($data[$key]) ? $data[$key] : $default; |
| 88 | } |
| 89 | } |