Build Reactive Signals for Bluesky's AT Protocol Firehose in Laravel
1<?php
2
3namespace SocialDept\AtpSignals\Storage;
4
5use Illuminate\Database\Query\Builder;
6use Illuminate\Support\Facades\DB;
7use SocialDept\AtpSignals\Contracts\CursorStore;
8
9class DatabaseCursorStore implements CursorStore
10{
11 protected string $table;
12 protected ?string $connection;
13
14 public function __construct()
15 {
16 $this->table = config('signal.cursor_config.database.table', 'signal_cursors');
17 $this->connection = config('signal.cursor_config.database.connection');
18 }
19
20 public function get(): ?int
21 {
22 $cursor = $this->query()
23 ->where('key', 'jetstream')
24 ->value('cursor');
25
26 return $cursor ? (int) $cursor : null;
27 }
28
29 public function set(int $cursor): void
30 {
31 $this->query()
32 ->updateOrInsert(
33 ['key' => 'jetstream'],
34 [
35 'cursor' => $cursor,
36 'updated_at' => now(),
37 ]
38 );
39 }
40
41 public function clear(): void
42 {
43 $this->query()
44 ->where('key', 'jetstream')
45 ->delete();
46 }
47
48 protected function query(): Builder
49 {
50 return DB::connection($this->connection)
51 ->table($this->table);
52 }
53}