LazyHandler::handle()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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