Test Failed
Branch master (e46a7e)
by mcfog
02:27
created

CallableHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 31
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

3 Methods

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