Completed
Push — master ( 8be278...bad0b1 )
by Kristof
16:41
created

DomainMessageBuilder::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\EventSourcing;
4
5
use Broadway\Domain\DateTime;
6
use Broadway\Domain\DomainMessage;
7
use Broadway\Domain\Metadata;
8
use CultuurNet\Broadway\Domain\DomainMessageIsReplayed;
9
use ValueObjects\Identity\UUID;
10
11
/**
12
 * Helper class for building domain messages, to be used in automated tests.
13
 */
14
class DomainMessageBuilder
15
{
16
    /**
17
     * @var string $userId
18
     */
19
    private $userId;
20
21
    /**
22
     * @var string $id
23
     */
24
    private $id;
25
26
    /**
27
     * @var int
28
     */
29
    private $playhead;
30
31
    /**
32
     * @var DateTime
33
     */
34
    private $recordedOn;
35
36
    /**
37
     * @var null|bool
38
     */
39
    private $forReplay;
40
41
    public function setId(string $id): self
42
    {
43
        $this->id = $id;
44
45
        return $this;
46
    }
47
48
    public function setUserId(string $userId): self
49
    {
50
        $this->userId = $userId;
51
52
        return $this;
53
    }
54
55
    public function setRecordedOnFromDateTimeString(string $dateTime): self
56
    {
57
        $this->recordedOn = DateTime::fromString($dateTime);
58
59
        return $this;
60
    }
61
62
    public function setPlayhead(int $i): self
63
    {
64
        $this->playhead = $i;
65
66
        return $this;
67
    }
68
69
    public function forReplay(bool $forReplay = true): self
70
    {
71
        $this->forReplay = $forReplay;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return \Broadway\Domain\DomainMessage
78
     */
79
    public function create($payload)
80
    {
81
        $finalMetaData = new Metadata();
82
83
        $finalMetaData = $finalMetaData->merge(
84
            new Metadata(
85
                [
86
                    'user_id' => $this->userId ?? UUID::generateAsString(),
87
                ]
88
            )
89
        );
90
91
        $message =  new DomainMessage(
92
            $this->id ?? UUID::generateAsString(),
93
            $this->playhead ?? 1,
94
            $finalMetaData,
95
            $payload,
96
            $this->recordedOn ?? DateTime::now()
97
        );
98
99
        if (is_bool($this->forReplay)) {
100
            $replayMetadata = new Metadata(
101
                [
102
                    DomainMessageIsReplayed::METADATA_REPLAY_KEY => $this->forReplay,
103
                ]
104
            );
105
106
            $message = $message->andMetadata($replayMetadata);
107
        }
108
109
        return $message;
110
    }
111
}
112