Passed
Push — master ( 093d25...9ddd4f )
by Mr
01:54
created

Responder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 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 Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
abstract class Responder implements ResponderInterface
17
{
18
    public function __invoke(ServerRequestInterface $request): ResponseInterface
19
    {
20
        $contentType = $request->getHeaderLine('Accept');
21
        $parts = explode('/', $contentType, 2);
22
        Assertion::count($parts, 2, "Invalid content type '$contentType'.");
23
        $methodName = 'respondTo'.ucfirst($parts[1]);
24
        $responseHandler = [$this, $methodName];
25
        if (!is_callable($responseHandler)) {
26
            throw new RuntimeException(sprintf(
27
                "Method '%s' for content type '%s' missing from '%s'.",
28
                $methodName,
29
                $contentType,
30
                static::class
31
            ));
32
        }
33
34
        return $responseHandler($request);
35
    }
36
}
37