RecentParameterMemory::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Startwind\Forrest\Config;
4
5
class RecentParameterMemory
6
{
7
    private string $memoryFile;
8
9
    private array $memories = [];
10
11
    /**
12
     * @param string $memoryFile
13
     */
14
    public function __construct(string $memoryFile)
15
    {
16
        $this->memoryFile = $memoryFile;
17
18
        if (file_exists($memoryFile)) {
19
            $this->memories = json_decode(file_get_contents($memoryFile), true);
20
        }
21
    }
22
23
    public function addParameter(string $parameterIdentifier, string $value): void
24
    {
25
        $this->memories[$parameterIdentifier] = $value;
26
    }
27
28
    public function hasParameter(string $parameterIdentifier): bool
29
    {
30
        return array_key_exists($parameterIdentifier, $this->memories);
31
    }
32
33
    public function getParameter(string $parameterIdentifier): string
34
    {
35
        if (!$this->hasParameter($parameterIdentifier)) {
36
            throw new \RuntimeException('No parameter with identifier "' . $parameterIdentifier . '" found. Please use hasParameter before using this method.');
37
        }
38
39
        return $this->memories[$parameterIdentifier];
40
    }
41
42
    public function dump(): void
43
    {
44
        file_put_contents($this->memoryFile, json_encode($this->memories));
45
    }
46
}
47