summaryrefslogtreecommitdiff
path: root/src/HistoryStorage.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/HistoryStorage.php')
-rw-r--r--src/HistoryStorage.php41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/HistoryStorage.php b/src/HistoryStorage.php
new file mode 100644
index 0000000..30d9ace
--- /dev/null
+++ b/src/HistoryStorage.php
@@ -0,0 +1,41 @@
+<?php
+
+declare(strict_types=1);
+
+namespace TCB;
+
+final class HistoryStorage
+{
+ public function __construct(
+ public array $playlists,
+ public array $history,
+ private readonly string $filename,
+ ) {}
+
+ static public function load(string $filename): self
+ {
+ $storage = is_readable($filename)
+ ? json_decode(file_get_contents($filename), true)
+ : [];
+
+ return new self(
+ playlists: $storage['playlists'] ?? [],
+ history: $storage['history'] ?? [],
+ filename: $filename,
+ );
+ }
+
+ public function save(): void
+ {
+ file_put_contents($this->filename, json_encode($this, JSON_PRETTY_PRINT));
+ }
+
+ public function saveSong(array $song): void
+ {
+ $this->history[$song['id']] = [
+ 'timestamp' => time(),
+ 'details' => $song,
+ ];
+ $this->save();
+ }
+}