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

MeasureMiddleware::cancel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 0
crap 2
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
    private const DEFAULT_PREFIX = '';
15
16
    /** @var string */
17
    private $prefix = self::DEFAULT_PREFIX;
18
19
    /** @var int */
20
    private $current = 0;
21
22
    /** @var int */
23
    private $total = 0;
24
25
    /** @var null|float */
26
    private $tookMin = null;
27
28
    /** @var float */
29
    private $tookMax = 0.0;
30
31
    /** @var float */
32
    private $tookAvg = 0.0;
33
34
    /** @var float */
35
    private $tookTotal = 0.0;
36
37 1
    public function __construct(string $prefix = self::DEFAULT_PREFIX)
38
    {
39 1
        $this->prefix = $prefix;
40 1
    }
41
42 1
    public function __invoke(ServerRequestInterface $request, callable $next)
43
    {
44 1
        $this->current++;
45 1
        $start = microtime(true);
46
47
        return resolve($next($request))->always(function () use ($start) {
48 1
            $took = microtime(true) - $start;
49 1
            $this->current--;
50 1
            $this->total++;
51 1
            $this->tookTotal += $took;
52
53 1
            if ($this->tookMin === null || $took < $this->tookMin) {
54 1
                $this->tookMin = $took;
55
            }
56
57 1
            if ($this->tookMax < $took) {
58 1
                $this->tookMax = $took;
59
            }
60
61 1
            $this->tookAvg = $this->tookTotal / $this->total;
62 1
        });
63
    }
64
65 1
    public function collect(): ObservableInterface
66
    {
67
        $metrics = [
68 1
            new Metric($this->prefix . 'current', $this->current),
69 1
            new Metric($this->prefix . 'total', $this->total),
70 1
            new Metric($this->prefix . 'took.min', $this->tookMin === null ? 0.0 : $this->tookMin),
71 1
            new Metric($this->prefix . 'took.max', $this->tookMax),
72 1
            new Metric($this->prefix . 'took.average', $this->tookAvg),
73 1
            new Metric($this->prefix . 'took.total', $this->tookTotal),
74
        ];
75
76 1
        $this->total = 0;
77 1
        $this->tookMin = null;
78 1
        $this->tookMax = 0.0;
79 1
        $this->tookAvg = 0.0;
80 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...
81
82 1
        return observableFromArray($metrics);
83
    }
84
85
    public function cancel(): void
86
    {
87
        // Does not apply to this class
88
    }
89
}
90