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

Middleware   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 96
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A list() 0 3 1
A before() 0 11 3
A skip() 0 7 2
A add() 0 11 4
A after() 0 11 3
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 1
    public function __construct()
14
    {
15 1
        $this->globalMiddleware = [
16 1
            GlobalHeaders::class => new GlobalHeaders,
17 1
            Cors::class => new Cors,
18
        ];
19 1
    }
20
21
    /**
22
     * Get registered middleware.
23
     *
24
     * @return array
25
     */
26 4
    public function list() : array
27
    {
28 4
        return $this->globalMiddleware;
29
    }
30
31
    /**
32
     * Get before middleware.
33
     *
34
     * @return array
35
     */
36 1
    public function before() : array
37
    {
38 1
        $before = [];
39
40 1
        foreach ($this->globalMiddleware as $m) {
41 1
            if ($m->before()) {
42 1
                $before[] = $m;
43
            }
44
        }
45
46 1
        return $before;
47
    }
48
49
    /**
50
     * Get after middleware.
51
     *
52
     * @return array
53
     */
54 1
    public function after() : array
55
    {
56 1
        $after = [];
57
58 1
        foreach ($this->globalMiddleware as $m) {
59 1
            if ( ! $m->before()) {
60 1
                $after[] = $m;
61
            }
62
        }
63
64 1
        return $after;
65
    }
66
67
    /**
68
     * Set middleware.
69
     *
70
     * @param array $middleware
71
     *
72
     * @return Middleware
73
     */
74 1
    public function add(...$middleware) : self
75
    {
76 1
        foreach ($middleware as $m) {
77 1
            if ($m instanceof TusMiddlewareInterface) {
78 1
                $this->globalMiddleware[get_class($m)] = $m;
79 1
            } else if (is_string($m)) {
80 1
                $this->globalMiddleware[$m] = new $m;
81
            }
82
        }
83
84 1
        return $this;
85
    }
86
87
    /**
88
     * Skip middleware.
89
     *
90
     * @param array ...$middleware
91
     *
92
     * @return Middleware
93
     */
94 1
    public function skip(...$middleware) : self
95
    {
96 1
        foreach ($middleware as $m) {
97 1
            unset($this->globalMiddleware[$m]);
98
        }
99
100 1
        return $this;
101
    }
102
}
103