BaseWebApplication   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 12.99 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 10
loc 77
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4
ccs 0
cts 38
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setRouter() 0 6 1
A processRequest() 0 6 1
A handleRoute() 0 7 1
A handleRouteMethodNotAllowed() 0 7 1
A handleRouteNotFound() 0 6 1
A getRouter() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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