Passed
Push — master ( 0010b4...ff0e1a )
by Mr
02:16
created

Responder::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 17
ccs 0
cts 16
cp 0
crap 6
rs 9.8666
c 0
b 0
f 0
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
        $contentType = $request->getHeaderLine('Accept');
22
        $parts = explode('/', $contentType, 2);
23
        Assertion::count($parts, 2, "Invalid content type '$contentType'.");
24
        $methodName = 'respondTo'.ucfirst($parts[1]);
25
        $responseHandler = [$this, $methodName];
26
        if (!is_callable($responseHandler)) {
27
            throw new RuntimeException(sprintf(
28
                "Method '%s' for content type '%s' missing from '%s'.",
29
                $methodName,
30
                $contentType,
31
                static::class
32
            ));
33
        }
34
35
        return $responseHandler($request);
36
    }
37
}
38