Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| FileStateService | |
0.00% |
0 / 20 |
|
0.00% |
0 / 7 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| load | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| save | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| get | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| set | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getAll | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| remove | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Modules\Installer\Services; |
| 4 | |
| 5 | class FileStateService |
| 6 | { |
| 7 | private string $file; |
| 8 | |
| 9 | public function __construct() |
| 10 | { |
| 11 | $this->file = WRITEPATH . 'installer/system_state.json'; |
| 12 | } |
| 13 | |
| 14 | private function load(): array |
| 15 | { |
| 16 | if (!file_exists($this->file)) { |
| 17 | return []; |
| 18 | } |
| 19 | |
| 20 | return json_decode(file_get_contents($this->file), true) ?? []; |
| 21 | } |
| 22 | |
| 23 | private function save(array $data): void |
| 24 | { |
| 25 | $dir = dirname($this->file); |
| 26 | |
| 27 | if (!is_dir($dir)) { |
| 28 | mkdir($dir, 0755, true); |
| 29 | } |
| 30 | |
| 31 | file_put_contents( |
| 32 | $this->file, |
| 33 | json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | public function get(string $key, $default = null) |
| 38 | { |
| 39 | $data = $this->load(); |
| 40 | return $data[$key] ?? $default; |
| 41 | } |
| 42 | |
| 43 | public function set(string $key, $value): void |
| 44 | { |
| 45 | $data = $this->load(); |
| 46 | $data[$key] = $value; |
| 47 | $this->save($data); |
| 48 | } |
| 49 | |
| 50 | public function getAll(): array |
| 51 | { |
| 52 | return $this->load(); |
| 53 | } |
| 54 | |
| 55 | public function remove(string $key): void |
| 56 | { |
| 57 | $data = $this->load(); |
| 58 | unset($data[$key]); |
| 59 | $this->save($data); |
| 60 | } |
| 61 | } |