Onion   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 6 1
B processMiddleware() 0 27 5
1
<?php
2
3
namespace Cormy\Server;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
/**
9
 * Onion style PSR-7 middleware stack.
10
 */
11
class Onion implements RequestHandlerInterface
12
{
13
    /**
14
     * @var RequestHandlerInterface|callable
15
     */
16
    protected $core;
17
18
    /**
19
     * @var (MiddlewareInterface|RequestHandlerInterface|callable)[]
20
     */
21
    protected $scales;
22
23
    /**
24
     * Constructs an onion style PSR-7 middleware stack.
25
     *
26
     * @param RequestHandlerInterface|callable                         $core   the innermost request handler
27
     * @param (MiddlewareInterface|RequestHandlerInterface|callable)[] $scales the middlewares to wrap around the core
28
     */
29
    public function __construct(callable $core, callable ...$scales)
30
    {
31
        $this->core = $core;
32
        $this->scales = $scales;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function __invoke(ServerRequestInterface $request):ResponseInterface
39
    {
40
        $topIndex = count($this->scales) - 1;
41
42
        return $this->processMiddleware($topIndex, $request);
43
    }
44
45
    /**
46
     * Process an incoming server request by delegating it to the middleware specified by $index.
47
     *
48
     * @param int                    $index   the $scales index
49
     * @param ServerRequestInterface $request
50
     *
51
     * @return ResponseInterface
52
     */
53
    protected function processMiddleware(int $index, ServerRequestInterface $request):ResponseInterface
54
    {
55
        if ($index < 0) {
56
            return ($this->core)($request);
57
        }
58
59
        $current = $this->scales[$index]($request);
60
61
        if ($current instanceof ResponseInterface) {
62
            return $current;
63
        }
64
65
        $nextIndex = $index - 1;
66
67
        while ($current->valid()) {
68
            $nextRequest = $current->current();
69
70
            try {
71
                $nextResponse = $this->processMiddleware($nextIndex, $nextRequest);
72
                $current->send($nextResponse);
73
            } catch (\Throwable $exception) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
74
                $current->throw($exception);
75
            }
76
        }
77
78
        return $current->getReturn();
79
    }
80
}
81