Completed
Pull Request — master (#25)
by Dawid
03:13
created

Application::createPipeline()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 15
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.7666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Application::getControllerAggregate() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Http;
4
5
use Igni\Application\Application as AbstractApplication;
6
use Igni\Application\Controller\ControllerAggregate as AbstractControllerAggregate;
7
use Igni\Application\Exception\ApplicationException;
8
use Igni\Http\Controller\ControllerAggregate;
9
use Igni\Http\Exception\HttpModuleException;
10
use Igni\Http\Middleware\CallableMiddleware;
11
use Igni\Http\Middleware\ErrorMiddleware;
12
use Igni\Http\Middleware\MiddlewarePipe;
13
use Igni\Http\Route as RouteInterface;
14
use Igni\Http\Router\Route;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Igni\Http\Route. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Igni\Http\Server\OnRequest;
16
use Psr\Container\ContainerInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
use Throwable;
22
use Zend\HttpHandlerRunner\Emitter\EmitterInterface;
23
use Zend\HttpHandlerRunner\Emitter\SapiEmitter;
24
25
/**
26
 * @see \Igni\Application\Application
27
 *
28
 * @package Igni\Http
29
 */
30
class Application
31
    extends AbstractApplication
32
    implements MiddlewareAggregate, MiddlewareInterface, RequestHandlerInterface, OnRequest {
33
34
    /**
35
     * @var Router
36
     */
37
    private $router;
38
39
    /**
40
     * @var ControllerAggregate
41
     */
42
    private $controllerAggregate;
43
44
    /**
45
     * @var string[]|MiddlewareInterface[]
46
     */
47
    private $middleware = [];
48
49
    /**
50
     * @var MiddlewarePipe
51
     */
52
    private $pipeline;
53
54
    /**
55
     * @var EmitterInterface
56
     */
57
    private $emitter;
58
59
    /**
60
     * Application constructor.
61
     *
62
     * @param ContainerInterface|null $container
63
     */
64 14
    public function __construct(ContainerInterface $container = null)
65
    {
66 14
        parent::__construct($container);
67
68 14
        if ($this->getContainer()->has(Router::class)) {
69
            $this->router = $this->getContainer()->get(Router::class);
70
        } else {
71 14
            $this->router = new Router\Router();
72
        }
73
74 14
        if ($this->getContainer()->has(ControllerAggregate::class)) {
75
            $this->controllerAggregate = $this->getContainer()->get(ControllerAggregate::class);
76
        } else {
77 14
            $this->controllerAggregate = new ControllerAggregate($this->router);
78
        }
79
80 14
        if ($this->getContainer()->has(EmitterInterface::class)) {
81
            $this->emitter = $this->getContainer()->get(EmitterInterface::class);
82
        } else {
83 14
            $this->emitter = new SapiEmitter();
84
        }
85 14
    }
86
87
    /**
88
     * While testing call this method before handle method.
89
     */
90 8
    public function startup(): void
91
    {
92 8
        $this->handleOnBootListeners();
93 8
        $this->initialize();
94 8
        $this->handleOnRunListeners();
95 8
    }
96
97
    /**
98
     * While testing, call this method after handle method.
99
     */
100 1
    public function shutdown(): void
101
    {
102 1
        $this->handleOnShutDownListeners();
103 1
    }
104
105
    /**
106
     * Startups and run application with/or without dedicated server.
107
     * Once application is run it will listen to incoming http requests,
108
     * and takes care of the entire request flow process.
109
     *
110
     * @param Server|null $server
111
     */
112
    public function run(Server $server = null): void
113
    {
114
        $this->startup();
115
        if ($server) {
116
            $server->addListener($this);
117
            $server->start();
118
        } else {
119
            $response = $this->handle(ServerRequest::fromGlobals());
120
            $this->emitter->emit($response);
121
            if ($response instanceof Response) {
122
                $response->end();
123
            }
124
        }
125
126
        $this->shutdown();
127
    }
128
129
    /**
130
     * Registers PSR-15 compatible middelware.
131
     * Middleware can be either callable object which accepts PSR-7 server request interface and returns
132
     * response interface, or just class name that implements psr-15 middleware or its instance.
133
     *
134
     * @param MiddlewareInterface|callable $middleware
135
     */
136 2
    public function use($middleware): void
137
    {
138 2
        if (!is_subclass_of($middleware, MiddlewareInterface::class)) {
139 2
            if (!is_callable($middleware)) {
140
                throw new ApplicationException(sprintf(
141
                    'Middleware must be either class or object that implements `%s`',
142
                    MiddlewareInterface::class
143
                ));
144
            }
145
146 2
            $middleware = new CallableMiddleware($middleware);
0 ignored issues
show
Bug introduced by
It seems like $middleware can also be of type Psr\Http\Server\MiddlewareInterface; however, parameter $middleware of Igni\Http\Middleware\Cal...ddleware::__construct() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

146
            $middleware = new CallableMiddleware(/** @scrutinizer ignore-type */ $middleware);
Loading history...
147
        }
148
149 2
        $this->middleware[] = $middleware;
150 2
    }
151
152
    /**
153
     * Handles request flow process.
154
     *
155
     * @see MiddlewareInterface::process()
156
     *
157
     * @param ServerRequestInterface $request
158
     * @param RequestHandlerInterface $next
159
     * @return ResponseInterface
160
     */
161 12
    public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
162
    {
163
        /** @var Route $route */
164 12
        $route = $this->router->findRoute(
165 12
            $request->getMethod(),
166 12
            $request->getUri()->getPath()
167
        );
168
169 10
        $controller = $route->getController();
170
171 10
        if ($request instanceof ServerRequest) {
172 10
            $request = $request->withAttributes($route->getAttributes());
173
        }
174
175 10
        if (is_string($controller) &&
176 10
            class_exists($controller) &&
177 10
            in_array(Controller::class, class_implements($controller))
178
        ) {
179
            /** @var Controller $instance */
180
            $instance = $this->resolver->resolve($controller);
181
            return $instance($request);
182
        }
183
184 10
        if (is_callable($controller)) {
185 10
            $response = $controller($request);
186 10
            if (!$response instanceof ResponseInterface) {
187
                throw HttpModuleException::controllerMustReturnValidResponse();
188
            }
189
190 10
            return $response;
191
        }
192
193
        throw HttpModuleException::couldNotRetrieveControllerForRoute($route->getPath());
194
    }
195
196
    /**
197
     * Runs application listeners and handles request flow process.
198
     *
199
     * @param ServerRequestInterface $request
200
     * @return ResponseInterface
201
     */
202 12
    public function handle(ServerRequestInterface $request): ResponseInterface
203
    {
204 12
        $response = $this->getMiddlewarePipe()->handle($request);
205 12
        return $response;
206
    }
207
208
    /**
209
     * Decorator for handle method, used by server instance.
210
     * @see Application::handle()
211
     * @see Server::addListener()
212
     *
213
     * @param ServerRequestInterface $request
214
     * @return ResponseInterface
215
     */
216
    public function onRequest(ServerRequestInterface $request): ResponseInterface
217
    {
218
        return $this->handle($request);
219
    }
220
221
    /**
222
     * Registers new controller that accepts get request
223
     * when request uri matches passed route pattern.
224
     *
225
     * @param string $route
226
     * @param callable $controller
227
     */
228 4
    public function get(string $route, callable $controller): void
229
    {
230 4
        $this->controllerAggregate->add($controller, Route::get($route));
231 4
    }
232
233
    /**
234
     * Registers new controller that accepts post request
235
     * when request uri matches passed route pattern.
236
     *
237
     * @param string $route
238
     * @param callable $controller
239
     */
240 1
    public function post(string $route, callable $controller): void
241
    {
242 1
        $this->controllerAggregate->add($controller, Route::post($route));
243 1
    }
244
245
    /**
246
     * Registers new controller that accepts put request
247
     * when request uri matches passed route pattern.
248
     *
249
     * @param string $route
250
     * @param callable $controller
251
     */
252 1
    public function put(string $route, callable $controller): void
253
    {
254 1
        $this->controllerAggregate->add($controller, Route::put($route));
255 1
    }
256
257
    /**
258
     * Registers new controller that accepts patch request
259
     * when request uri matches passed route pattern.
260
     *
261
     * @param string $route
262
     * @param callable $controller
263
     */
264 1
    public function patch(string $route, callable $controller): void
265
    {
266 1
        $this->controllerAggregate->add($controller, Route::patch($route));
267 1
    }
268
269
    /**
270
     * Registers new controller that accepts delete request
271
     * when request uri matches passed route pattern.
272
     *
273
     * @param string $route
274
     * @param callable $controller
275
     */
276 1
    public function delete(string $route, callable $controller): void
277
    {
278 1
        $this->controllerAggregate->add($controller, Route::delete($route));
279 1
    }
280
281
    /**
282
     * Registers new controller that accepts options request
283
     * when request uri matches passed route pattern.
284
     *
285
     * @param string $route
286
     * @param callable $controller
287
     */
288 1
    public function options(string $route, callable $controller): void
289
    {
290 1
        $this->controllerAggregate->add($controller, Route::options($route));
291 1
    }
292
293
    /**
294
     * Registers new controller that accepts head request
295
     * when request uri matches passed route pattern.
296
     *
297
     * @param string $route
298
     * @param callable $controller
299
     */
300 1
    public function head(string $route, callable $controller): void
301
    {
302 1
        $this->controllerAggregate->add($controller, Route::head($route));
303 1
    }
304
305
    /**
306
     * Registers new controller that listens on the passed route.
307
     *
308
     * @param RouteInterface $route
309
     * @param callable $controller
310
     */
311
    public function on(RouteInterface $route, callable $controller): void
312
    {
313
        $this->controllerAggregate->add($controller, $route);
314
    }
315
316
    /**
317
     * Returns application's controller aggregate.
318
     *
319
     * @return AbstractControllerAggregate
320
     */
321 1
    public function getControllerAggregate(): AbstractControllerAggregate
322
    {
323 1
        return $this->controllerAggregate;
324
    }
325
326 12
    protected function getMiddlewarePipe(): MiddlewarePipe
327
    {
328 12
        if ($this->pipeline) {
329
            return $this->pipeline;
330
        }
331
332 12
        return $this->pipeline = $this->composeMiddlewarePipe();
333
    }
334
335 12
    private function composeMiddlewarePipe(): MiddlewarePipe
336
    {
337 12
        $pipe = new MiddlewarePipe();
338
        $pipe->add(new ErrorMiddleware(function(Throwable $exception) {
339 2
            $this->handleOnErrorListeners($exception);
340 12
        }));
341 12
        foreach ($this->middleware as $middleware) {
342 2
            if (is_string($middleware)) {
343
                $middleware = $this->resolver->resolve($middleware);
344
            }
345 2
            $pipe->add($middleware);
346
        }
347 12
        $pipe->add($this);
348
349 12
        return $pipe;
350
    }
351
}
352