LazyHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 27
ccs 10
cts 10
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A handle() 0 3 1
A resolve() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Middleware;
5
6
use InvalidArgumentException;
7
use Psr\Container\ContainerInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use function sprintf;
12
13
class LazyHandler implements RequestHandlerInterface
14
{
15
    /** @var ContainerInterface */
16
    private $container;
17
18
    /** @var string */
19
    private $handler;
20
21 3
    public function __construct(ContainerInterface $container, string $handler)
22
    {
23 3
        if ($container->has($handler) === false) {
24 1
            throw new InvalidArgumentException(sprintf('Container is missing handler "%s"', $handler));
25
        }
26
27 2
        $this->container = $container;
28 2
        $this->handler = $handler;
29 2
    }
30
31
    // RequestHandlerInterface
32 1
    public function handle(ServerRequestInterface $request): ResponseInterface
33
    {
34 1
        return $this->resolve()->handle($request);
35
    }
36
37 1
    private function resolve(): RequestHandlerInterface
38
    {
39 1
        return $this->container->get($this->handler);
40
    }
41
}
42