Lines
84.61%
11 / 13
Functions and Methods
50.00%
1 / 2
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| EditFormController | 84.61% | 11 / 13 | 50.00% | 1 / 2 | 4.06 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| __invoke | 83.33% | 10 / 12 | 0.00% | 0 / 1 | 3.04 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Districts\Editor\UI\Controller; | |
| 6 | ||
| 7 | use Districts\Core\Infrastructure\NotFoundInRepositoryException; | |
| 8 | use Districts\Editor\Application\DistrictService; | |
| 9 | use Districts\Editor\UI\Factory\GetDistrictQueryFactory; | |
| 10 | use Districts\Editor\UI\Session; | |
| 11 | use Psr\Http\Message\ResponseInterface as Response; | |
| 12 | use Psr\Http\Message\ServerRequestInterface as Request; | |
| 13 | use Slim\Exception\HttpNotFoundException; | |
| 14 | use Slim\Views\Twig as View; | |
| 15 | ||
| 16 | final readonly class EditFormController | |
| 17 | { | |
| 18 | public function __construct( | |
| 19 | private DistrictService $districtService, | |
| 20 | private GetDistrictQueryFactory $queryFactory, | |
| 21 | private Session $session, | |
| 22 | private View $view, | |
| 23 | ) { | |
| 24 | } | |
| 25 | ||
| 26 | /** | |
| 27 | * @param array<string, string> $args | |
| 28 | */ | |
| 29 | public function __invoke(Request $request, Response $response, array $args): Response | |
| 30 | { | |
| 31 | $district = $this->session->getAndDelete("form.edit.values"); | |
| 32 | if ($district === null) { | |
| 33 | try { | |
| 34 | $district = $this->districtService->get($this->queryFactory->fromRoute($args, $request)); | |
| 35 | } catch (NotFoundInRepositoryException) { | |
| 36 | throw new HttpNotFoundException($request); | |
| 37 | } | |
| 38 | } | |
| 39 | $templateData = [ | |
| 40 | "title" => "Edit a district", | |
| 41 | "district" => $district, | |
| 42 | "errors" => $this->session->getAndDelete("form.edit.errors"), | |
| 43 | "errorMessage" => $this->session->getAndDelete("form.edit.error.message"), | |
| 44 | ]; | |
| 45 | return $this->view->render($response, "edit.html", $templateData); | |
| 46 | } | |
| 47 | } |