Build Reactive Signals for Bluesky's AT Protocol Firehose in Laravel
at dev 1.8 kB view raw
1<?php 2 3namespace SocialDept\AtpSignals\Events; 4 5use SocialDept\AtpSignals\Contracts\EventContract; 6use SocialDept\AtpSignals\Enums\SignalCommitOperation; 7 8class CommitEvent implements EventContract 9{ 10 public SignalCommitOperation $operation; 11 12 public function __construct( 13 public string $rev, 14 string|SignalCommitOperation $operation, 15 public string $collection, 16 public string $rkey, 17 public ?object $record = null, 18 public ?string $cid = null, 19 ) { 20 // Convert string to enum if needed 21 $this->operation = is_string($operation) 22 ? SignalCommitOperation::from($operation) 23 : $operation; 24 } 25 26 public function isCreate(): bool 27 { 28 return $this->operation === SignalCommitOperation::Create; 29 } 30 31 public function isUpdate(): bool 32 { 33 return $this->operation === SignalCommitOperation::Update; 34 } 35 36 public function isDelete(): bool 37 { 38 return $this->operation === SignalCommitOperation::Delete; 39 } 40 41 public function uri(): string 42 { 43 return "at://{$this->collection}/{$this->rkey}"; 44 } 45 46 public static function fromArray(array $data): self 47 { 48 return new self( 49 rev: $data['rev'], 50 operation: $data['operation'], 51 collection: $data['collection'], 52 rkey: $data['rkey'], 53 record: isset($data['record']) ? (object) $data['record'] : null, 54 cid: $data['cid'] ?? null, 55 ); 56 } 57 58 public function toArray(): array 59 { 60 return [ 61 'rev' => $this->rev, 62 'operation' => $this->operation->value, 63 'collection' => $this->collection, 64 'rkey' => $this->rkey, 65 'record' => $this->record, 66 'cid' => $this->cid, 67 ]; 68 } 69}