Passed
Push — master ( 81835c...bfb296 )
by Thorsten
03:09
created

Commit::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/cqrs 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\AggregateRevision;
14
use Daikon\EventSourcing\Aggregate\DomainEventSequence;
15
use Daikon\EventSourcing\Aggregate\DomainEventSequenceInterface;
16
use Daikon\EventSourcing\EventStore\Stream\StreamId;
17
use Daikon\EventSourcing\EventStore\Stream\StreamIdInterface;
18
use Daikon\EventSourcing\EventStore\Stream\StreamRevision;
19
use Daikon\MessageBus\MessageInterface;
20
use Daikon\MessageBus\Metadata\Metadata;
21
22
final class Commit implements CommitInterface
23
{
24
    /** @var StreamIdInterface */
25
    private $streamId;
26
27
    /** @var StreamRevision */
28
    private $streamRevision;
29
30
    /** @var DomainEventSequenceInterface */
31
    private $eventLog;
32
33
    /** @var Metadata */
34
    private $metadata;
35
36
    public static function make(
37
        StreamIdInterface $streamId,
38
        StreamRevision $streamRevision,
39
        DomainEventSequenceInterface $eventLog,
40
        Metadata $metadata
41
    ): CommitInterface {
42
        return new self($streamId, $streamRevision, $eventLog, $metadata);
43
    }
44
45
    public static function fromArray(array $state): MessageInterface
46
    {
47
        return new self(
48
            StreamId::fromNative($state['streamId']),
49
            StreamRevision::fromNative((int)$state['streamRevision']),
50
            DomainEventSequence::fromArray($state['eventLog']),
51
            Metadata::fromArray($state['metadata'])
52
        );
53
    }
54
55
    public function getStreamId(): StreamIdInterface
56
    {
57
        return $this->streamId;
58
    }
59
60
    public function getStreamRevision(): StreamRevision
61
    {
62
        return $this->streamRevision;
63
    }
64
65
    public function getAggregateRevision(): AggregateRevision
66
    {
67
        return $this->eventLog->getHeadRevision();
68
    }
69
70
    public function getEventLog(): DomainEventSequenceInterface
71
    {
72
        return $this->eventLog;
73
    }
74
75
    public function getMetadata(): Metadata
76
    {
77
        return $this->metadata;
78
    }
79
80
    public function toArray(): array
81
    {
82
        return [
83
            '@type' => static::class,
84
            'streamId' => $this->streamId->toNative(),
85
            'streamRevision' => $this->streamRevision->toNative(),
86
            'eventLog' => $this->eventLog->toNative(),
87
            'metadata' => $this->metadata->toArray()
88
        ];
89
    }
90
91
    private function __construct(
92
        StreamIdInterface $streamId,
93
        StreamRevision $streamRevision,
94
        DomainEventSequenceInterface $eventLog,
95
        Metadata $metadata
96
    ) {
97
        $this->streamId = $streamId;
98
        $this->streamRevision = $streamRevision;
99
        $this->eventLog = $eventLog;
100
        $this->metadata = $metadata;
101
    }
102
}
103