| Total Complexity | 13 |
| Total Lines | 57 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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 |
||
| 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 |
||
| 64 | } |
||
| 65 | } |