Lines 85.71% 12 / 14
Functions and Methods 0.00% 0 / 1
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AddDistrictCommandFactory 85.71% 12 / 14 0.00% 0 / 1 6.10 0.00% 0 / 1
 fromRequest 85.71% 12 / 14 0.00% 0 / 1 6.10
1<?php
2
3declare(strict_types=1);
4
5namespace Districts\Editor\UI\Factory;
6
7use Districts\Editor\Application\Command\AddDistrictCommand;
8use Psr\Http\Message\ServerRequestInterface as Request;
9use Slim\Exception\HttpBadRequestException;
10
11final class AddDistrictCommandFactory
12{
13    public function fromRequest(Request $request): AddDistrictCommand
14    {
15        $parsedBody = $request->getParsedBody();
16        if (!is_array($parsedBody)) {
17            throw new HttpBadRequestException($request);
18        }
19
20        if (
21            array_key_exists("city", $parsedBody)
22            && array_key_exists("name", $parsedBody)
23            && array_key_exists("area", $parsedBody)
24            && array_key_exists("population", $parsedBody)
25        ) {
26            return new AddDistrictCommand(
27                intval($parsedBody["city"]),
28                trim($parsedBody["name"]),
29                floatval($parsedBody["area"]),
30                intval($parsedBody["population"]),
31            );
32        }
33
34        throw new HttpBadRequestException($request);
35    }
36}