Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ApiHostController
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
4 / 4
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 list
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 save
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
11
 jsonResponse
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace App\Web\Controller\Api;
6
7use App\Domain\Host\Entity\HostRecord;
8use App\Domain\Host\Repository\HostRecordRepository;
9use Psr\Http\Message\ResponseFactoryInterface;
10use Psr\Http\Message\ResponseInterface;
11use Psr\Http\Message\ServerRequestInterface;
12
13 class ApiHostController
14{
15    public function __construct(
16        private HostRecordRepository $hostRecordRepository,
17        private ResponseFactoryInterface $responseFactory
18    ) {}
19
20    public function list(): ResponseInterface
21    {
22        $hosts = $this->hostRecordRepository->findAll();
23        $data = [];
24        foreach ($hosts as $h) {
25            $data[] = [
26                'id' => $h->getId(),
27                'name' => $h->getName(),
28                'label' => $h->getLabel(),
29                'type' => $h->getType(),
30                'url' => $h->getUrl(),
31                'created_at' => $h->getCreatedAt(),
32                'updated_at' => $h->getUpdatedAt(),
33            ];
34        }
35
36        return $this->jsonResponse(['success' => true, 'data' => $data]);
37    }
38
39    public function save(ServerRequestInterface $request): ResponseInterface
40    {
41        /** @var array<string, mixed> $body */
42        $body = (array) ($request->getParsedBody() ?? json_decode((string) $request->getBody(), true));
43        $id = isset($body['id']) && is_string($body['id']) ? $body['id'] : bin2hex(random_bytes(18));
44        $name = isset($body['name']) && is_string($body['name']) ? $body['name'] : 'New Host';
45        $type = isset($body['type']) && is_string($body['type']) ? $body['type'] : 'client';
46        $url = isset($body['url']) && is_string($body['url']) ? $body['url'] : 'http://127.0.0.1:8080';
47        $label = isset($body['label']) && is_string($body['label']) ? $body['label'] : null;
48
49        $record = new HostRecord($id, $name, $type, $url, $label, time(), time());
50        $this->hostRecordRepository->save($record);
51
52        return $this->jsonResponse(['success' => true, 'id' => $id]);
53    }
54
55    /**
56     * @param array<string, mixed> $data
57     */
58    private function jsonResponse(array $data): ResponseInterface
59    {
60        $response = $this->responseFactory->createResponse(200)
61            ->withHeader('Content-Type', 'application/json');
62        $response->getBody()->write((string) json_encode($data, JSON_THROW_ON_ERROR));
63        return $response;
64    }
65}