Passed
Push — master ( 1a23d8...36a2b2 )
by Hannes
01:53
created

CallbackHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 11 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace inroutephp\inroute\Runtime\Middleware;
6
7
use inroutephp\inroute\Runtime\Exception\DispatchException;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
12
/**
13
* PSR-15 wrapper for callbacks generated by {@see Pipeline} during dispatch.
14
*
15
* @internal
16
*/
17
final class CallbackHandler implements RequestHandlerInterface
18
{
19
    /**
20
     * @var callable
21
     */
22
    private $handler;
23
24
    public function __construct(callable $handler)
25
    {
26
        $this->handler = $handler;
27
    }
28
29
    public function handle(ServerRequestInterface $request): ResponseInterface
30
    {
31
        $response = ($this->handler)($request);
32
33
        if (!$response instanceof ResponseInterface) {
34
            throw new DispatchException(
35
                'Handler callable must return a ResponseInterface object. Found: ' . gettype($response)
36
            );
37
        }
38
39
        return $response;
40
    }
41
}
42