Build Reactive Signals for Bluesky's AT Protocol Firehose in Laravel
1<?php
2
3namespace SocialDept\AtpSignals\Events;
4
5use SocialDept\AtpSignals\Contracts\EventContract;
6
7class SignalEvent implements EventContract
8{
9 public function __construct(
10 public string $did,
11 public int $timeUs,
12 public string $kind, // 'commit', 'identity', 'account'
13 public ?CommitEvent $commit = null,
14 public ?IdentityEvent $identity = null,
15 public ?AccountEvent $account = null,
16 ) {
17 }
18
19 public function isCommit(): bool
20 {
21 return $this->kind === 'commit';
22 }
23
24 public function isIdentity(): bool
25 {
26 return $this->kind === 'identity';
27 }
28
29 public function isAccount(): bool
30 {
31 return $this->kind === 'account';
32 }
33
34 public function getCollection(): ?string
35 {
36 return $this->commit?->collection;
37 }
38
39 public function getRecord(): ?object
40 {
41 return $this->commit?->record;
42 }
43
44 public function getOperation(): ?\SocialDept\AtpSignals\Enums\SignalCommitOperation
45 {
46 return $this->commit?->operation;
47 }
48
49 public static function fromArray(array $data): self
50 {
51 $commit = isset($data['commit'])
52 ? CommitEvent::fromArray($data['commit'])
53 : null;
54
55 $identity = isset($data['identity'])
56 ? IdentityEvent::fromArray($data['identity'])
57 : null;
58
59 $account = isset($data['account'])
60 ? AccountEvent::fromArray($data['account'])
61 : null;
62
63 return new self(
64 did: $data['did'],
65 timeUs: $data['time_us'],
66 kind: $data['kind'],
67 commit: $commit,
68 identity: $identity,
69 account: $account,
70 );
71 }
72
73 public function toArray(): array
74 {
75 return [
76 'did' => $this->did,
77 'time_us' => $this->timeUs,
78 'kind' => $this->kind,
79 'commit' => $this->commit?->toArray(),
80 'identity' => $this->identity?->toArray(),
81 'account' => $this->account?->toArray(),
82 ];
83 }
84}