|
1
|
|
|
<?php |
|
2
|
|
|
namespace SmoothPhp\Domain; |
|
3
|
|
|
|
|
4
|
|
|
use DateTime; |
|
|
|
|
|
|
5
|
|
|
use SmoothPhp\Contracts\Domain\DomainMessage as DomainMessageInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class DomainMessage |
|
9
|
|
|
* @package SmoothPhp\Domain |
|
10
|
|
|
* @author Simon Bennett <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
final class DomainMessage implements DomainMessageInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var int |
|
16
|
|
|
*/ |
|
17
|
|
|
private $playHead; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Metadata |
|
21
|
|
|
*/ |
|
22
|
|
|
private $metadata; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
private $payload; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $id; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var DateTime |
|
36
|
|
|
*/ |
|
37
|
|
|
private $recordedOn; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $id |
|
41
|
|
|
* @param int $playHead |
|
42
|
|
|
* @param Metadata $metadata |
|
43
|
|
|
* @param mixed $payload |
|
44
|
|
|
* @param DateTime $recordedOn |
|
45
|
33 |
|
*/ |
|
46
|
|
|
public function __construct($id, $playHead, Metadata $metadata, $payload, DateTime $recordedOn) |
|
47
|
33 |
|
{ |
|
48
|
33 |
|
$this->id = $id; |
|
49
|
33 |
|
$this->playHead = $playHead; |
|
50
|
33 |
|
$this->metadata = $metadata; |
|
51
|
33 |
|
$this->payload = $payload; |
|
52
|
33 |
|
$this->recordedOn = $recordedOn; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $id |
|
57
|
|
|
* @param int $playHead |
|
58
|
|
|
* @param Metadata $metadata |
|
59
|
|
|
* @param mixed $payload |
|
60
|
|
|
* |
|
61
|
|
|
* @return DomainMessage |
|
62
|
21 |
|
*/ |
|
63
|
|
|
public static function recordNow($id, $playHead, Metadata $metadata, $payload) |
|
64
|
21 |
|
{ |
|
65
|
|
|
return new DomainMessage($id, $playHead, $metadata, $payload, new DateTime); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return int |
|
70
|
6 |
|
*/ |
|
71
|
|
|
public function getPlayHead() |
|
72
|
6 |
|
{ |
|
73
|
|
|
return $this->playHead; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return Metadata |
|
78
|
6 |
|
*/ |
|
79
|
|
|
public function getMetadata() |
|
80
|
6 |
|
{ |
|
81
|
|
|
return $this->metadata; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return mixed |
|
86
|
12 |
|
*/ |
|
87
|
|
|
public function getPayload() |
|
88
|
12 |
|
{ |
|
89
|
|
|
return $this->payload; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
6 |
|
*/ |
|
95
|
|
|
public function getId() |
|
96
|
6 |
|
{ |
|
97
|
|
|
return $this->id; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return DateTime |
|
102
|
6 |
|
*/ |
|
103
|
|
|
public function getRecordedOn() |
|
104
|
6 |
|
{ |
|
105
|
|
|
return $this->recordedOn; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string |
|
110
|
6 |
|
*/ |
|
111
|
|
|
public function getType() |
|
112
|
6 |
|
{ |
|
113
|
|
|
return strtr(get_class($this->payload), '\\', '.'); |
|
114
|
|
|
} |
|
115
|
|
|
} |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: