Lines 100.00% 19 / 19
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ListController 100.00% 19 / 19 100.00% 2 / 2 3 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 __invoke 100.00% 18 / 18 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Districts\Editor\UI\Controller;
6
7use Districts\Core\Domain\District;
8use Districts\Editor\Application\DistrictService;
9use Districts\Editor\UI\Factory\ListDistrictsQueryFactory;
10use Districts\Editor\UI\Session;
11use Districts\Editor\UI\View\ListTemplater;
12use InvalidArgumentException;
13use Psr\Http\Message\ResponseInterface as Response;
14use Psr\Http\Message\ServerRequestInterface as Request;
15use Slim\Views\Twig as View;
16
17final readonly class ListController
18{
19    /**
20     * @param ListTemplater<District> $listTemplater
21     */
22    public function __construct(
23        private DistrictService $districtService,
24        private ListDistrictsQueryFactory $queryFactory,
25        private Session $session,
26        private ListTemplater $listTemplater,
27        private View $view,
28    ) {
29    }
30
31    /**
32     * @param array<string, string> $args
33     */
34    // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
35    public function __invoke(Request $request, Response $response, array $args): Response
36    {
37        try {
38            $query = $this->queryFactory->fromRequest($request);
39        } catch (InvalidArgumentException) {
40            $query = $this->queryFactory->fromDefaults();
41            $errorMessage = "Invalid query parameters";
42        }
43        $templateData = $this->listTemplater->prepareTemplateData(
44            $this->districtService->list($query),
45            $request,
46            [
47                "city",
48                "name",
49                "area",
50                "population",
51            ],
52            "List of districts",
53            $this->session->getAndDelete("success.message"),
54            $errorMessage ?? null,
55        );
56        return $this->view->render($response, "list.html", $templateData);
57    }
58}