Completed
Push — master ( ad3024...d672d1 )
by Javi
59:10 queued 57:18
created

Application   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 109
ccs 28
cts 32
cp 0.875
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getMiddlewares() 0 4 1
A setMiddlewares() 0 12 3
A execute() 0 12 2
A dispatch() 0 16 3
A getRouter() 0 4 1
A getRequest() 0 4 1
A getUriResolver() 0 4 1
A getResponse() 0 4 1
1
<?php
2
3
namespace Philae;
4
5
use FastRoute\RouteCollector;
6
use Interop\Http\ServerMiddleware\MiddlewareInterface;
7
use League\Container\Container;
8
use League\Container\ContainerAwareInterface;
9
use Philae\Exceptions\InvalidCallableServiceException;
10
use Philae\Utils\UriResolverInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
14
class Application extends Container
15
{
16
    /**
17
     * @var MiddlewareInterface[]
18
     */
19
    protected $middlewares;
20
21
    /**
22
     * @return MiddlewareInterface[]
23
     */
24 21
    public function getMiddlewares(): array
25
    {
26 21
        return $this->middlewares;
27
    }
28
29
    /**
30
     * @param MiddlewareInterface[] $middlewares
31
     *
32
     * @return self|$this
33
     */
34 21
    public function setMiddlewares(array $middlewares): Application
35
    {
36 21
        foreach ($middlewares as $i => $middleware) {
37 21
            if ($middleware instanceof ContainerAwareInterface) {
38 21
                $middleware->setContainer($this);
39
            }
40
        }
41
42 21
        $this->middlewares = $middlewares;
43
44 21
        return $this;
45
    }
46
47
    /**
48
     * Dispatches the request and sends the response
49
     *
50
     * @param ServerRequestInterface|null $request
51
     * @return self|$this
52
     * @throws InvalidCallableServiceException
53
     */
54 3
    public function execute(ServerRequestInterface $request = null): Application
55
    {
56 3
        $response = $this->dispatch($request);
57 3
        $emitter  = $this->get(DefaultServiceProvider::SERVICE_RESPONSE_EMITTER);
58
59 3
        if (!is_callable($emitter)) {
60 3
            throw new InvalidCallableServiceException(DefaultServiceProvider::SERVICE_RESPONSE_EMITTER);
61
        }
62
        call_user_func($emitter, $response);
63
64
        return $this;
65
    }
66
67
    /**
68
     * Dispatches the request and returns the response
69
     *
70
     * @param ServerRequestInterface|null $request
71
     * @return ResponseInterface
72
     * @throws InvalidCallableServiceException
73
     */
74 21
    public function dispatch(ServerRequestInterface $request = null): ResponseInterface
75
    {
76 21
        $request = $request ?: $this->getRequest();
77 21
        $this->share(DefaultServiceProvider::SERVICE_REQUEST, $request);
78
79 21
        $dispatcher = $this->get(
80 21
            DefaultServiceProvider::SERVICE_MIDDLEWARE_DISPATCHER,
81 21
            [$this->getMiddlewares()]
82
        );
83
84 21
        if (!is_callable($dispatcher)) {
85 3
            throw new InvalidCallableServiceException(DefaultServiceProvider::SERVICE_MIDDLEWARE_DISPATCHER);
86
        }
87
88 18
        return call_user_func($dispatcher, $request);
89
    }
90
91
    /**
92
     * @return RouteCollector
93
     */
94 18
    public function getRouter(): RouteCollector
95
    {
96 18
        return $this->get(DefaultServiceProvider::SERVICE_ROUTE_COLLECTOR);
97
    }
98
99
    /**
100
     * @return ServerRequestInterface
101
     */
102
    public function getRequest(): ServerRequestInterface
103
    {
104
        return $this->get(DefaultServiceProvider::SERVICE_REQUEST);
105
    }
106
107
    /**
108
     * @return UriResolverInterface
109
     */
110 3
    public function getUriResolver(): UriResolverInterface
111
    {
112 3
        return $this->get(DefaultServiceProvider::SERVICE_URI_RESOLVER);
113
    }
114
115
    /**
116
     * @return ResponseInterface
117
     */
118 3
    public function getResponse(): ResponseInterface
119
    {
120 3
        return $this->get(DefaultServiceProvider::SERVICE_RESPONSE);
121
    }
122
}
123