Completed
Pull Request — master (#31)
by Ankit
02:33
created

Middleware   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A skip() 0 7 2
A add() 0 11 4
A list() 0 3 1
1
<?php
2
3
namespace TusPhp\Middleware;
4
5
class Middleware
6
{
7
    /** @var array */
8
    protected $globalMiddleware = [];
9
10
    /**
11
     * Middleware constructor.
12
     */
13
    public function __construct()
14
    {
15
        $this->globalMiddleware = [
16
            GlobalHeaders::class => new GlobalHeaders(),
17
            Cors::class => new Cors(),
18
        ];
19
    }
20
21
    /**
22
     * Set middleware.
23
     *
24
     * @param array $middleware
25
     *
26
     * @return Middleware
27
     */
28
    public function add(...$middleware) : self
29
    {
30
        foreach ($middleware as $m) {
31
            if ($m instanceof MiddlewareInterface) {
0 ignored issues
show
Bug introduced by
The type TusPhp\Middleware\MiddlewareInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
                $this->globalMiddleware[get_class($m)] = $m;
33
            } else if (is_string($m)) {
34
                $this->globalMiddleware[$m] = new $m;
35
            }
36
        }
37
38
        return $this;
39
    }
40
41
    /**
42
     * Get registered middleware.
43
     *
44
     * @return array
45
     */
46
    public function list() : array
47
    {
48
        return $this->globalMiddleware;
49
    }
50
51
    /**
52
     * Skip middleware.
53
     *
54
     * @param array ...$middleware
55
     *
56
     * @return Middleware
57
     */
58
    public function skip(...$middleware) : self
59
    {
60
        foreach ($middleware as $m) {
61
            unset($this->globalMiddleware[$m]);
62
        }
63
64
        return $this;
65
    }
66
}
67