Passed
Push — master ( 3d4420...1c003d )
by mcfog
01:46
created

MiddlewareTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 50
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A append() 0 8 1
A prepend() 0 8 1
A when() 0 4 1
A catch() 0 4 1
1
<?php namespace Nimo\Traits;
2
3
use Interop\Http\Server\MiddlewareInterface;
4
use Nimo\MiddlewarePipe;
5
use Nimo\Middlewares\CatchMiddleware;
6
use Nimo\Middlewares\ConditionMiddleware;
7
8
trait MiddlewareTrait
9
{
10
    use AttachToRequestTrait;
11
12
    /**
13
     * append $middleware after this one, return the new $middlewareStack
14
     *
15
     * @param $middleware
16
     * @return MiddlewarePipe
17
     */
18
    public function append($middleware): MiddlewarePipe
19
    {
20
        $stack = new MiddlewarePipe();
21
22
        return $stack
23
            ->append($this)
24
            ->append($middleware);
25
    }
26
27
    /**
28
     * prepend $middleware before this one, return the new $middlewareStack
29
     *
30
     * @param $middleware
31
     * @return MiddlewarePipe
32
     */
33
    public function prepend($middleware): MiddlewarePipe
34
    {
35
        $stack = new MiddlewarePipe();
36
37
        return $stack
38
            ->prepend($this)
39
            ->prepend($middleware);
40
    }
41
42
    /**
43
     * wrap this middleware with $conditionCallback (skip this when the callback return falsy value)
44
     *
45
     * @param callable $conditionCallback ($req, $res, $next)
46
     * @return ConditionMiddleware
47
     */
48
    public function when(callable $conditionCallback): MiddlewareInterface
49
    {
50
        return new ConditionMiddleware($conditionCallback, $this);
51
    }
52
53
    public function catch(callable $catcher, string $catchClass = \Throwable::class): MiddlewareInterface
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
54
    {
55
        return new CatchMiddleware($this, $catcher, $catchClass);
56
    }
57
}
58