Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
50 / 50 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| TranslationController | |
100.00% |
50 / 50 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
49 / 49 |
|
100.00% |
1 / 1 |
8 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Web\Controller; |
| 6 | |
| 7 | use App\Domain\Shared\Query\GridQuerySpecification; |
| 8 | use App\Infrastructure\Shared\Query\GridQueryBuilder; |
| 9 | use App\Shared\Host\ActiveHostProvider; |
| 10 | use App\Shared\Host\TablePrefixResolver; |
| 11 | use App\Shared\UI\ListView\ListViewData; |
| 12 | use Psr\Http\Message\ResponseInterface; |
| 13 | use Psr\Http\Message\ServerRequestInterface; |
| 14 | use Yiisoft\Yii\View\Renderer\WebViewRenderer; |
| 15 | |
| 16 | class TranslationController |
| 17 | { |
| 18 | public function __construct( |
| 19 | private WebViewRenderer $viewRenderer, |
| 20 | private ActiveHostProvider $activeHostProvider, |
| 21 | private TablePrefixResolver $prefixResolver |
| 22 | ) { |
| 23 | $this->viewRenderer = $viewRenderer->withControllerName('translation'); |
| 24 | } |
| 25 | |
| 26 | public function index( |
| 27 | ServerRequestInterface $request, |
| 28 | GridQueryBuilder $queryBuilder |
| 29 | ): ResponseInterface { |
| 30 | $allowedColumns = [ |
| 31 | 'language_code' => 'language_code', |
| 32 | 'category' => 'category', |
| 33 | 'message_key' => 'message_key', |
| 34 | 'message_content' => 'message_content', |
| 35 | 'created_at' => 'created_at', |
| 36 | ]; |
| 37 | |
| 38 | /** @var array<string, mixed> $queryParams */ |
| 39 | $queryParams = $request->getQueryParams(); |
| 40 | $spec = new GridQuerySpecification($queryParams, $allowedColumns, 'category', 'ASC'); |
| 41 | |
| 42 | $targetTable = $this->prefixResolver->resolve('translations_records'); |
| 43 | $queryResult = $queryBuilder->fetchData($targetTable, $spec, 'category', 'ASC'); |
| 44 | |
| 45 | $rawRows = $queryResult['items']; |
| 46 | $totalItems = $queryResult['totalCount']; |
| 47 | |
| 48 | $items = array_map(function ($row) { |
| 49 | /** @var array<string, mixed> $row */ |
| 50 | $createdAt = isset($row['created_at']) && is_numeric($row['created_at']) ? (int) $row['created_at'] : time(); |
| 51 | |
| 52 | return [ |
| 53 | 'id' => is_string($row['id'] ?? null) ? (string) $row['id'] : '', |
| 54 | 'language_code' => is_string($row['language_code'] ?? null) ? (string) $row['language_code'] : '', |
| 55 | 'category' => is_string($row['category'] ?? null) ? (string) $row['category'] : '', |
| 56 | 'message_key' => is_string($row['message_key'] ?? null) ? (string) $row['message_key'] : '', |
| 57 | 'message_content' => is_string($row['message_content'] ?? null) ? (string) $row['message_content'] : '', |
| 58 | 'created_at' => date('Y-m-d H:i:s', $createdAt), |
| 59 | ]; |
| 60 | }, $rawRows); |
| 61 | |
| 62 | $listView = new ListViewData( |
| 63 | items: $items, |
| 64 | columns: [ |
| 65 | 'language_code' => 'Język', |
| 66 | 'category' => 'Kategoria', |
| 67 | 'message_key' => 'Klucz', |
| 68 | 'message_content' => 'Tłumaczenie', |
| 69 | 'created_at' => 'Utworzono', |
| 70 | ], |
| 71 | currentPage: $spec->getPage(), |
| 72 | pageSize: $spec->getPageSize(), |
| 73 | totalItems: $totalItems, |
| 74 | sortField: $spec->getSortField(), |
| 75 | sortDirection: $spec->getSortDirection(), |
| 76 | filterParams: $spec->getFilters(), |
| 77 | updateRoute: 'translation/update', |
| 78 | deleteRoute: 'translation/delete', |
| 79 | tableExists: (bool) ($queryResult['tableExists'] ?? true), |
| 80 | tableName: (string) ($queryResult['tableName'] ?? 'translations_records') |
| 81 | ); |
| 82 | |
| 83 | return $this->viewRenderer->render('//translation/index.twig', [ |
| 84 | 'listView' => $listView, |
| 85 | 'title' => 'Zarządzanie Tłumaczeniami (API-First)', |
| 86 | 'activeHost' => $this->activeHostProvider->getActiveHost(), |
| 87 | ]); |
| 88 | } |
| 89 | } |