Total Complexity | 5 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
20 | class JsonStorage implements StorageInterface |
||
21 | { |
||
22 | protected string $file; |
||
23 | |||
24 | public function __construct(string $file) |
||
25 | { |
||
26 | $this->file = $file; |
||
27 | } |
||
28 | |||
29 | public function write(array $data): void |
||
30 | { |
||
31 | /** @var string $json */ |
||
32 | $json = json_encode($data); |
||
33 | file_put_contents($this->file, $json); |
||
34 | } |
||
35 | |||
36 | public function read(): array |
||
37 | { |
||
38 | if (!$this->hasData()) { |
||
39 | throw new RuntimeException('Cannot read data, no data stored.'); |
||
40 | } |
||
41 | |||
42 | /** @var string $data */ |
||
43 | $data = file_get_contents($this->file); |
||
44 | |||
45 | return json_decode($data, true); |
||
46 | } |
||
47 | |||
48 | public function hasData(): bool |
||
51 | } |
||
52 | } |
||
53 |