Issues (11)

WebPresentation/FrontController/FrontServlet.php (1 issue)

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace WebPresentation\FrontController;
6
7
use Throwable;
8
use ReflectionClass;
9
use ReflectionException;
10
use Exception\ApplicationException;
11
use Psr\Http\Message\ResponseInterface as Response;
12
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
13
14
class FrontServlet
15
{
16
    private ?Response $response = null;
17
18 1
    public function doGet(ServerRequest $request, Response $response): void
19
    {
20 1
        $command = $this->getCommand($request);
21 1
        $command->init($request, $response);
22 1
        $command->process();
23
24 1
        $this->response = $command->getResponse();
25 1
    }
26
27 1
    private function getCommand(ServerRequest $request): FrontCommand
28
    {
29
        try {
30 1
            return $this->getCommandClass($request)->newInstance();
31
        } catch (Throwable $e) {
32
            throw new ApplicationException($e->getMessage(), (int) $e->getCode(), $e);
33
        }
34
    }
35
36 1
    private function getCommandClass(ServerRequest $request): ReflectionClass
37
    {
38 1
        $query = $request->getQueryParams();
39 1
        $className = sprintf("%s\\%sCommand", __NAMESPACE__, $query['command'] ?? null);
40
41
        try {
42 1
            $result = new ReflectionClass($className);
43
        } catch (ReflectionException $e) {
44
            $result = new ReflectionClass(UnknownCommand::class);
45
        }
46
47 1
        return $result;
48
    }
49
50 1
    public function getResponse(): Response
51
    {
52 1
        return $this->response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response could return the type null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
53
    }
54
}
55