Responder   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 20
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 2
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Middleware\Action;
10
11
use Daikon\Interop\Assertion;
12
use Daikon\Interop\RuntimeException;
13
use Fig\Http\Message\StatusCodeInterface;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
17
abstract class Responder implements ResponderInterface, StatusCodeInterface
18
{
19
    public function handle(ServerRequestInterface $request): ResponseInterface
20
    {
21
        Assertion::isInstanceOf($request, DaikonRequest::class);
22
        $contentType = $request->getHeaderLine('Accept');
23
        $parts = explode('/', $contentType, 2);
24
        Assertion::count($parts, 2, "Invalid content type '$contentType'.");
25
        $methodName = 'respondTo'.ucfirst($parts[1]);
26
        $responseHandler = [$this, $methodName];
27
        if (!is_callable($responseHandler)) {
28
            throw new RuntimeException(sprintf(
29
                "Method '%s' for content type '%s' missing from '%s'.",
30
                $methodName,
31
                $contentType,
32
                static::class
33
            ));
34
        }
35
36
        return $responseHandler($request);
37
    }
38
}
39