BaseWebApplication::handleRoute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
crap 2
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 View Code Duplication
    public function getRouter() : Router
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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