Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ApiMenuController
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
5 / 5
18
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%
22 / 22
100.00% covered (success)
100.00%
1 / 1
3
 save
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
9
 delete
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 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\Menu\Entity\MenuRecord;
8use App\Domain\Menu\Repository\MenuRecordRepository;
9use App\Domain\Menu\Repository\MenuTreeRepository;
10use Psr\Http\Message\ResponseFactoryInterface;
11use Psr\Http\Message\ResponseInterface;
12use Psr\Http\Message\ServerRequestInterface;
13
14 class ApiMenuController
15{
16    public function __construct(
17        private MenuRecordRepository $menuRecordRepository,
18        private MenuTreeRepository $menuTreeRepository,
19        private ResponseFactoryInterface $responseFactory
20    ) {}
21
22    public function list(): ResponseInterface
23    {
24        $menus = $this->menuRecordRepository->findAll();
25        $data = [];
26        foreach ($menus as $m) {
27            $items = $this->menuTreeRepository->findByMenuId($m->getId());
28            $tree = [];
29            foreach ($items as $item) {
30                $tree[] = [
31                    'id' => $item->getId(),
32                    'parent_id' => $item->getParentId(),
33                    'label' => $item->getLabel(),
34                    'url_or_route' => $item->getUrlOrRoute(),
35                    'sort_order' => $item->getSortOrder(),
36                ];
37            }
38
39            $data[] = [
40                'id' => $m->getId(),
41                'name' => $m->getName(),
42                'status' => $m->getStatus(),
43                'created_at' => $m->getCreatedAt(),
44                'updated_at' => $m->getUpdatedAt(),
45                'tree' => $tree,
46            ];
47        }
48
49        return $this->jsonResponse(['success' => true, 'data' => $data]);
50    }
51
52    public function save(ServerRequestInterface $request): ResponseInterface
53    {
54        $parsedBody = $request->getParsedBody();
55        $parsed = is_array($parsedBody) ? $parsedBody : json_decode((string) $request->getBody(), true);
56        /** @var array<string, mixed> $body */
57        $body = is_array($parsed) ? $parsed : [];
58
59        $id = isset($body['id']) && is_string($body['id']) ? $body['id'] : bin2hex(random_bytes(18));
60        $name = isset($body['name']) && is_string($body['name']) ? $body['name'] : 'New Menu';
61        $status = isset($body['status']) && is_string($body['status']) ? $body['status'] : 'active';
62
63        $record = new MenuRecord($id, $name, $status, time(), time());
64        $this->menuRecordRepository->save($record);
65
66        return $this->jsonResponse(['success' => true, 'id' => $id]);
67    }
68
69    public function delete(ServerRequestInterface $request): ResponseInterface
70    {
71        /** @var array<string, mixed> $queryParams */
72        $queryParams = $request->getQueryParams();
73        $id = isset($queryParams['id']) && is_string($queryParams['id']) ? $queryParams['id'] : '';
74
75        if ($id !== '') {
76            $this->menuRecordRepository->delete($id);
77        }
78
79        return $this->jsonResponse(['success' => true]);
80    }
81
82    /**
83     * @param array<string, mixed> $data
84     */
85    private function jsonResponse(array $data): ResponseInterface
86    {
87        $response = $this->responseFactory->createResponse(200)
88            ->withHeader('Content-Type', 'application/json');
89        $response->getBody()->write((string) json_encode($data));
90        return $response;
91    }
92}