Responder::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 18
ccs 0
cts 10
cp 0
crap 6
rs 9.8333
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
        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