| Total Complexity | 6 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | final class PlayerState |
||
| 10 | { |
||
| 11 | 11 | public function __construct( |
|
| 12 | private NormalizePlayer $normalizePlayer, |
||
| 13 | private DenormalizePlayer $denormalizePlayer, |
||
| 14 | private string $savesLocation, |
||
| 15 | 11 | ) {} |
|
| 16 | |||
| 17 | 1 | public function save(Player $player): void |
|
| 18 | { |
||
| 19 | 1 | $playerNormalized = ($this->normalizePlayer)($player); |
|
| 20 | |||
| 21 | 1 | file_put_contents($this->resolvePlayerSaveFile($player->getName()), json_encode($playerNormalized)); |
|
| 22 | 1 | } |
|
| 23 | |||
| 24 | /** |
||
| 25 | * @throws PlayerStateException |
||
| 26 | */ |
||
| 27 | 2 | public function load(string $playerName): Player |
|
| 28 | { |
||
| 29 | 2 | if (!file_exists($fileName = $this->resolvePlayerSaveFile($playerName))) { |
|
| 30 | 1 | throw PlayerStateException::notFound($fileName); |
|
| 31 | } |
||
| 32 | |||
| 33 | 1 | assert(is_readable($fileName)); |
|
| 34 | 1 | $playerNormalized = json_decode(file_get_contents($fileName) ?: '', true); |
|
| 35 | |||
| 36 | 1 | return ($this->denormalizePlayer)($playerNormalized); |
|
| 37 | } |
||
| 38 | |||
| 39 | 2 | private function resolvePlayerSaveFile(string $playerName): string |
|
| 44 | } |
||
| 45 | } |
||
| 46 |