Completed
Push — master ( a7b262...bc155c )
by
unknown
04:15
created

BaseAsyncWebApplication   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 10
loc 100
c 0
b 0
f 0
wmc 7
lcom 1
cbo 9
ccs 0
cts 54
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouter() 10 10 2
A setRouter() 0 6 1
B processRequest() 0 24 1
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
        $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
        return $deferred->promise();
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86
    public function handleRoute(
87
        ServerRequestInterface $request,
88
        ResponseInterface $response,
89
        callable $callback
90
    ) : ExtendedPromiseInterface {
91
92
        $defered = new Deferred();
93
94
        $callback($defered, $request, $response);
95
96
        return $defered->promise();
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function handleRouteMethodNotAllowed(
103
        ServerRequestInterface $request,
104
        ResponseInterface $response,
105
        array $allowedMethods
106
    ) : ExtendedPromiseInterface {
107
        return new FulfilledPromise($response->withStatus(405)->withHeader('Allow', implode(', ', $allowedMethods)));
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function handleRouteNotFound(
114
        ServerRequestInterface $request,
115
        ResponseInterface $response
116
    ) : ExtendedPromiseInterface {
117
        return new FulfilledPromise($response->withStatus(404));
118
    }
119
}
120