Passed
Push — master ( b6a195...1e79f7 )
by mcfog
03:21
created

CallableHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 29
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A wrap() 0 11 3
A main() 0 3 1
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
class CallableHandler extends AbstractHandler
11
{
12
    /**
13
     * @var callable
14
     */
15
    protected $callable;
16
17 9
    public function __construct(callable $callable)
18
    {
19 9
        $this->callable = $callable;
20 9
    }
21
22 8
    public static function wrap($handler): RequestHandlerInterface
23
    {
24 8
        if ($handler instanceof RequestHandlerInterface) {
25
            return $handler;
26
        }
27 8
        if (is_callable($handler)) {
28 8
            return new self($handler);
29
        }
30
31
        // @codeCoverageIgnoreStart
32
        throw new \InvalidArgumentException('$handler must be a valid handler');
33
        // @codeCoverageIgnoreEnd
34
    }
35
36 4
    protected function main(): ResponseInterface
37
    {
38 4
        return call_user_func($this->callable, $this->request);
39
    }
40
}
41