CallableHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Nimo\Handlers;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
10
/**
11
 * Converts a callback to a RequestHandlerInterface
12
 */
13
class CallableHandler extends AbstractHandler
14
{
15
    /**
16
     * @var callable
17
     */
18
    protected $callable;
19
20 6
    public function __construct(callable $callable)
21
    {
22 6
        $this->callable = $callable;
23 6
    }
24
25 5
    public static function wrap($handler): RequestHandlerInterface
26
    {
27 5
        if ($handler instanceof RequestHandlerInterface) {
28
            return $handler;
29
        }
30 5
        if (is_callable($handler)) {
31 5
            return new self($handler);
32
        }
33
34
        // @codeCoverageIgnoreStart
35
        throw new \InvalidArgumentException('$handler must be a valid handler');
36
        // @codeCoverageIgnoreEnd
37
    }
38
39 2
    protected function main(): ResponseInterface
40
    {
41 2
        return call_user_func($this->callable, $this->request);
42
    }
43
}
44