BaseAsyncWebApplication   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 9.62 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 9
dl 10
loc 104
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0

6 Methods

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

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\AsyncRouter;
10
use Thruster\Component\Promise\Deferred;
11
use Thruster\Component\Promise\ExtendedPromiseInterface;
12
use Thruster\Component\Promise\FulfilledPromise;
13
14
/**
15
 * Class BaseAsyncWebApplication
16
 *
17
 * @package Thruster\Component\WebApplication
18
 * @author  Aurimas Niekis <[email protected]>
19
 */
20
abstract class BaseAsyncWebApplication implements AsyncWebApplicationInterface
21
{
22
    use RequestMiddlewaresAwareTrait;
23
24
    /**
25
     * @var AsyncRouter
26
     */
27
    protected $router;
28
29
    /**
30
     * @return AsyncRouter
31
     */
32 View Code Duplication
    public function getRouter() : AsyncRouter
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...
33
    {
34
        if ($this->router) {
35
            return $this->router;
36
        }
37
38
        $this->router = new AsyncRouter($this, $this);
39
40
        return $this->router;
41
    }
42
43
    /**
44
     * @param AsyncRouter $router
45
     *
46
     * @return $this
47
     */
48
    public function setRouter(AsyncRouter $router)
49
    {
50
        $this->router = $router;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function processRequest(ServerRequestInterface $request) : ExtendedPromiseInterface
59
    {
60
        $response = new Response();
61
62
        $deferred = new Deferred();
63
64
        $response = $this->getRequestMiddlewares()->getPreMiddlewares()->__invoke(
65
            $request,
66
            $response,
67
            function (ServerRequestInterface $request, ResponseInterface $response) use ($deferred) {
68
                $promise = $this->getRouter()->__invoke(
69
                    $request,
70
                    $response,
71
                    $this->getRequestMiddlewares()->getPostMiddlewares()
72
                );
73
74
                $promise->then([$deferred, 'resolve'], [$deferred, 'reject'], [$deferred, 'notify']);
75
76
                return $response;
77
            }
78
        );
79
80
        if (null !== $response) {
81
            $deferred->resolve($response);
82
        }
83
84
        return $deferred->promise();
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function handleRoute(
91
        ServerRequestInterface $request,
92
        ResponseInterface $response,
93
        callable $callback
94
    ) : ExtendedPromiseInterface {
95
96
        $defered = new Deferred();
97
98
        $callback($defered, $request, $response);
99
100
        return $defered->promise();
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106
    public function handleRouteMethodNotAllowed(
107
        ServerRequestInterface $request,
108
        ResponseInterface $response,
109
        array $allowedMethods
110
    ) : ExtendedPromiseInterface {
111
        return new FulfilledPromise($response->withStatus(405)->withHeader('Allow', implode(', ', $allowedMethods)));
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function handleRouteNotFound(
118
        ServerRequestInterface $request,
119
        ResponseInterface $response
120
    ) : ExtendedPromiseInterface {
121
        return new FulfilledPromise($response->withStatus(404));
122
    }
123
}
124