This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
                                via PHP's auto-loading mechanism.
                                                    These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php | ||
| 2 | |||
| 3 | namespace Spatie\EventSourcing; | ||
| 4 | |||
| 5 | use Illuminate\Support\Arr; | ||
| 6 | use Illuminate\Support\Str; | ||
| 7 | use ReflectionClass; | ||
| 8 | use ReflectionProperty; | ||
| 9 | use Spatie\EventSourcing\Exceptions\CouldNotPersistAggregate; | ||
| 10 | use Spatie\EventSourcing\Snapshots\Snapshot; | ||
| 11 | use Spatie\EventSourcing\Snapshots\SnapshotRepository; | ||
| 12 | |||
| 13 | abstract class AggregateRoot | ||
| 14 | { | ||
| 15 | private string $uuid = ''; | ||
| 0 ignored issues–
                            show             Bug
    
    
    
        introduced 
                            by  
  Loading history... | |||
| 16 | |||
| 17 | private array $recordedEvents = []; | ||
| 18 | |||
| 19 | protected int $aggregateVersion = 0; | ||
| 20 | |||
| 21 | protected int $aggregateVersionAfterReconstitution = 0; | ||
| 22 | |||
| 23 | protected static bool $allowConcurrency = false; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * @param string $uuid | ||
| 27 | * | ||
| 28 | * @return static | ||
| 29 | */ | ||
| 30 | public static function retrieve(string $uuid): self | ||
| 31 |     { | ||
| 32 | $aggregateRoot = app(static::class); | ||
| 33 | |||
| 34 | $aggregateRoot->uuid = $uuid; | ||
| 35 | |||
| 36 | return $aggregateRoot->reconstituteFromEvents(); | ||
| 37 | } | ||
| 38 | |||
| 39 | public function recordThat(ShouldBeStored $domainEvent): self | ||
| 40 |     { | ||
| 41 | $this->recordedEvents[] = $domainEvent; | ||
| 42 | |||
| 43 | $this->apply($domainEvent); | ||
| 44 | |||
| 45 | return $this; | ||
| 46 | } | ||
| 47 | |||
| 48 | public function persist(): self | ||
| 49 |     { | ||
| 50 | $this->ensureNoOtherEventsHaveBeenPersisted(); | ||
| 51 | |||
| 52 | $storedEvents = call_user_func( | ||
| 53 | [$this->getStoredEventRepository(), 'persistMany'], | ||
| 54 | $this->getAndClearRecordedEvents(), | ||
| 55 | $this->uuid ?? '', | ||
| 56 | $this->aggregateVersion, | ||
| 57 | ); | ||
| 58 | |||
| 59 |         $storedEvents->each(function (StoredEvent $storedEvent) { | ||
| 60 | $storedEvent->handle(); | ||
| 61 | }); | ||
| 62 | |||
| 63 | $this->aggregateVersionAfterReconstitution = $this->aggregateVersion; | ||
| 64 | |||
| 65 | return $this; | ||
| 66 | } | ||
| 67 | |||
| 68 | public function snapshot(): Snapshot | ||
| 69 |     { | ||
| 70 | return $this->getSnapshotRepository()->persist(new Snapshot( | ||
| 71 | $this->uuid, | ||
| 72 | $this->aggregateVersion, | ||
| 73 | $this->getState(), | ||
| 74 | )); | ||
| 75 | } | ||
| 76 | |||
| 77 | protected function getSnapshotRepository(): SnapshotRepository | ||
| 78 |     { | ||
| 79 |         return app($this->snapshotRepository ?? config('event-sourcing.snapshot_repository')); | ||
| 80 | } | ||
| 81 | |||
| 82 | protected function getStoredEventRepository(): StoredEventRepository | ||
| 83 |     { | ||
| 84 |         return app($this->storedEventRepository ?? config('event-sourcing.stored_event_repository')); | ||
| 85 | } | ||
| 86 | |||
| 87 | public function getRecordedEvents(): array | ||
| 88 |     { | ||
| 89 | return $this->recordedEvents; | ||
| 90 | } | ||
| 91 | |||
| 92 | protected function getState(): array | ||
| 93 |     { | ||
| 94 | $class = new ReflectionClass($this); | ||
| 95 | |||
| 96 | return collect($class->getProperties(ReflectionProperty::IS_PUBLIC)) | ||
| 97 | ->reject(fn (ReflectionProperty $reflectionProperty) => $reflectionProperty->isStatic()) | ||
| 98 |             ->mapWithKeys(function (ReflectionProperty $property) { | ||
| 99 |                 return [$property->getName() => $this->{$property->getName()}]; | ||
| 100 | })->toArray(); | ||
| 101 | } | ||
| 102 | |||
| 103 | protected function useState(array $state): void | ||
| 104 |     { | ||
| 105 |         foreach ($state as $key => $value) { | ||
| 106 | $this->$key = $value; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | protected function getAndClearRecordedEvents(): array | ||
| 111 |     { | ||
| 112 | $recordedEvents = $this->recordedEvents; | ||
| 113 | |||
| 114 | $this->recordedEvents = []; | ||
| 115 | |||
| 116 | return $recordedEvents; | ||
| 117 | } | ||
| 118 | |||
| 119 | protected function reconstituteFromEvents(): self | ||
| 120 |     { | ||
| 121 | $storedEventRepository = $this->getStoredEventRepository(); | ||
| 122 | $snapshot = $this->getSnapshotRepository()->retrieve($this->uuid); | ||
| 123 | |||
| 124 |         if ($snapshot) { | ||
| 125 | $this->aggregateVersion = $snapshot->aggregateVersion; | ||
| 126 | $this->useState($snapshot->state); | ||
| 127 | } | ||
| 128 | |||
| 129 | $storedEventRepository->retrieveAllAfterVersion($this->aggregateVersion, $this->uuid) | ||
| 130 |             ->each(function (StoredEvent $storedEvent) { | ||
| 131 | $this->apply($storedEvent->event); | ||
| 132 | }); | ||
| 133 | |||
| 134 | $this->aggregateVersionAfterReconstitution = $this->aggregateVersion; | ||
| 135 | |||
| 136 | return $this; | ||
| 137 | } | ||
| 138 | |||
| 139 | protected function ensureNoOtherEventsHaveBeenPersisted(): void | ||
| 140 |     { | ||
| 141 |         if (static::$allowConcurrency) { | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | $latestPersistedVersionId = $this->getStoredEventRepository()->getLatestAggregateVersion($this->uuid); | ||
| 146 | |||
| 147 |         if ($this->aggregateVersionAfterReconstitution !== $latestPersistedVersionId) { | ||
| 148 | throw CouldNotPersistAggregate::unexpectedVersionAlreadyPersisted( | ||
| 149 | $this, | ||
| 150 | $this->uuid, | ||
| 151 | $this->aggregateVersionAfterReconstitution, | ||
| 152 | $latestPersistedVersionId, | ||
| 153 | ); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | private function apply(ShouldBeStored $event): void | ||
| 158 |     { | ||
| 159 | $classBaseName = class_basename($event); | ||
| 160 | |||
| 161 | $camelCasedBaseName = ucfirst(Str::camel($classBaseName)); | ||
| 162 | |||
| 163 |         $applyingMethodName = "apply{$camelCasedBaseName}"; | ||
| 164 | |||
| 165 |         if (method_exists($this, $applyingMethodName)) { | ||
| 166 | $this->$applyingMethodName($event); | ||
| 167 | } | ||
| 168 | |||
| 169 | $this->aggregateVersion++; | ||
| 170 | } | ||
| 171 | |||
| 172 | /** | ||
| 173 | * @param \Spatie\EventSourcing\ShouldBeStored|\Spatie\EventSourcing\ShouldBeStored[] $events | ||
| 174 | * | ||
| 175 | * @return $this | ||
| 176 | */ | ||
| 177 | public static function fake($events = []): FakeAggregateRoot | ||
| 178 |     { | ||
| 179 | $events = Arr::wrap($events); | ||
| 180 | |||
| 181 | return (new FakeAggregateRoot(app(static::class)))->given($events); | ||
| 182 | } | ||
| 183 | } | ||
| 184 |