PathMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 30
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A register() 0 5 1
A process() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Routing\Middleware;
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
12
class PathMiddleware implements MiddlewareInterface
13
{
14
    protected $paths = [];
15
16 3
    public function __construct(array $paths = [])
17
    {
18 3
        foreach ($paths as $path => $middleware)
19
        {
20 2
            $this->register($path, $middleware);
21
        }
22 3
    }
23
24 3
    public function register(string $path, MiddlewareInterface $middleware): self
25
    {
26 3
        $this->paths[$path] = $middleware;
27 3
        return $this;
28
    }
29
30 2
    public function process(ServerRequestInterface $req, RequestHandlerInterface $handler): ResponseInterface
31
    {
32 2
        $path = $req->getUri()->getPath();
33
34 2
        if (isset($this->paths[$path]))
35
        {
36 1
            return ($this->paths[$path])->process($req, $handler);
37
        }
38
39 1
        return $handler->handle($req);
40
    }
41
}