PredictionWrapperMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 3
eloc 10
c 2
b 0
f 2
dl 0
loc 27
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldRun() 0 6 2
A __construct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Nimo\Middlewares;
6
7
use Lit\Nimo\Interfaces\RequestPredictionInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
12
/**
13
 * Wraps a middleware, skip inner logic when the RequestPredictionInterface fail (or pass, by `reverted` option)
14
 */
15
class PredictionWrapperMiddleware extends AbstractConditionMiddleware
16
{
17
    /**
18
     * @var RequestPredictionInterface
19
     */
20
    protected $requestPrediction;
21
    /**
22
     * @var bool
23
     */
24
    protected $reverted;
25
26 2
    public function __construct(
27
        MiddlewareInterface $innerMiddleware,
28
        RequestPredictionInterface $requestPrediction,
29
        $reverted = false
30
    ) {
31 2
        parent::__construct($innerMiddleware);
32 2
        $this->requestPrediction = $requestPrediction;
33 2
        $this->reverted = $reverted;
34 2
    }
35
36 2
    protected function shouldRun(ServerRequestInterface $request, RequestHandlerInterface $handler): bool
37
    {
38 2
        if ($this->reverted) {
39 1
            return !$this->requestPrediction->isTrue($request);
40
        } else {
41 2
            return $this->requestPrediction->isTrue($request);
42
        }
43
    }
44
}
45