Lines 75.00% 6 / 8
Functions and Methods 50.00% 1 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
RemoveActionController 75.00% 6 / 8 50.00% 1 / 2 3.14 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 __invoke 71.42% 5 / 7 0.00% 0 / 1 2.09
1<?php
2
3declare(strict_types=1);
4
5namespace Districts\Editor\UI\Controller;
6
7use Districts\Core\Infrastructure\NotFoundInRepositoryException;
8use Districts\Editor\Application\DistrictService;
9use Districts\Editor\UI\Factory\RemoveDistrictCommandFactory;
10use Districts\Editor\UI\Session;
11use Fig\Http\Message\StatusCodeInterface as StatusCode;
12use Psr\Http\Message\ResponseFactoryInterface as ResponseFactory;
13use Psr\Http\Message\ResponseInterface as Response;
14use Psr\Http\Message\ServerRequestInterface as Request;
15use Slim\Exception\HttpNotFoundException;
16use Slim\Interfaces\RouteParserInterface;
17
18final readonly class RemoveActionController
19{
20    public function __construct(
21        private DistrictService $districtService,
22        private RemoveDistrictCommandFactory $commandFactory,
23        private Session $session,
24        private RouteParserInterface $routeParser,
25        private ResponseFactory $responseFactory,
26    ) {
27    }
28
29    /**
30     * @param array<string, string> $args
31     */
32    // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
33    public function __invoke(Request $request, Response $response, array $args): Response
34    {
35        $command = $this->commandFactory->fromRoute($args, $request);
36        try {
37            $this->districtService->remove($command);
38            $this->session->set("success.message", "District data removed.");
39        } catch (NotFoundInRepositoryException) {
40            throw new HttpNotFoundException($request);
41        }
42        $url = $this->routeParser->relativeUrlFor("list");
43        return $this->responseFactory->createResponse(StatusCode::STATUS_FOUND)->withHeader("Location", $url);
44    }
45}