AggregateRootBehaviour::recordThat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
ccs 1
cts 1
cp 1
crap 1
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> */
0 ignored issues
show
Bug introduced by
The type EventSauce\EventSourcing\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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