Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
47 / 47 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ApiController | |
100.00% |
47 / 47 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
46 / 46 |
|
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\TablePrefixResolver; |
| 10 | use App\Shared\UI\ListView\ListViewData; |
| 11 | use Psr\Http\Message\ResponseInterface; |
| 12 | use Psr\Http\Message\ServerRequestInterface; |
| 13 | use Yiisoft\Yii\View\Renderer\WebViewRenderer; |
| 14 | |
| 15 | class ApiController |
| 16 | { |
| 17 | public function __construct( |
| 18 | private WebViewRenderer $viewRenderer, |
| 19 | private TablePrefixResolver $prefixResolver |
| 20 | ) { |
| 21 | $this->viewRenderer = $viewRenderer->withControllerName('api'); |
| 22 | } |
| 23 | |
| 24 | public function index( |
| 25 | ServerRequestInterface $request, |
| 26 | GridQueryBuilder $queryBuilder |
| 27 | ): ResponseInterface { |
| 28 | $allowedColumns = [ |
| 29 | 'host_name' => 'endpoint_url', |
| 30 | 'endpoint_url' => 'endpoint_url', |
| 31 | 'status' => 'status', |
| 32 | 'created_at' => 'created_at', |
| 33 | ]; |
| 34 | |
| 35 | /** @var array<string, mixed> $queryParams */ |
| 36 | $queryParams = $request->getQueryParams(); |
| 37 | $spec = new GridQuerySpecification($queryParams, $allowedColumns, 'created_at', 'DESC'); |
| 38 | |
| 39 | $targetTable = $this->prefixResolver->resolve('api_records'); |
| 40 | $queryResult = $queryBuilder->fetchData($targetTable, $spec, 'created_at', 'DESC'); |
| 41 | |
| 42 | $rawRows = $queryResult['items']; |
| 43 | $totalItems = $queryResult['totalCount']; |
| 44 | |
| 45 | $items = array_map(function ($row) { |
| 46 | /** @var array<string, mixed> $row */ |
| 47 | $createdAt = isset($row['created_at']) && is_numeric($row['created_at']) ? (int) $row['created_at'] : time(); |
| 48 | $statusStr = isset($row['status']) && $row['status'] === 'active' ? 'Aktywny' : 'Nieaktywny'; |
| 49 | |
| 50 | return [ |
| 51 | 'id' => is_string($row['id'] ?? null) ? (string) $row['id'] : '', |
| 52 | 'host_name' => is_string($row['host_id'] ?? null) ? (string) $row['host_id'] : 'Main', |
| 53 | 'endpoint_url' => is_string($row['endpoint_url'] ?? null) ? (string) $row['endpoint_url'] : '', |
| 54 | 'status' => $statusStr, |
| 55 | 'created_at' => date('Y-m-d H:i:s', $createdAt), |
| 56 | ]; |
| 57 | }, $rawRows); |
| 58 | |
| 59 | $listView = new ListViewData( |
| 60 | items: $items, |
| 61 | columns: [ |
| 62 | 'host_name' => 'Host', |
| 63 | 'endpoint_url' => 'Endpoint URL', |
| 64 | 'status' => 'Status', |
| 65 | 'created_at' => 'Utworzono', |
| 66 | ], |
| 67 | currentPage: $spec->getPage(), |
| 68 | pageSize: $spec->getPageSize(), |
| 69 | totalItems: $totalItems, |
| 70 | sortField: $spec->getSortField(), |
| 71 | sortDirection: $spec->getSortDirection(), |
| 72 | filterParams: $spec->getFilters(), |
| 73 | updateRoute: 'api/update', |
| 74 | deleteRoute: 'api/delete', |
| 75 | tableExists: (bool) ($queryResult['tableExists'] ?? true), |
| 76 | tableName: (string) ($queryResult['tableName'] ?? 'api_records') |
| 77 | ); |
| 78 | |
| 79 | return $this->viewRenderer->render('//api/index.twig', [ |
| 80 | 'listView' => $listView, |
| 81 | 'title' => 'Konfiguracja API', |
| 82 | ]); |
| 83 | } |
| 84 | } |