PipeMiddleware   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 37
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A register() 0 5 1
A handle() 0 11 2
A process() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Pipeline;
6
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use RuntimeException;
12
13
class PipeMiddleware implements MiddlewareInterface, RequestHandlerInterface
14
{
15
    protected $stack = [];
16
17 5
    public function __construct(array $middlewares = [])
18
    {
19 5
        foreach ($middlewares as $middleware)
20
        {
21 3
            $this->register($middleware);
22
        }
23 5
    }
24
25 4
    public function register(MiddlewareInterface $middleware): self
26
    {
27 4
        $this->stack[] = $middleware;
28 4
        return $this;
29
    }
30
31 4
    public function handle(ServerRequestInterface $req): ResponseInterface
32
    {
33 4
        if (! $this->stack)
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->stack of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
34
        {
35 1
            throw new RuntimeException('The pipe is empty');
36
        }
37
38 3
        $pipe = clone $this;
39 3
        $middleware = array_shift($pipe->stack);
40 3
        return $middleware->process($req, $pipe);
41
    }
42
43 2
    public function process(ServerRequestInterface $req, RequestHandlerInterface $handler): ResponseInterface
44
    {
45 2
        $pipe = clone $this;
46 2
        $pipe->stack[] = $handler instanceof MiddlewareInterface ? $handler : new HandlerMiddleware($handler);
47 2
        return $pipe->handle($req);
48
    }
49
}