| Total Complexity | 4 |
| Total Lines | 44 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | class HighscoreService |
||
| 10 | { |
||
| 11 | /** @var string Sökvägen till JSON-filen som lagrar highscores */ |
||
| 12 | private string $file; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Konstruktor. |
||
| 16 | * |
||
| 17 | * @param string $projectDir Projektets rotkatalog (används för att hitta/highscores.json) |
||
| 18 | */ |
||
| 19 | 3 | public function __construct(string $projectDir) |
|
| 20 | { |
||
| 21 | 3 | $this->file = $projectDir.'/var/highscores.json'; |
|
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Returnerar en lista med highscores. |
||
| 26 | * |
||
| 27 | * @return array En array med highscore-poster, varje post innehåller 'name' och 'score' |
||
| 28 | */ |
||
| 29 | 3 | public function getHighscores(): array |
|
| 30 | { |
||
| 31 | 3 | if (!file_exists($this->file)) { |
|
| 32 | 3 | return []; |
|
| 33 | } |
||
| 34 | |||
| 35 | 2 | return json_decode(file_get_contents($this->file), true) ?? []; |
|
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Lägger till en ny score och uppdaterar filen. |
||
| 40 | * Endast de 10 bästa resultaten sparas. |
||
| 41 | * |
||
| 42 | * @param string $name Namn på spelaren |
||
| 43 | * @param int $score Poäng |
||
| 44 | */ |
||
| 45 | 2 | public function addScore(string $name, int $score): void |
|
| 53 | } |
||
| 54 | } |
||
| 55 |