Passed
Push — master ( 79b18a...ffcc8f )
by Nils
02:37
created

Memory::getNumberOfGreaterThan()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Startwind\Inventorio\Metrics\Memory;
4
/**
5
 * @property array $data
6
 */
7
class Memory
8
{
9
    private const MEMORY_SIZE = 12; // one hour if every 5 minutes taken
10
11
    private static ?Memory $instance = null;
12
13
    private array $data = [];
14
15
    static public function getInstance(): Memory
16
    {
17
        if (self::$instance === null) {
18
            self::$instance = new self();
19
        }
20
21
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance could return the type null which is incompatible with the type-hinted return Startwind\Inventorio\Metrics\Memory\Memory. Consider adding an additional type-check to rule them out.
Loading history...
22
    }
23
24
    private function __construct()
25
    {
26
    }
27
28
    public function addData(string $key, float $value): void
29
    {
30
        if (!array_key_exists($key, $this->data)) {
31
            $this->data[$key] = [];
32
        }
33
34
        if (count($this->data[$key]) >= self::MEMORY_SIZE) {
35
            array_shift($this->data[$key]);
36
        }
37
38
        $this->data[$key][] = $value;
39
    }
40
41
    public function getData(string $key): float
42
    {
43
        return $this->data[$key] ?? -1;
44
    }
45
46
    public function addDataSet(array $dataset): void
47
    {
48
        foreach ($dataset as $key => $value) {
49
            $this->addData($key, $value);
50
        }
51
    }
52
53
    public function getNumberOfGreaterThan(string $key, int $threshold): int
54
    {
55
        if (!array_key_exists($key, $this->data)) return 0;
56
57
        $count = 0;
58
59
        foreach ($this->data[$key] as $value) {
60
            if ($value >= $threshold) $count++;
61
        }
62
63
        return $count;
64
    }
65
66
    public function getDataSet(): array
67
    {
68
        return $this->data;
69
    }
70
}