Completed
Push — master ( a7b7c1...05b103 )
by Andrii
13:55
created

IfMiddleware::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace hiapi\Core\Http\Psr15\Middleware;
4
5
use Closure;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use hiqdev\yii\compat\DiInvoker;
11
12
class IfMiddleware implements MiddlewareInterface
13
{
14
    /**
15
     * @var Closure
16
     */
17
    private $if;
18
    /**
19
     * @var MiddlewareInterface
20
     */
21
    private $then;
22
    /**
23
     * @var MiddlewareInterface
24
     */
25
    private $else;
26
    /**
27
     * @var DiInvoker
28
     */
29
    private $invoker;
30
31
    public function __construct(Closure $if, MiddlewareInterface $then, MiddlewareInterface $else, DiInvoker $invoker)
32
    {
33
        $this->if = $if;
34
        $this->then = $then;
35
        $this->else = $else;
36
        $this->invoker = $invoker;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
43
    {
44
        $if = $this->invoker->invoke($this->if, ['request' => $request]);
45
        $middleware = $if ? $this->then : $this->else;
46
47
        return $middleware->process($request, $handler);
48
    }
49
}
50