summaryrefslogtreecommitdiff
path: root/src/TokenStorage.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/TokenStorage.php')
-rw-r--r--src/TokenStorage.php34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/TokenStorage.php b/src/TokenStorage.php
new file mode 100644
index 0000000..d7b8db8
--- /dev/null
+++ b/src/TokenStorage.php
@@ -0,0 +1,34 @@
+<?php
+
+declare(strict_types=1);
+
+namespace TCB;
+
+final class TokenStorage
+{
+ public function __construct(
+ public string $accessToken,
+ public string $refreshToken,
+ public int $expiresOn,
+ 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(
+ accessToken: $storage['accessToken'] ?? '',
+ refreshToken: $storage['refreshToken'] ?? '',
+ expiresOn: $storage['expiresOn'] ?? 0,
+ filename: $filename,
+ );
+ }
+
+ public function save(): void
+ {
+ file_put_contents($this->filename, json_encode($this, JSON_PRETTY_PRINT));
+ }
+}