6
6
use Puzzle \AMQP \WritableMessage ;
7
7
use Puzzle \AMQP \Messages \Bodies \NullBody ;
8
8
use Puzzle \Pieces \ConvertibleToString ;
9
+ use Puzzle \Pieces \Exceptions \JsonEncodeError ;
10
+ use Puzzle \Pieces \Json ;
9
11
10
12
class Message implements WritableMessage, ConvertibleToString
11
13
{
12
- const
14
+ public const
13
15
ATTRIBUTE_CONTENT_TYPE = 'content_type ' ;
14
16
15
17
use BodySetter;
@@ -22,7 +24,7 @@ class Message implements WritableMessage, ConvertibleToString
22
24
$ headers ,
23
25
$ attributes ;
24
26
25
- public function __construct ($ routingKey = '' )
27
+ public function __construct (string $ routingKey = '' )
26
28
{
27
29
$ this ->body = new NullBody ();
28
30
@@ -35,38 +37,38 @@ public function __construct($routingKey = '')
35
37
$ this ->changeRoutingKey ($ routingKey );
36
38
}
37
39
38
- public function changeRoutingKey ($ routingKey )
40
+ public function changeRoutingKey (string $ routingKey ): void
39
41
{
40
42
$ this ->setAttribute ('routing_key ' , $ routingKey );
41
43
}
42
44
43
- private function initializeAttributes ()
45
+ private function initializeAttributes (): void
44
46
{
45
47
$ this ->attributes = array (
46
48
'routing_key ' => null ,
47
49
self ::ATTRIBUTE_CONTENT_TYPE => $ this ->getContentType (),
48
50
'content_encoding ' => 'utf8 ' ,
49
- 'message_id ' => function ($ timestamp ) {
51
+ 'message_id ' => function (int $ timestamp ) {
50
52
return sha1 ($ this ->getRoutingKey () . $ timestamp . $ this ->generateBodyId () . mt_rand ());
51
53
},
52
54
'user_id ' => null ,
53
55
'app_id ' => null ,
54
56
'delivery_mode ' => self ::PERSISTENT ,
55
57
'priority ' => null ,
56
- 'timestamp ' => function ($ timestamp ) {
58
+ 'timestamp ' => function (int $ timestamp ) {
57
59
return $ timestamp ;
58
60
},
59
61
'expiration ' => null ,
60
62
'type ' => null ,
61
63
'reply_to ' => null ,
62
64
'correlation_id ' => null ,
63
- 'headers ' => function ($ timestamp ) {
65
+ 'headers ' => function (int $ timestamp ) {
64
66
return $ this ->packHeaders ($ timestamp );
65
67
},
66
68
);
67
69
}
68
70
69
- private function generateBodyId ()
71
+ private function generateBodyId (): string
70
72
{
71
73
if ($ this ->body instanceof Footprintable)
72
74
{
@@ -76,19 +78,19 @@ private function generateBodyId()
76
78
return uniqid (true );
77
79
}
78
80
79
- public function canBeDroppedSilently ()
81
+ public function canBeDroppedSilently (): bool
80
82
{
81
83
return $ this ->canBeDroppedSilently ;
82
84
}
83
85
84
- public function disallowSilentDropping ()
86
+ public function disallowSilentDropping (): WritableMessage
85
87
{
86
88
$ this ->canBeDroppedSilently = false ;
87
89
88
90
return $ this ;
89
91
}
90
92
91
- public function getContentType ()
93
+ public function getContentType (): string
92
94
{
93
95
if ($ this ->userContentType === null )
94
96
{
@@ -98,37 +100,37 @@ public function getContentType()
98
100
return $ this ->userContentType ;
99
101
}
100
102
101
- public function getRoutingKey ()
103
+ public function getRoutingKey (): string
102
104
{
103
- return $ this ->getAttribute ('routing_key ' );
105
+ return ( string ) $ this ->getAttribute ('routing_key ' );
104
106
}
105
107
106
108
public function getBodyInTransportFormat ()
107
109
{
108
110
return $ this ->body ->asTransported ();
109
111
}
110
112
111
- public function setBody (Body $ body )
113
+ public function setBody (Body $ body ): WritableMessage
112
114
{
113
115
$ this ->body = $ body ;
114
116
$ this ->updateContentType ();
115
117
116
118
return $ this ;
117
119
}
118
120
119
- private function updateContentType ()
121
+ private function updateContentType (): void
120
122
{
121
123
$ this ->attributes [self ::ATTRIBUTE_CONTENT_TYPE ] = $ this ->getContentType ();
122
124
}
123
125
124
- public function addHeader ($ headerName , $ value )
126
+ public function addHeader (string $ headerName , $ value ): WritableMessage
125
127
{
126
128
$ this ->headers [$ headerName ] = $ value ;
127
129
128
130
return $ this ;
129
131
}
130
132
131
- public function addHeaders (array $ headers )
133
+ public function addHeaders (array $ headers ): WritableMessage
132
134
{
133
135
foreach ($ headers as $ name => $ value )
134
136
{
@@ -138,18 +140,18 @@ public function addHeaders(array $headers)
138
140
return $ this ;
139
141
}
140
142
141
- public function setAuthor ($ author )
143
+ public function setAuthor (string $ author ): WritableMessage
142
144
{
143
145
$ this ->addHeader ('author ' , $ author );
144
146
145
147
return $ this ;
146
148
}
147
149
148
- public function packAttributes ($ timestamp = false )
150
+ public function packAttributes (? int $ timestamp = null ): array
149
151
{
150
152
$ this ->updateContentType ();
151
153
152
- if ($ timestamp === false )
154
+ if ($ timestamp === null )
153
155
{
154
156
$ timestamp = (new \DateTime ("now " ))->getTimestamp ();
155
157
}
@@ -166,14 +168,14 @@ public function packAttributes($timestamp = false)
166
168
}, $ this ->attributes );
167
169
}
168
170
169
- private function packHeaders ($ timestamp )
171
+ private function packHeaders (int $ timestamp ): array
170
172
{
171
173
$ this ->headers ['message_datetime ' ] = date ('Y-m-d H:i:s ' , $ timestamp );
172
174
173
175
return $ this ->headers ;
174
176
}
175
177
176
- public function setAttribute ($ attributeName , $ value )
178
+ public function setAttribute (string $ attributeName , $ value ): WritableMessage
177
179
{
178
180
if ($ attributeName !== 'headers ' )
179
181
{
@@ -191,14 +193,14 @@ public function setAttribute($attributeName, $value)
191
193
return $ this ;
192
194
}
193
195
194
- public function getHeaders ()
196
+ public function getHeaders (): array
195
197
{
196
198
$ attributes = $ this ->packAttributes ();
197
199
198
200
return $ attributes ['headers ' ];
199
201
}
200
202
201
- public function getAttribute ($ attributeName )
203
+ public function getAttribute (string $ attributeName )
202
204
{
203
205
if (array_key_exists ($ attributeName , $ this ->attributes ))
204
206
{
@@ -208,17 +210,23 @@ public function getAttribute($attributeName)
208
210
throw new InvalidArgumentException (sprintf ('Property "%s" is unknown or is not a message property ' , $ attributeName ));
209
211
}
210
212
211
- public function __toString ()
213
+ public function __toString (): string
212
214
{
213
- return json_encode (array (
214
- 'routing_key ' => $ this ->getRoutingKey (),
215
- 'body ' => (string ) $ this ->body ,
216
- 'attributes ' => $ this ->attributes ,
217
- 'can be dropped silently ' => $ this ->canBeDroppedSilently
218
- ));
215
+ try {
216
+ return (string ) Json::encode ([
217
+ 'routing_key ' => $ this ->getRoutingKey (),
218
+ 'body ' => (string ) $ this ->body ,
219
+ 'attributes ' => $ this ->attributes ,
220
+ 'can be dropped silently ' => $ this ->canBeDroppedSilently
221
+ ]);
222
+ }
223
+ catch (JsonEncodeError $ e )
224
+ {
225
+ return sprintf ('Can \'t json encode the message. error: "%s" ' , $ e ->getMessage ());
226
+ }
219
227
}
220
228
221
- public function setExpiration ($ expirationInSeconds )
229
+ public function setExpiration (int $ expirationInSeconds ): WritableMessage
222
230
{
223
231
$ ttlInMs = 1000 * (int ) $ expirationInSeconds ;
224
232
@@ -227,19 +235,19 @@ public function setExpiration($expirationInSeconds)
227
235
return $ this ;
228
236
}
229
237
230
- public function isCompressionAllowed ()
238
+ public function isCompressionAllowed (): bool
231
239
{
232
240
return $ this ->allowCompression ;
233
241
}
234
242
235
- public function allowCompression ($ allow = true )
243
+ public function allowCompression (bool $ allow = true ): WritableMessage
236
244
{
237
245
$ this ->allowCompression = (bool ) $ allow ;
238
246
239
247
return $ this ;
240
248
}
241
249
242
- public function isChunked ()
250
+ public function isChunked (): bool
243
251
{
244
252
return $ this ->body ->isChunked ();
245
253
}
0 commit comments