Completed
Pull Request — master (#384)
by Kristof
04:13 queued 34s
created

DomainMessageBuilder::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 ValueObjects\Identity\UUID;
9
10
/**
11
 * Helper class for building domain messages, to be used in automated tests.
12
 */
13
class DomainMessageBuilder
14
{
15
    /**
16
     * @var string $userId
17
     */
18
    private $userId;
19
20
    /**
21
     * @var string $id
22
     */
23
    private $id;
24
25
    /**
26
     * @var int
27
     */
28
    private $playhead;
29
30
    /**
31
     * @var DateTime
32
     */
33
    private $recordedOn;
34
35
    public function setId(string $id): self
36
    {
37
        $this->id = $id;
38
39
        return $this;
40
    }
41
42
    public function setUserId(string $userId): self
43
    {
44
        $this->userId = $userId;
45
46
        return $this;
47
    }
48
49
    public function setRecordedOnFromDateTimeString(string $dateTime): self
50
    {
51
        $this->recordedOn = DateTime::fromString($dateTime);
52
53
        return $this;
54
    }
55
56
    public function setPlayhead(int $i): self
57
    {
58
        $this->i = $i;
0 ignored issues
show
Bug introduced by
The property i does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
60
        return $this;
61
    }
62
63
    /**
64
     * @return \Broadway\Domain\DomainMessage
65
     */
66
    public function create($payload)
67
    {
68
        $finalMetaData = new Metadata();
69
70
        $finalMetaData = $finalMetaData->merge(
71
            new Metadata(
72
                [
73
                    'user_id' => $this->userId ?? UUID::generateAsString(),
74
                ]
75
            )
76
        );
77
78
        return new DomainMessage(
79
            $this->id ?? UUID::generateAsString(),
80
            $this->playhead ?? 1,
81
            $finalMetaData,
82
            $payload,
83
            $this->recordedOn ?? DateTime::now()
84
        );
85
    }
86
}
87