GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#3)
by
unknown
01:44
created

BaseApp::getNoRouteResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Starlit App.
4
 *
5
 * @copyright Copyright (c) 2016 Starweb AB
6
 * @license   BSD 3-Clause
7
 */
8
9
namespace Starlit\App;
10
11
use Starlit\App\Container\Container;
12
use Starlit\App\Provider\BootableServiceProviderInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
16
use Symfony\Component\HttpFoundation\Session\Session;
17
use Starlit\App\Provider\ServiceProviderInterface;
18
use Starlit\App\Provider\StandardServiceProvider;
19
use Starlit\App\Provider\ErrorServiceProvider;
20
21
/**
22
 * Main framework application and bootstrap class, which also serves as a micro service/dependency injection container.
23
 *
24
 * @author Andreas Nilsson <http://github.com/jandreasn>
25
 */
26
class BaseApp extends Container
27
{
28
    /**
29
     * @const string
30
     */
31
    const CHARSET = 'UTF-8';
32
33
    /**
34
     * @var Config
35
     */
36
    protected $config;
37
38
    /**
39
     * @var string
40
     */
41
    protected $environment;
42
43
    /**
44
     * @var ServiceProviderInterface[]
45
     */
46
    protected $providers = [];
47
48
    /**
49
     * @var bool
50
     */
51
    protected $booted = false;
52
53
    /**
54
     * @var bool
55
     */
56
    protected $isCli = false;
57
58
    /**
59
     * Constructor.
60
     *
61
     * @param array|Config $config
62
     * @param string       $environment Defaults to "production"
63
     */
64 21
    public function __construct($config = [], $environment = 'production')
65
    {
66 21
        if ($config instanceof Config) {
67
            $this->config = $config;
68
        } else {
69 21
            $this->config = new Config($config);
70
        }
71
72 21
        $this->environment = $environment;
73
74 21
        $this->init();
75 21
    }
76
77
    /**
78
     * Initializes the application object.
79
80
     * Override and put initialization code that should always be run as early as
81
     * possible here, but make sure no objects are actually instanced here, because then
82
     * mock objects can't be injected in their place. Place object instance code in
83
     * the preHandle method.
84
     */
85 21
    protected function init()
86
    {
87 21
        $this->isCli = (PHP_SAPI === 'cli');
88
89 21
        if ($this->config->has('phpSettings')) {
90 21
            $this->setPhpSettings($this->config->get('phpSettings'));
91
        }
92
93 21
        $this->registerProviders();
94 21
    }
95
96
    /**
97
     * Register service providers.
98
     */
99 21
    protected function registerProviders()
100
    {
101 21
        $this->register(new ErrorServiceProvider());
102 21
        $this->register(new StandardServiceProvider());
103 21
    }
104
105
    /**
106
     * Register service provider.
107
     *
108
     * @param ServiceProviderInterface $provider
109
     */
110 21
    public function register(ServiceProviderInterface $provider)
111
    {
112 21
        $this->providers[] = $provider;
113
114 21
        $provider->register($this);
115 21
    }
116
117
    /**
118
     * @param array  $phpSettings
119
     * @param string $prefix
120
     */
121 21
    protected function setPhpSettings($phpSettings, $prefix = '')
122
    {
123 21
        foreach ($phpSettings as $key => $val) {
124 21
            $key = $prefix . $key;
125 21
            if (is_scalar($val)) {
126 21
                ini_set($key, $val);
127 21
            } elseif (is_array($val)) {
128 21
                $this->setPhpSettings($val, $key . '.'); // Set sub setting with a recursive call
129
            }
130
        }
131 21
    }
132
133
    /**
134
     * Boot the application and its service providers.
135
     *
136
     * This is normally called by handle(). If requests are not handled
137
     * this method will have to called manually to boot.
138
     */
139 5
    public function boot()
140
    {
141 5
        if ($this->booted) {
142
            return;
143
        }
144
145 5
        foreach ($this->providers as $provider) {
146 5
            if ($provider instanceof BootableServiceProviderInterface) {
147 5
                $provider->boot($this);
148
            }
149
        }
150
151 5
        $this->booted = true;
152 5
    }
153
154
    /**
155
     * Pre handle method meant to be overridden in descendant classes (optional).
156
     *
157
     * This method is called before an request is handled. Object instance code should be
158
     * place here and not in init() (more info about this at init()).
159
     *
160
     * @param Request $request
161
     * @return Response|null
162
     */
163 3
    protected function preHandle(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165 3
    }
166
167
    /**
168
     * Post route method meant to be overridden in descendant classes (optional).
169
     * This method is called before an request is dispatched  but after it's routed. This means that  we know
170
     * it's a valid route and have access to the route attributes at this stage.
171
     *
172
     * @param Request $request
173
     * @return Response|null
174
     */
175 1
    protected function postRoute(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
    {
177 1
    }
178
179
    /**
180
     * Handles an http request and returns a response.
181
     *
182
     * @param Request $request
183
     * @return Response
184
     */
185 4
    public function handle(Request $request)
186
    {
187 4
        $this->alias('request', Request::class);
188 4
        $this->set(Request::class, $request);
189
190 4
        $this->boot();
191
192 4
        if (($preHandleResponse = $this->preHandle($request))) {
193 1
            return $preHandleResponse;
194
        }
195
196
        try {
197 3
            $controller = $this->get(Router::class)->route($request);
198
199 2
            if (($postRouteResponse = $this->postRoute($request))) {
200 1
                return $postRouteResponse;
201
            }
202
203 1
            $response = $controller->dispatch();
204 1
        } catch (ResourceNotFoundException $e) {
205 1
            $response = $this->getNoRouteResponse($request);
206
        }
207
208 2
        $this->postHandle($request);
209
210 2
        return $response;
211
    }
212
213
    /**
214
     * Returns a response for no route / resource not found.
215
     *
216
     * @param Request $request
217
     * @return Response
218
     */
219 1
    protected function getNoRouteResponse(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
220
    {
221 1
        return new Response('Not Found', 404);
222
    }
223
224
    /**
225
     * Post handle method meant to be overridden in descendant classes (optional).
226
     * This method is called after an request has been handled but before
227
     * the response is returned from the handle method.
228
     *
229
     * @param Request $request
230
     */
231 2
    protected function postHandle(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
232
    {
233 2
    }
234
235
    /**
236
     * @return Config
237
     */
238 21
    public function getConfig()
239
    {
240 21
        return $this->config;
241
    }
242
243
    /**
244
     * @return bool
245
     */
246 21
    public function isCli(): bool
247
    {
248 21
        return $this->isCli;
249
    }
250
251
    /**
252
     * @return Session
253
     */
254
    public function getSession()
255
    {
256
        return $this->get(Session::class); // Makes this method faster by bypassing __call() (which is quite slow).
257
    }
258
259
    /**
260
     * @return Router
261
     */
262
    public function getRouter()
263
    {
264
        return $this->get(Router::class); // Makes this method faster by bypassing __call() (which is quite slow).
265
    }
266
267
    /**
268
     * @return Request|null
269
     */
270
    public function getRequest()
271
    {
272
        return $this->has(Request::class) ? $this->get(Request::class) : null;
273
    }
274
275
    /**
276
     * @return string
277
     */
278 1
    public function getEnvironment()
279
    {
280 1
        return $this->environment;
281
    }
282
}
283