Passed
Branch master (e3e371)
by Nate
03:50
created

Runner::resolveRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/relay/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/relay
7
 */
8
9
namespace Flipbox\Relay\Runner;
10
11
use Flipbox\Skeleton\Helpers\ObjectHelper;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Log\InvalidArgumentException;
15
use Relay\MiddlewareInterface;
16
use Zend\Diactoros\Request;
17
use Zend\Diactoros\Response;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 3.0.0
22
 */
23
class Runner
24
{
25
    /**
26
     * @var (callable|MiddlewareInterface)[]
27
     */
28
    private $middleware = [];
29
30
    /**
31
     * @param array $queue
32
     */
33
    public function __construct(array $queue)
34
    {
35
        $this->middleware = $queue;
36
    }
37
38
    /**
39
     *
40
     * Calls the next middleware in the queue.
41
     *
42
     * @param RequestInterface $request The incoming request.
43
     * @param ResponseInterface $response The outgoing response.
44
     * @return ResponseInterface
45
     */
46
    public function __invoke(RequestInterface $request = null, ResponseInterface $response = null)
47
    {
48
        $middleware = array_shift($this->middleware);
49
        $middleware = $this->resolve($middleware);
0 ignored issues
show
Bug introduced by
It seems like $middleware defined by $this->resolve($middleware) on line 49 can also be of type null; however, Flipbox\Relay\Runner\Runner::resolve() does only seem to accept callable, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
        return $middleware(
51
            $this->resolveRequest($request),
0 ignored issues
show
Bug introduced by
It seems like $request defined by parameter $request on line 46 can also be of type object<Psr\Http\Message\RequestInterface>; however, Flipbox\Relay\Runner\Runner::resolveRequest() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52
            $this->resolveResponse($response),
0 ignored issues
show
Bug introduced by
It seems like $response defined by parameter $response on line 46 can also be of type object<Psr\Http\Message\ResponseInterface>; however, Flipbox\Relay\Runner\Runner::resolveResponse() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
53
            $this
54
        );
55
    }
56
57
    /**
58
     * Converts a queue middleware to a callable.
59
     *
60
     * @param string|array|callable|MiddlewareInterface $middleware
61
     * @return callable
62
     */
63
    protected function resolve($middleware): callable
64
    {
65
        if (null === $middleware) {
66
            return function (RequestInterface $request, ResponseInterface $response, callable $next) {
0 ignored issues
show
Unused Code introduced by
The parameter $next 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...
67
                return $response;
68
            };
69
        }
70
71
        return $this->resolveMiddleware($middleware);
72
    }
73
74
    /**
75
     * @param $middleware
76
     * @return callable|\Flipbox\Skeleton\Object\ObjectInterface|MiddlewareInterface
77
     */
78
    private function resolveMiddleware($middleware)
79
    {
80
        if ($this->isMiddleware($middleware)) {
81
            return $middleware;
82
        }
83
84
        try {
85
            /** @var callable|MiddlewareInterface $middleware */
86
            $middleware = ObjectHelper::create(
87
                $middleware
88
            );
89
        } catch (\Exception $e) {
90
            throw new InvalidArgumentException($e->getMessage());
91
        }
92
93
        if (!$this->isMiddleware($middleware)) {
94
            throw new InvalidArgumentException("Unable to resolve middleware");
95
        }
96
97
        return $middleware;
98
    }
99
100
    /**
101
     * @param $middleware
102
     * @return bool
103
     */
104
    protected function isMiddleware($middleware): bool
105
    {
106
        return is_callable($middleware) || $middleware instanceof MiddlewareInterface;
107
    }
108
109
    /**
110
     * @param null $request
111
     * @return RequestInterface
112
     */
113
    protected function resolveRequest($request = null): RequestInterface
114
    {
115
        if ($request instanceof RequestInterface) {
116
            return $request;
117
        }
118
119
        return new Request();
120
    }
121
122
    /**
123
     * @param null $response
124
     * @return ResponseInterface
125
     */
126
    protected function resolveResponse($response = null): ResponseInterface
127
    {
128
        if ($response instanceof ResponseInterface) {
129
            return $response;
130
        }
131
132
        return new Response();
133
    }
134
}
135