Completed
Push — master ( 75df13...a7b262 )
by
unknown
04:41
created

BaseWebApplication::getRouter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Thruster\Component\WebApplication;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Thruster\Component\HttpMessage\Response;
8
use Thruster\Component\HttpMiddleware\RequestMiddlewaresAwareTrait;
9
use Thruster\Component\HttpRouter\Router;
10
11
/**
12
 * Class BaseWebApplication
13
 *
14
 * @package Thruster\Component\WebApplication
15
 * @author  Aurimas Niekis <[email protected]>
16
 */
17
abstract class BaseWebApplication implements WebApplicationInterface
18
{
19
    use RequestMiddlewaresAwareTrait;
20
21
    /**
22
     * @var Router
23
     */
24
    protected $router;
25
26
    /**
27
     * @return Router
28
     */
29
    public function getRouter() : Router
30
    {
31
        if ($this->router) {
32
            return $this->router;
33
        }
34
35
        $this->router = new Router($this, $this);
36
37
        return $this->router;
38
    }
39
40
    /**
41
     * @param Router $router
42
     *
43
     * @return $this
44
     */
45
    public function setRouter(Router $router)
46
    {
47
        $this->router = $router;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function processRequest(ServerRequestInterface $request) : ResponseInterface
56
    {
57
        $response = new Response();
58
59
        return $this->executeMiddlewares($request, $response, $this->getRouter());
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function handleRoute(
66
        ServerRequestInterface $request,
67
        ResponseInterface $response,
68
        callable $callback
69
    ) : ResponseInterface {
70
        return $callback($request, $response);
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function handleRouteMethodNotAllowed(
77
        ServerRequestInterface $request,
78
        ResponseInterface $response,
79
        array $allowedMethods
80
    ) : ResponseInterface {
81
        return $response->withStatus(405)->withHeader('Allow', implode(', ', $allowedMethods));
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function handleRouteNotFound(
88
        ServerRequestInterface $request,
89
        ResponseInterface $response
90
    ) : ResponseInterface {
91
        return $response->withStatus(404);
92
    }
93
}
94