|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Antidot\Queue; |
|
6
|
|
|
|
|
7
|
|
|
use Assert\Assertion; |
|
8
|
|
|
use Interop\Queue\Message; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use JsonSerializable; |
|
11
|
|
|
|
|
12
|
|
|
class JobPayload implements JsonSerializable |
|
13
|
|
|
{ |
|
14
|
|
|
public const INVALID_CONTENT_MESSAGE = 'Invalid message content type "%s" given, it must be array or string type.'; |
|
15
|
|
|
protected string $type; |
|
16
|
|
|
/** @var string|array<mixed> */ |
|
17
|
|
|
protected $message; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string|array<mixed> $messageContent |
|
21
|
|
|
*/ |
|
22
|
5 |
|
public static function create(string $messageType, $messageContent): self |
|
23
|
|
|
{ |
|
24
|
5 |
|
$self = new self(); |
|
25
|
5 |
|
$self->type = $messageType; |
|
26
|
5 |
|
$self->assertValidMessageContent($messageContent); |
|
27
|
4 |
|
$self->message = $messageContent; |
|
28
|
|
|
|
|
29
|
4 |
|
return $self; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
4 |
|
public static function createFromMessage(Message $message): self |
|
33
|
|
|
{ |
|
34
|
4 |
|
$self = new self(); |
|
35
|
4 |
|
$payload = json_decode($message->getBody(), true, 10, JSON_THROW_ON_ERROR); |
|
36
|
4 |
|
$self->assertValidMessageData($payload); |
|
37
|
3 |
|
$self->type = $payload['type']; |
|
38
|
3 |
|
$self->message = $payload['message']; |
|
39
|
|
|
|
|
40
|
3 |
|
return $self; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
5 |
|
public function type(): string |
|
44
|
|
|
{ |
|
45
|
5 |
|
return $this->type; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array<mixed>|string |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function message() |
|
52
|
|
|
{ |
|
53
|
3 |
|
return $this->message; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string|array<mixed> $messageContent |
|
58
|
|
|
*/ |
|
59
|
5 |
|
private function assertValidMessageContent($messageContent): void |
|
60
|
|
|
{ |
|
61
|
5 |
|
if (is_string($messageContent) || is_array($messageContent)) { |
|
|
|
|
|
|
62
|
4 |
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
throw new InvalidArgumentException(sprintf(self::INVALID_CONTENT_MESSAGE, gettype($messageContent))); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array<mixed> $payload |
|
70
|
|
|
* @throws \Assert\AssertionFailedException |
|
71
|
|
|
*/ |
|
72
|
4 |
|
private function assertValidMessageData(array $payload): void |
|
73
|
|
|
{ |
|
74
|
4 |
|
Assertion::keyExists($payload, 'type', 'The job payload should have the "type" key.'); |
|
75
|
3 |
|
Assertion::string($payload['type'], 'The job payload kay "type" should have a string value.'); |
|
76
|
3 |
|
Assertion::keyExists($payload, 'message', 'The job payload should have the "message" key.'); |
|
77
|
3 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return array<mixed> |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function jsonSerialize(): array |
|
83
|
|
|
{ |
|
84
|
|
|
return [ |
|
85
|
2 |
|
'type' => $this->type, |
|
86
|
2 |
|
'message' => $this->message, |
|
87
|
|
|
]; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|