Issues (20)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/AggregateRoot.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

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
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
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