Passed
Branch master (7c3f6c)
by Javi
02:38
created

Application::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace itsjavi\Flatdown;
4
5
use League\Container\Container;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
class Application extends Container
10
{
11
    /**
12
     * @var array
13
     */
14
    private $middlewares;
15
16
    public function __construct(array $config = [])
17
    {
18
        parent::__construct();
19
        $this->share('config', new Config($config));
20
    }
21
22
    public function isCli()
23
    {
24
        return php_sapi_name() === 'cli';
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    public function getMiddlewares()
31
    {
32
        return $this->middlewares;
33
    }
34
35
    /**
36
     * @param array $middlewares
37
     *
38
     * @return Application
39
     */
40
    public function setMiddlewares(array $middlewares)
41
    {
42
        $this->middlewares = $middlewares;
43
44
        return $this;
45
    }
46
47
    /**
48
     * Dispatches the request and sends the response
49
     *
50
     * @param ServerRequestInterface|null $request
51
     *
52
     * @return $this
53
     */
54
    public function execute(ServerRequestInterface $request = null)
55
    {
56
        $response = $this->dispatch($request);
57
        $this->share('response.final', $response);
58
        $this->get('response.emitter', [$response]);
59
60
        return $this;
61
    }
62
63
    /**
64
     * Dispatches the request and returns the response
65
     *
66
     * @param ServerRequestInterface|null $request
67
     *
68
     * @return ResponseInterface
69
     */
70
    public function dispatch(ServerRequestInterface $request = null)
71
    {
72
        $request = $request ?: $this->get('request');
73
        $this->share('request', $request);
74
75
        return $this->get('middleware.dispatcher', [$request]);
76
    }
77
78
    /**
79
     * @param string|null $key
80
     * @param mixed|null $default
81
     *
82
     * @return Config|mixed
83
     */
84 View Code Duplication
    public function config($key = null, $default = null)
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...
85
    {
86
        if (func_num_args() == 0) {
87
            return $this->get('config');
88
        }
89
90
        return $this->get('config')->get($key, $default);
91
    }
92
}
93