Passed
Push — master ( da738a...72a485 )
by Woody
40s
created

LazyMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 29
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A resolve() 0 3 1
A process() 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\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
13
class LazyMiddleware implements MiddlewareInterface
14
{
15
    /** @var ContainerInterface */
16
    private $container;
0 ignored issues
show
Coding Style introduced by
Private member variable "container" must contain a leading underscore
Loading history...
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
17
18
    /** @var string */
19
    private $middleware;
0 ignored issues
show
Coding Style introduced by
Private member variable "middleware" must contain a leading underscore
Loading history...
20
21 3
    public function __construct(ContainerInterface $container, string $middleware)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
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
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
32
33
    // MiddlewareInterface
34 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
35
    {
36 1
        return $this->resolve()->process($request, $handler);
37
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
38
39 1
    private function resolve(): MiddlewareInterface
40
    {
41 1
        return $this->container->get($this->middleware);
42
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
43
}
44