LazyHandler::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 1
b 0
f 0
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