|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/event-sourcing project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Daikon\EventSourcing\EventStore\Commit; |
|
12
|
|
|
|
|
13
|
|
|
use Daikon\EventSourcing\Aggregate\AggregateId; |
|
14
|
|
|
use Daikon\EventSourcing\Aggregate\AggregateIdInterface; |
|
15
|
|
|
use Daikon\EventSourcing\Aggregate\AggregateRevision; |
|
16
|
|
|
use Daikon\EventSourcing\Aggregate\Event\DomainEventSequence; |
|
17
|
|
|
use Daikon\EventSourcing\Aggregate\Event\DomainEventSequenceInterface; |
|
18
|
|
|
use Daikon\EventSourcing\EventStore\Stream\Sequence; |
|
19
|
|
|
use Daikon\Interop\Assertion; |
|
20
|
|
|
use Daikon\Metadata\Metadata; |
|
21
|
|
|
use Daikon\Metadata\MetadataInterface; |
|
22
|
|
|
use DateTimeImmutable; |
|
23
|
|
|
|
|
24
|
|
|
final class Commit implements CommitInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private const NATIVE_FORMAT = 'Y-m-d\TH:i:s.uP'; |
|
27
|
|
|
|
|
28
|
|
|
private AggregateIdInterface $aggregateId; |
|
29
|
|
|
|
|
30
|
|
|
private Sequence $sequence; |
|
31
|
|
|
|
|
32
|
|
|
private DateTimeImmutable $committedAt; |
|
33
|
|
|
|
|
34
|
|
|
private DomainEventSequenceInterface $eventLog; |
|
35
|
|
|
|
|
36
|
|
|
private MetadataInterface $metadata; |
|
37
|
|
|
|
|
38
|
|
|
public static function make( |
|
39
|
|
|
AggregateIdInterface $aggregateId, |
|
40
|
|
|
Sequence $sequence, |
|
41
|
|
|
DomainEventSequenceInterface $eventLog, |
|
42
|
|
|
MetadataInterface $metadata |
|
43
|
|
|
): self { |
|
44
|
|
|
return new self($aggregateId, $sequence, new DateTimeImmutable, $eventLog, $metadata); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** @param array $state */ |
|
48
|
|
|
public static function fromNative($state): self |
|
49
|
|
|
{ |
|
50
|
|
|
Assertion::keyExists($state, 'aggregateId'); |
|
51
|
|
|
Assertion::keyExists($state, 'sequence'); |
|
52
|
|
|
Assertion::keyExists($state, 'committedAt'); |
|
53
|
|
|
Assertion::keyExists($state, 'eventLog'); |
|
54
|
|
|
Assertion::keyExists($state, 'metadata'); |
|
55
|
|
|
Assertion::date($state['committedAt'], self::NATIVE_FORMAT); |
|
56
|
|
|
|
|
57
|
|
|
/** @psalm-suppress PossiblyFalseArgument */ |
|
58
|
|
|
return new self( |
|
59
|
|
|
AggregateId::fromNative($state['aggregateId']), |
|
60
|
|
|
Sequence::fromNative((int) $state['sequence']), |
|
61
|
|
|
DateTimeImmutable::createFromFormat(self::NATIVE_FORMAT, $state['committedAt']), |
|
62
|
|
|
DomainEventSequence::fromNative($state['eventLog']), |
|
63
|
|
|
Metadata::fromNative($state['metadata']) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getAggregateId(): AggregateIdInterface |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->aggregateId; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getSequence(): Sequence |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->sequence; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getCommittedAt(): DateTimeImmutable |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->committedAt; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getHeadRevision(): AggregateRevision |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->eventLog->getHeadRevision(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getTailRevision(): AggregateRevision |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->eventLog->getTailRevision(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getEventLog(): DomainEventSequenceInterface |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->eventLog; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getMetadata(): MetadataInterface |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->metadata; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function toNative(): array |
|
103
|
|
|
{ |
|
104
|
|
|
return [ |
|
105
|
|
|
'@type' => self::class, |
|
106
|
|
|
'aggregateId' => $this->aggregateId->toNative(), |
|
107
|
|
|
'sequence' => $this->sequence->toNative(), |
|
108
|
|
|
'committedAt' => $this->committedAt->format(self::NATIVE_FORMAT), |
|
109
|
|
|
'eventLog' => $this->eventLog->toNative(), |
|
110
|
|
|
'metadata' => $this->metadata->toNative() |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function __construct( |
|
115
|
|
|
AggregateIdInterface $aggregateId, |
|
116
|
|
|
Sequence $sequence, |
|
117
|
|
|
DateTimeImmutable $committedAt, |
|
118
|
|
|
DomainEventSequenceInterface $eventLog, |
|
119
|
|
|
MetadataInterface $metadata |
|
120
|
|
|
) { |
|
121
|
|
|
$this->aggregateId = $aggregateId; |
|
122
|
|
|
$this->sequence = $sequence; |
|
123
|
|
|
$this->committedAt = $committedAt; |
|
124
|
|
|
$this->eventLog = $eventLog; |
|
125
|
|
|
$this->metadata = $metadata; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|