Passed
Push — develop ( 429d81...1647ec )
by Brent
02:48
created

Server::handleStaticRoute()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher\Application;
4
5
use GuzzleHttp\Psr7\Request;
6
use GuzzleHttp\Psr7\Response;
7
use GuzzleHttp\Psr7\ServerRequest;
8
use Stitcher\Exception\Http;
9
10
abstract class Server
11
{
12
    /** @var Router */
13
    protected $router;
14
15
    /** @var Request */
16
    protected $request;
17
18 3
    public function setRouter(Router $router): Server
19
    {
20 3
        $this->router = $router;
21
22 3
        return $this;
23
    }
24
25 4
    public function run(): string
26
    {
27 4
        $response = $this->handleStaticRoute();
28
29 4
        if (!$response) {
30
            $response = $this->handleDynamicRoute();
31
        }
32
33 4
        if (!$response) {
34
            throw Http::notFound($this->getCurrentPath());
35
        }
36
37 4
        return $response->getBody()->getContents();
38
    }
39
40 2
    protected function getRequest(): Request
41
    {
42 2
        if (!$this->request) {
43 2
            $this->request = ServerRequest::fromGlobals();
0 ignored issues
show
Documentation Bug introduced by
It seems like \GuzzleHttp\Psr7\ServerRequest::fromGlobals() of type object<Psr\Http\Message\ServerRequestInterface> is incompatible with the declared type object<GuzzleHttp\Psr7\Request> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
        }
45
46 2
        return $this->request;
47
    }
48
49 2
    protected function getCurrentPath(): string
50
    {
51 2
        $path = $this->getRequest()->getUri()->getPath();
52
53 2
        return $path === '' ? '/' : $path;
54
    }
55
56
    abstract protected function handleStaticRoute(): ?Response;
57
58
    protected function handleDynamicRoute(): ?Response
59
    {
60
        if (! $this->router) {
61
            return null;
62
        }
63
64
        return $this->router->dispatch($this->getRequest());
65
    }
66
}
67