1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Mongo; |
4
|
|
|
|
5
|
|
|
use MongoDB\BSON\ObjectId; |
6
|
|
|
use MongoDB\BSON\UTCDateTime; |
7
|
|
|
|
8
|
|
|
final class Message |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var ObjectId |
12
|
|
|
*/ |
13
|
|
|
private $id; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $payload; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var UTCDateTime |
22
|
|
|
*/ |
23
|
|
|
private $earliestGet; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var float |
27
|
|
|
*/ |
28
|
|
|
private $priority; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Construct a new Message instance. |
32
|
|
|
* |
33
|
|
|
* @param ObjectId $id The unique id of the message. |
34
|
|
|
* @param array $payload The data to store in the message. |
35
|
|
|
* @param UTCDateTime $earliestGet The earliest unix timestamp time which the message can be retreived. |
36
|
|
|
* @param float $priority The priority for order out of get(). 0 is higher priority than 1. |
37
|
|
|
*/ |
38
|
|
|
public function __construct( |
39
|
|
|
ObjectId $id = null, |
40
|
|
|
array $payload = [], |
41
|
|
|
UTCDateTime $earliestGet = null, |
42
|
|
|
float $priority = 0.0 |
43
|
|
|
) { |
44
|
|
|
$this->id = $id ?? new ObjectId(); |
45
|
|
|
$this->payload = $payload; |
46
|
|
|
$this->earliestGet = $earliestGet ?? new UTCDateTime(); |
47
|
|
|
$this->priority = $this->validatePriority($priority); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets the unique id of the message. |
52
|
|
|
* |
53
|
|
|
* @return ObjectId |
54
|
|
|
*/ |
55
|
|
|
public function getId() : ObjectId |
56
|
|
|
{ |
57
|
|
|
return $this->id; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Gets the data to store in the message. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function getPayload() : array |
66
|
|
|
{ |
67
|
|
|
return $this->payload; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Gets the earliest unix timestamp time which the message can be retreived. |
72
|
|
|
* |
73
|
|
|
* @return UTCDateTime |
74
|
|
|
*/ |
75
|
|
|
public function getEarliestGet() : UTCDateTime |
76
|
|
|
{ |
77
|
|
|
return $this->earliestGet; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets the priority for order out of get(). 0 is higher priority than 1. |
82
|
|
|
* |
83
|
|
|
* @return float |
84
|
|
|
*/ |
85
|
|
|
public function getPriority() : float |
86
|
|
|
{ |
87
|
|
|
return $this->priority; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Create a clone of this message with the data to store in the message. |
92
|
|
|
* |
93
|
|
|
* @param array $payload The data to store in the message. |
94
|
|
|
* |
95
|
|
|
* @return Message |
96
|
|
|
*/ |
97
|
|
|
public function withPayload(array $payload) : Message |
98
|
|
|
{ |
99
|
|
|
$clone = clone $this; |
100
|
|
|
$clone->payload = $payload; |
101
|
|
|
return $clone; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create a clone of this message with the earliest unix timestamp time which the message can be retreived. |
106
|
|
|
* |
107
|
|
|
* @param UTCDateTIme $earliestGet The earliest unix timestamp time which the message can be retreived. |
108
|
|
|
* |
109
|
|
|
* @return Message |
110
|
|
|
*/ |
111
|
|
|
public function withEarliestGet(UTCDateTime $earliestGet) : Message |
112
|
|
|
{ |
113
|
|
|
$clone = clone $this; |
114
|
|
|
$clone->earliestGet = $earliestGet; |
115
|
|
|
return $clone; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Create a clone of this message with the priority for order out of get(). 0 is higher priority than 1. |
120
|
|
|
* |
121
|
|
|
* @param float $priority The priority for order out of get(). 0 is higher priority than 1. |
122
|
|
|
* |
123
|
|
|
* @return Message |
124
|
|
|
*/ |
125
|
|
|
public function withPriority(float $priority) : Message |
126
|
|
|
{ |
127
|
|
|
$clone = clone $this; |
128
|
|
|
$clone->priority = $this->validatePriority($priority); |
129
|
|
|
return $clone; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function validatePriority(float $priority) : float |
133
|
|
|
{ |
134
|
|
|
if (is_nan($priority)) { |
135
|
|
|
throw new \InvalidArgumentException('$priority was NaN'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $priority; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|