Completed
Branch master (832503)
by Michael
02:05
created

MiddlewareStackTrait::push()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
namespace Schnittstabil\Psr7\MiddlewareStack;
4
5
/**
6
 * Implentations of stack operations.
7
 */
8
trait MiddlewareStackTrait
9
{
10
    protected $middlewareStack;
11
12
    /**
13
     * Push a middleware onto the top of this stack.
14
     *
15
     * @param callable $newTopMiddleware the middleware to be pushed onto the top.
16
     *
17
     * @return static $this
18
     */
19
    protected function push(callable $newTopMiddleware)
20
    {
21
        $oldStack = $this->middlewareStack;
22
23
        if ($oldStack === null) {
24
            $this->middlewareStack = $newTopMiddleware;
25
26
            return $this;
27
        }
28
29
        $this->middlewareStack = function ($request, $response, callable $next) use ($oldStack, $newTopMiddleware) {
30
            return $newTopMiddleware($request, $response, function ($req, $res) use ($next, $oldStack) {
31
                return $oldStack($req, $res, $next);
32
            });
33
        };
34
35
        return $this;
36
    }
37
}
38