Passed
Push — master ( af36fa...585457 )
by Nils
02:48
created

Memory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
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 addDataSet(array $dataset): void
42
    {
43
        foreach ($dataset as $key => $value) {
44
            $this->addData($key, $value);
45
        }
46
    }
47
48
    public function getNumberOfGreaterThan(string $key, int $threshold): int
49
    {
50
        if (!array_key_exists($key, $this->data)) return 0;
51
52
        $count = 0;
53
54
        foreach ($this->data[$key] as $value) {
55
            if ($value >= $threshold) $count++;
56
        }
57
58
        return $count;
59
    }
60
61
    public function getData(): array
62
    {
63
        return $this->data;
64
    }
65
}