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

MiddlewareTrait::catch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
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