Build Reactive Signals for Bluesky's AT Protocol Firehose in Laravel
1<?php
2
3namespace SocialDept\AtpSignals\Storage;
4
5use Illuminate\Support\Facades\File;
6use SocialDept\AtpSignals\Contracts\CursorStore;
7
8class FileCursorStore implements CursorStore
9{
10 protected string $path;
11
12 public function __construct()
13 {
14 $this->path = config('signal.cursor_config.file.path', storage_path('signal/cursor.json'));
15
16 // Ensure directory exists
17 $directory = dirname($this->path);
18 if (! File::exists($directory)) {
19 File::makeDirectory($directory, 0755, true);
20 }
21 }
22
23 public function get(): ?int
24 {
25 if (! File::exists($this->path)) {
26 return null;
27 }
28
29 $data = json_decode(File::get($this->path), true);
30
31 return $data['cursor'] ?? null;
32 }
33
34 public function set(int $cursor): void
35 {
36 File::put($this->path, json_encode([
37 'cursor' => $cursor,
38 'updated_at' => now()->toIso8601String(),
39 ], JSON_PRETTY_PRINT));
40 }
41
42 public function clear(): void
43 {
44 if (File::exists($this->path)) {
45 File::delete($this->path);
46 }
47 }
48}