Passed
Branch master (733912)
by Woody
03:06
created

LazyMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 27
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A process() 0 6 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\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
13
class LazyMiddleware implements MiddlewareInterface
14
{
15
    /** @var ContainerInterface */
16
    private $container;
17
18
    /** @var string */
19
    private $middleware;
20
21 3
    public function __construct(ContainerInterface $container, string $middleware)
22
    {
23 3
        if (! is_subclass_of($middleware, MiddlewareInterface::class, true)) {
24 1
            throw new InvalidArgumentException(
25 1
                sprintf('%s does not implement %s', $middleware, MiddlewareInterface::class)
26
            );
27
        }
28
29 2
        $this->container = $container;
30 2
        $this->middleware = $middleware;
31 2
    }
32
33
    // MiddlewareInterface
34 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
35
    {
36
        /** @var MiddlewareInterface */
37 1
        $middleware = $this->container->get($this->middleware);
38
39 1
        return $middleware->process($request, $handler);
40
    }
41
}
42