AggregateEventStream   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 107
ccs 19
cts 19
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A sortEvents() 0 13 2
A extractOriginEvents() 0 8 1
1
<?php
2
3
/**
4
 * Event Sourcing implementation.
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\EventSourcing\EventStream;
14
15
use ServiceBus\EventSourcing\AggregateId;
16
17
/**
18
 * Event stream.
19
 *
20
 * @psalm-readonly
21
 */
22
final class AggregateEventStream
23
{
24
    /**
25
     * Stream (aggregate) identifier.
26
     *
27
     * @var AggregateId
28
     */
29
    public $id;
30
31
    /**
32
     * Aggregate class.
33
     *
34
     * @psalm-var class-string<\ServiceBus\EventSourcing\Aggregate>
35
     *
36
     * @var string
37
     */
38
    public $aggregateClass;
39
40
    /**
41
     * Event collection.
42
     *
43
     * @psalm-var array<int, \ServiceBus\EventSourcing\EventStream\AggregateEvent>
44
     *
45
     * @var \ServiceBus\EventSourcing\EventStream\AggregateEvent[]
46
     */
47
    public $events;
48
49
    /**
50
     * Origin event collection.
51
     *
52
     * @psalm-var array<int, object>
53
     *
54
     * @var object[]
55
     */
56
    public $originEvents;
57
58
    /**
59
     * Created at datetime.
60
     *
61
     * @var \DateTimeImmutable
62
     */
63
    public $createdAt;
64
65
    /**
66
     * Closed at datetime.
67
     *
68
     * @var \DateTimeImmutable|null
69
     */
70
    public $closedAt = null;
71
72
    /**
73
     * @psalm-param class-string<\ServiceBus\EventSourcing\Aggregate> $aggregateClass
74
     * @psalm-param array<int, \ServiceBus\EventSourcing\EventStream\AggregateEvent> $events
75
     *
76
     * @param \ServiceBus\EventSourcing\EventStream\AggregateEvent[] $events
77
     */
78 9
    public function __construct(
79
        AggregateId $id,
80
        string $aggregateClass,
81
        array $events,
82
        \DateTimeImmutable $createdAt,
83
        ?\DateTimeImmutable $closedAt
84
    ) {
85 9
        $this->id             = $id;
86 9
        $this->aggregateClass = $aggregateClass;
87 9
        $this->events         = self::sortEvents($events);
88 9
        $this->originEvents   = self::extractOriginEvents($this->events);
89 9
        $this->createdAt      = $createdAt;
90 9
        $this->closedAt       = $closedAt;
91 9
    }
92
93
    /**
94
     * @psalm-param  array<int, \ServiceBus\EventSourcing\EventStream\AggregateEvent> $events
95
     *
96
     * @psalm-return array<int, \ServiceBus\EventSourcing\EventStream\AggregateEvent>
97
     */
98 9
    private static function sortEvents(array $events): array
99
    {
100 9
        $result = [];
101
102 9
        foreach ($events as $aggregateEvent)
103
        {
104
            /** @var \ServiceBus\EventSourcing\EventStream\AggregateEvent $aggregateEvent */
105 9
            $result[$aggregateEvent->playhead] = $aggregateEvent;
106
        }
107
108 9
        \ksort($result);
109
110 9
        return $result;
111
    }
112
113
    /**
114
     * @psalm-param  array<int, \ServiceBus\EventSourcing\EventStream\AggregateEvent> $events
115
     * @psalm-return array<int, object>
116
     *
117
     * @param \ServiceBus\EventSourcing\EventStream\AggregateEvent[] $events
118
     *
119
     * @return object[]
120
     */
121 9
    private static function extractOriginEvents(array $events): array
122
    {
123 9
        return \array_map(
124
            static function(AggregateEvent $event): object
125
            {
126 9
                return $event->event;
127 9
            },
128 9
            $events
129
        );
130
    }
131
}
132