Passed
Branch v2-dev (0ddf7c)
by Henri
10:02
created

MiddlewareTrait::middleware()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
use Psr\Http\Server\RequestHandlerInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use HnrAzevedo\Http\Response;
9
10
trait MiddlewareTrait
11
{
12
    use Helper;
13
14
    protected static array $globalMiddlewares = [];
15
16
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $handler is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

16
    public function process(ServerRequestInterface $request, /** @scrutinizer ignore-unused */ RequestHandlerInterface $handler): ResponseInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

16
    public function process(/** @scrutinizer ignore-unused */ ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17
    {
18
        return new Response();
19
    }
20
21
    public static function globalMiddlewares(array $middlewares): RouterInterface
22
    {
23
        foreach($middlewares as $middleware){
24
            if(!class_exists($middleware)){
25
                throw new \RuntimeException("Middleware class {$middleware} not exists");
26
            }
27
        }
28
        self::getInstance()->middlewares = $middlewares;
0 ignored issues
show
Bug Best Practice introduced by
The property middlewares does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
        return self::getInstance();
30
    }
31
32
    public static function middleware($middlewares): RouterInterface
33
    {
34
        $middlewares = (is_array($middlewares)) ? $middlewares : [ $middlewares ];
35
        $route = self::getInstance()->inSave();
36
        $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares;
37
        self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->routes));
0 ignored issues
show
Bug introduced by
The property routes is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
38
        return self::getInstance();
39
    }
40
41
    private static function existMiddleware(string $name): void
42
    {
43
        if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){
44
            throw new \RuntimeException("Middleware {$name} does not exist");
45
        }
46
    }
47
48
49
}