MiddlewareDispatcher   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 19 3
1
<?php
2
3
namespace Cormy\Server;
4
5
use Throwable;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
/**
10
 * Cormy PSR-7 server middleware dispatcher.
11
 */
12
class MiddlewareDispatcher implements RequestHandlerInterface
13
{
14
    /**
15
     * @var callable|MiddlewareInterface
16
     */
17
    protected $middleware;
18
19
    /**
20
     * @var callable|RequestHandlerInterface
21
     */
22
    protected $finalHandler;
23
24
    /**
25
     * Create a Cormy PSR-7 server middleware dispatcher.
26
     *
27
     * @param callable|MiddlewareInterface     $middleware
28
     * @param callable|RequestHandlerInterface $finalHandler
29
     */
30
    public function __construct(callable $middleware, callable $finalHandler)
31
    {
32
        $this->middleware = $middleware;
33
        $this->finalHandler = $finalHandler;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function __invoke(ServerRequestInterface $request):ResponseInterface
40
    {
41
        $middleware = $this->middleware;
42
        $finalHandler = $this->finalHandler;
43
        $current = $middleware($request);
44
45
        while ($current->valid()) {
46
            $nextRequest = $current->current();
47
48
            try {
49
                $nextResponse = $finalHandler($nextRequest);
50
                $current->send($nextResponse);
51
            } catch (Throwable $exception) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
52
                $current->throw($exception);
53
            }
54
        }
55
56
        return $current->getReturn();
57
    }
58
}
59