Issues (14)

src/AggregateRootBehaviour.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing;
6
7
use Generator;
8
9
/**
10
 * @see AggregateRoot
11
 */
12
trait AggregateRootBehaviour
13
{
14
    use AggregateAlwaysAppliesEvents;
15
16
    private AggregateRootId $aggregateRootId;
17
    private int $aggregateRootVersion = 0;
18
    /** @var list<object> */
19
    private array $recordedEvents = [];
20
21
    private function __construct(AggregateRootId $aggregateRootId)
22
    {
23
        $this->aggregateRootId = $aggregateRootId;
24
    }
25
26
    public function aggregateRootId(): AggregateRootId
27
    {
28
        return $this->aggregateRootId;
29 17
    }
30
31 17
    /**
32 17
     * @see AggregateRoot::aggregateRootVersion
33
     */
34 14
    public function aggregateRootVersion(): int
35
    {
36 14
        return $this->aggregateRootVersion;
37
    }
38
39
    protected function recordThat(object $event): void
40
    {
41
        $this->apply($event);
42 16
        $this->recordedEvents[] = $event;
43
    }
44 16
45
    /**
46
     * @return object[]
47 11
     */
48
    public function releaseEvents(): array
49 11
    {
50 11
        $releasedEvents = $this->recordedEvents;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->recordedEvents of type array is incompatible with the declared type EventSauce\EventSourcing\list of property $recordedEvents.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51 11
        $this->recordedEvents = [];
52 11
53
        return $releasedEvents;
54 9
    }
55
56 9
    /**
57 9
     * @see AggregateRoot::reconstituteFromEvents
58 9
     */
59
    public static function reconstituteFromEvents(AggregateRootId $aggregateRootId, Generator $events): static
60
    {
61
        $aggregateRoot = new static($aggregateRootId);
62
63 14
        /** @var object $event */
64
        foreach ($events as $event) {
65 14
            $aggregateRoot->apply($event);
66 14
        }
67
68 14
        $aggregateRoot->aggregateRootVersion = $events->getReturn() ?: 0;
69
70
        return $aggregateRoot;
71
    }
72
}
73