FrameRunner::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Sirius\Middleware;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
/**
9
 * FrameRunner Middleware inspired by this article
10
 * http://blog.ircmaxell.com/2016/05/all-about-middleware.html
11
 */
12
class FrameRunner
13
{
14
15
    /* @var runner */
16
    protected $next;
17
18
    protected $middleware;
19
20
    /**
21
     * Creates a runner based on an array of middleware
22
     *
23
     * @param array $middlewares
24
     * @return FrameRunner
25
     */
26 1
    public static function factory(array $middlewares = array())
27
    {
28 1
        $runner = null;
29 1
        foreach ($middlewares as $middleware) {
30 1
            $runner = $runner ? $runner->add($middleware) : new static($middleware);
31 1
        }
32 1
        return $runner;
33
    }
34
35 5
    public function __construct(callable $middleware)
36
    {
37 5
        $this->middleware = $middleware;
38 5
    }
39
40
    /**
41
     * Returns a new FrameRunner
42
     * @param callable $middleware
43
     * @return FrameRunner
44
     */
45 3
    public function add(callable $middleware)
46
    {
47 3
        $runner = new static($middleware);
48 3
        $runner->next = $this;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this of type this<Sirius\Middleware\FrameRunner> is incompatible with the declared type object<Sirius\Middleware\Runner> of property $next.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
50 3
        return $runner;
51
    }
52
53
    /**
54
     * @param ServerRequestInterface $request
55
     * @return ResponseInterface
56
     * @throws \Exception
57
     */
58
    public function __invoke(ServerRequestInterface $request)
59
    {
60
61 5
        $next = $this->next ?: function (ServerRequestInterface $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...
62 1
            throw new \Exception('The first middleware in the stack calls next when it shouldn\'t');
63 5
        };
64
65 5
        $response = call_user_func($this->middleware, $request, $next);
66
67 4
        if (!$response instanceof ResponseInterface) {
68 1
            throw new \Exception('Middleware is not returning a Response object');
69
        }
70
71 3
        return $response;
72
    }
73
}
74