| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\EventSourcing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\LazyCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Spatie\EventSourcing\EventSerializers\EventSerializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Spatie\EventSourcing\Exceptions\InvalidEloquentStoredEventModel; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  | use Spatie\EventSourcing\Models\EloquentStoredEvent; | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class EloquentStoredEventRepository implements StoredEventRepository | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     protected string $storedEventModel; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->storedEventModel = config('event-sourcing.stored_event_model', EloquentStoredEvent::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         if (! new $this->storedEventModel instanceof EloquentStoredEvent) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             throw new InvalidEloquentStoredEventModel("The class {$this->storedEventModel} must extend EloquentStoredEvent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function retrieveAll(string $uuid = null): LazyCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         /** @var \Illuminate\Database\Query\Builder $query */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $query = $this->storedEventModel::query(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         if ($uuid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $query->uuid($uuid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return $query->orderBy('id')->cursor()->map(fn (EloquentStoredEvent $storedEvent) => $storedEvent->toStoredEvent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function retrieveAllStartingFrom(int $startingFrom, string $uuid = null): LazyCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $query = $this->prepareEventModelQuery($startingFrom, $uuid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return $query->orderBy('id')->cursor()->map(fn (EloquentStoredEvent $storedEvent) => $storedEvent->toStoredEvent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function countAllStartingFrom(int $startingFrom, string $uuid = null): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return $this->prepareEventModelQuery($startingFrom, $uuid)->count('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function retrieveAllAfterVersion(int $version, string $uuid): LazyCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         /** @var \Illuminate\Database\Query\Builder $query */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $query = $this->storedEventModel::query() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ->uuid($uuid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ->afterVersion($version); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $query->orderBy('id')->cursor()->map(fn (EloquentStoredEvent $storedEvent) => $storedEvent->toStoredEvent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function persist(ShouldBeStored $event, string $uuid = null, int $aggregateVersion = null): StoredEvent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         /** @var EloquentStoredEvent $eloquentStoredEvent */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $eloquentStoredEvent = new $this->storedEventModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $eloquentStoredEvent->setRawAttributes([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'event_properties' => app(EventSerializer::class)->serialize(clone $event), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'aggregate_uuid' => $uuid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'aggregate_version' => $aggregateVersion, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             'event_class' => self::getEventClass(get_class($event)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             'meta_data' => json_encode([]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             'created_at' => Carbon::now(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $eloquentStoredEvent->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $eloquentStoredEvent->toStoredEvent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function persistMany(array $events, string $uuid = null, int $aggregateVersion = null): LazyCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $storedEvents = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         foreach ($events as $event) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $storedEvents[] = self::persist($event, $uuid, $aggregateVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return new LazyCollection($storedEvents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function update(StoredEvent $storedEvent): StoredEvent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         /** @var EloquentStoredEvent $eloquentStoredEvent */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $eloquentStoredEvent = $this->storedEventModel::find($storedEvent->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $eloquentStoredEvent->update($storedEvent->toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $eloquentStoredEvent->toStoredEvent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     private function getEventClass(string $class): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $map = config('event-sourcing.event_class_map', []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         if (! empty($map) && in_array($class, $map)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             return array_search($class, $map, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function getLatestAggregateVersion(string $aggregateUuid): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $this->storedEventModel::query() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             ->where('aggregate_uuid', $aggregateUuid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             ->max('aggregate_version') ?? 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     private function prepareEventModelQuery(int $startingFrom, string $uuid = null): Builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         /** @var Builder $query */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $query = $this->storedEventModel::query()->startingFrom($startingFrom); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         if ($uuid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $query->uuid($uuid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |