Completed
Push — master ( aec546...27a395 )
by Filipe
04:35
created

Application   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 74.63%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 19
c 3
b 0
f 1
lcom 1
cbo 4
dl 0
loc 197
ccs 50
cts 67
cp 0.7463
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A container() 0 8 2
A setRunner() 0 5 1
A getResponse() 0 7 2
A getRunner() 0 10 2
A __construct() 0 10 1
A getContainer() 0 7 2
A setContainer() 0 4 1
A setConfigPath() 0 6 1
A getConfigPath() 0 8 2
A getRequest() 0 8 2
A checkContainer() 0 17 3
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc;
11
12
use Interop\Container\ContainerInterface;
13
use Slick\Configuration\Configuration;
14
use Slick\Di\ContainerBuilder;
15
use Slick\Http\PhpEnvironment\MiddlewareRunnerInterface;
16
use Slick\Http\PhpEnvironment\Request;
17
use Slick\Http\PhpEnvironment\Response;
18
19
/**
20
 * Class Application
21
 * @package Slick\Mvc
22
 */
23
class Application
24
{
25
26
    /**
27
     * @var ContainerInterface
28
     */
29
    private static $container;
30
31
    /**
32
     * @var string
33
     */
34
    private $configPath;
35
36
    /**
37
     * @var Request
38
     */
39
    private $request;
40
41
    /**
42
     * @var MiddlewareRunnerInterface
43
     */
44
    private $runner;
45
46
    /**
47
     * @var Response
48
     */
49
    private $response;
50
51
    /**
52
     * @var ContainerInterface
53
     */
54
    private static $defaultContainer;
55
56
    /**
57
     * Creates an application with an HTTP request
58
     *
59
     * If no request is provided then a Slick\Http\PhpEnvironment\Request is
60
     * created with values form current php environment settings.
61
     *
62
     * @param Request $request
63
     */
64 8
    public function __construct(Request $request = null)
65
    {
66 8
        $this->request = $request;
67 8
        Configuration::addPath(__DIR__.'/Configuration');
68 8
        $definitions = include __DIR__.'/Configuration/services.php';
69 8
        self::$defaultContainer = (
70 8
            new ContainerBuilder($definitions)
71
            )
72 8
            ->getContainer();
73 8
    }
74
75
    /**
76
     * Gets the application dependency injection container
77
     *
78
     * @return ContainerInterface
79
     */
80 6
    public function getContainer()
81
    {
82 6
        if (is_null(self::$container)) {
83 2
            self::$container = $this->checkContainer();
84 1
        }
85 6
        return self::$container;
86
    }
87
88
    /**
89
     * Sets the dependency injection container
90
     *
91
     * @param ContainerInterface $container
92
     */
93 6
    public static function setContainer(ContainerInterface $container)
94
    {
95 6
        self::$container = $container;
96 6
    }
97
98
    /**
99
     * Returns the application dependency container
100
     *
101
     * @return ContainerInterface
102
     */
103 6
    public static function container()
104
    {
105 6
        if (null == self::$container) {
106 2
            $app = new static;
107 2
            $app->getContainer();
108 1
        }
109 6
        return self::$container;
110
    }
111
112
    /**
113
     * Set middleware runner
114
     *
115
     * @param MiddlewareRunnerInterface $runner
116
     *
117
     * @return $this|self|Application
118
     */
119 2
    public function setRunner(MiddlewareRunnerInterface $runner)
120
    {
121 2
        $this->runner = $runner;
122 2
        return $this;
123
    }
124
125
    /**
126
     * Returns the processed response
127
     *
128
     * @return Response
129
     */
130 2
    public function getResponse()
131
    {
132 2
        if (is_null($this->response)) {
133 2
            $this->response = $this->getRunner()->run();
134 1
        }
135 2
        return $this->response;
136
    }
137
138
    /**
139
     * Gets the HTTP middleware runner for this application
140
     *
141
     * @return MiddlewareRunnerInterface
142
     */
143 2
    public function getRunner()
144
    {
145 2
        if (null === $this->runner) {
146 2
            $this->setRunner(
147 2
                $this->getContainer()
148 2
                    ->get('middleware.runner')
149 1
            );
150 1
        }
151 2
        return $this->runner;
152
    }
153
154
    /**
155
     * Set configuration path
156
     * 
157
     * @param string $configPath
158
     * 
159
     * @return Application|self
160
     */
161
    public function setConfigPath($configPath)
162
    {
163
        $this->configPath = $configPath;
164
        Configuration::addPath($configPath);
165
        return $this;
166
    }
167
168
    /**
169
     * Gets configuration path
170
     * 
171
     * @return string
172
     */
173
    public function getConfigPath()
174
    {
175
        if (null == $this->configPath) {
176
            $this->configPath = getcwd().'/Configuration';
177
            Configuration::addPath($this->configPath);
178
        }
179
        return $this->configPath;
180
    }
181
182
    /**
183
     * Gets HTTP request object
184
     *
185
     * @return Request
186
     */
187 2
    public function getRequest()
188
    {
189 2
        if (null === $this->request) {
190 2
            $this->request = $this->getContainer()
191 2
                ->get('request');
192 1
        }
193 2
        return $this->request;
194
    }
195
196
    /**
197
     * Gets container with user overridden settings
198
     * 
199
     * @return ContainerInterface|\Slick\Di\Container
200
     */
201 2
    protected function checkContainer()
202
    {
203 2
        $container = self::$defaultContainer;
204
        if (
205 2
            null != $this->configPath &&
206 1
            file_exists($this->configPath.'/services.php')
207 1
        ) {
208
            $definitions = include $this->configPath.'/services.php';
209
            $container = (
210
                new ContainerBuilder(
211
                    $definitions,
212
                    true
213
                )
214
            )->getContainer();
215
        }
216 2
        return $container;
217
    }
218
219
}