Completed
Push — master ( 1c8c00...882636 )
by Cees-Jan
01:21
created

MeasureMiddleware::__invoke()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.568
c 0
b 0
f 0
cc 4
nc 1
nop 2
crap 4
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Rx\ObservableInterface;
7
use WyriHaximus\React\Inspector\CollectorInterface;
8
use WyriHaximus\React\Inspector\Metric;
9
use function ApiClients\Tools\Rx\observableFromArray;
10
use function React\Promise\resolve;
11
12
final class MeasureMiddleware implements CollectorInterface
13
{
14
    /** @var int */
15
    private $current = 0;
16
17
    /** @var int */
18
    private $total = 0;
19
20
    /** @var null|float */
21
    private $tookMin = null;
22
23
    /** @var float */
24
    private $tookMax = 0.0;
25
26
    /** @var float */
27
    private $tookAvg = 0.0;
28
29
    /** @var float */
30
    private $tookTotal = 0.0;
31
32 1
    public function __invoke(ServerRequestInterface $request, callable $next)
33
    {
34 1
        $this->current++;
35 1
        $start = microtime(true);
36
37
        return resolve($next($request))->always(function () use ($start) {
38 1
            $took = microtime(true) - $start;
39 1
            $this->current--;
40 1
            $this->total++;
41 1
            $this->tookTotal += $took;
42
43 1
            if ($this->tookMin === null || $took < $this->tookMin) {
44 1
                $this->tookMin = $took;
45
            }
46
47 1
            if ($this->tookMax < $took) {
48 1
                $this->tookMax = $took;
49
            }
50
51 1
            $this->tookAvg = $this->tookTotal / $this->total;
52 1
        });
53
    }
54
55 1
    public function collect(): ObservableInterface
56
    {
57
        $metrics = [
58 1
            new Metric('current', $this->current),
59 1
            new Metric('total', $this->total),
60 1
            new Metric('took.min', $this->tookMin === null ? 0.0 : $this->tookMin),
61 1
            new Metric('took.max', $this->tookMax),
62 1
            new Metric('took.average', $this->tookAvg),
63 1
            new Metric('took.total', $this->tookTotal),
64
        ];
65
66 1
        $this->total = 0;
67 1
        $this->tookMin = null;
68 1
        $this->tookMax = 0.0;
69 1
        $this->tookAvg = 0.0;
70 1
        $this->tookTotal = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $tookTotal was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
71
72 1
        return observableFromArray($metrics);
73
    }
74
75
    public function cancel(): void
76
    {
77
        // Does not apply to this class
78
    }
79
}
80