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

Responder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 16
cp 0
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 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
        $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