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