.github
2 years ago
CannotObtainLockException.php
4 years ago
FsConnectionFactory.php
4 years ago
FsConsumer.php
4 years ago
FsContext.php
4 years ago
FsDestination.php
4 years ago
FsMessage.php
4 years ago
FsProducer.php
4 years ago
LICENSE
4 years ago
LegacyFilesystemLock.php
4 years ago
Lock.php
4 years ago
README.md
2 years ago
composer.json
2 years ago
FsMessage.php
164 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Enqueue\Fs; |
| 6 | |
| 7 | use Interop\Queue\Message; |
| 8 | |
| 9 | class FsMessage implements Message, \JsonSerializable |
| 10 | { |
| 11 | /** |
| 12 | * @var string |
| 13 | */ |
| 14 | private $body; |
| 15 | |
| 16 | /** |
| 17 | * @var array |
| 18 | */ |
| 19 | private $properties; |
| 20 | |
| 21 | /** |
| 22 | * @var array |
| 23 | */ |
| 24 | private $headers; |
| 25 | |
| 26 | /** |
| 27 | * @var bool |
| 28 | */ |
| 29 | private $redelivered; |
| 30 | |
| 31 | public function __construct(string $body = '', array $properties = [], array $headers = []) |
| 32 | { |
| 33 | $this->body = $body; |
| 34 | $this->properties = $properties; |
| 35 | $this->headers = $headers; |
| 36 | $this->redelivered = false; |
| 37 | } |
| 38 | |
| 39 | public function setBody(string $body): void |
| 40 | { |
| 41 | $this->body = $body; |
| 42 | } |
| 43 | |
| 44 | public function getBody(): string |
| 45 | { |
| 46 | return $this->body; |
| 47 | } |
| 48 | |
| 49 | public function setProperties(array $properties): void |
| 50 | { |
| 51 | $this->properties = $properties; |
| 52 | } |
| 53 | |
| 54 | public function getProperties(): array |
| 55 | { |
| 56 | return $this->properties; |
| 57 | } |
| 58 | |
| 59 | public function setProperty(string $name, $value): void |
| 60 | { |
| 61 | $this->properties[$name] = $value; |
| 62 | } |
| 63 | |
| 64 | public function getProperty(string $name, $default = null) |
| 65 | { |
| 66 | return array_key_exists($name, $this->properties) ? $this->properties[$name] : $default; |
| 67 | } |
| 68 | |
| 69 | public function setHeaders(array $headers): void |
| 70 | { |
| 71 | $this->headers = $headers; |
| 72 | } |
| 73 | |
| 74 | public function getHeaders(): array |
| 75 | { |
| 76 | return $this->headers; |
| 77 | } |
| 78 | |
| 79 | public function setHeader(string $name, $value): void |
| 80 | { |
| 81 | $this->headers[$name] = $value; |
| 82 | } |
| 83 | |
| 84 | public function getHeader(string $name, $default = null) |
| 85 | { |
| 86 | return array_key_exists($name, $this->headers) ? $this->headers[$name] : $default; |
| 87 | } |
| 88 | |
| 89 | public function isRedelivered(): bool |
| 90 | { |
| 91 | return $this->redelivered; |
| 92 | } |
| 93 | |
| 94 | public function setRedelivered(bool $redelivered): void |
| 95 | { |
| 96 | $this->redelivered = $redelivered; |
| 97 | } |
| 98 | |
| 99 | public function setCorrelationId(string $correlationId = null): void |
| 100 | { |
| 101 | $this->setHeader('correlation_id', (string) $correlationId); |
| 102 | } |
| 103 | |
| 104 | public function getCorrelationId(): ?string |
| 105 | { |
| 106 | return $this->getHeader('correlation_id'); |
| 107 | } |
| 108 | |
| 109 | public function setMessageId(string $messageId = null): void |
| 110 | { |
| 111 | $this->setHeader('message_id', (string) $messageId); |
| 112 | } |
| 113 | |
| 114 | public function getMessageId(): ?string |
| 115 | { |
| 116 | return $this->getHeader('message_id'); |
| 117 | } |
| 118 | |
| 119 | public function getTimestamp(): ?int |
| 120 | { |
| 121 | $value = $this->getHeader('timestamp'); |
| 122 | |
| 123 | return null === $value ? null : (int) $value; |
| 124 | } |
| 125 | |
| 126 | public function setTimestamp(int $timestamp = null): void |
| 127 | { |
| 128 | $this->setHeader('timestamp', $timestamp); |
| 129 | } |
| 130 | |
| 131 | public function setReplyTo(string $replyTo = null): void |
| 132 | { |
| 133 | $this->setHeader('reply_to', $replyTo); |
| 134 | } |
| 135 | |
| 136 | public function getReplyTo(): ?string |
| 137 | { |
| 138 | return $this->getHeader('reply_to'); |
| 139 | } |
| 140 | |
| 141 | public function jsonSerialize(): array |
| 142 | { |
| 143 | return [ |
| 144 | 'body' => $this->getBody(), |
| 145 | 'properties' => $this->getProperties(), |
| 146 | 'headers' => $this->getHeaders(), |
| 147 | ]; |
| 148 | } |
| 149 | |
| 150 | public static function jsonUnserialize(string $json): self |
| 151 | { |
| 152 | $data = json_decode($json, true); |
| 153 | if (JSON_ERROR_NONE !== json_last_error()) { |
| 154 | throw new \InvalidArgumentException(sprintf( |
| 155 | 'The malformed json given. Error %s and message %s', |
| 156 | json_last_error(), |
| 157 | json_last_error_msg() |
| 158 | )); |
| 159 | } |
| 160 | |
| 161 | return new self($data['body'], $data['properties'], $data['headers']); |
| 162 | } |
| 163 | } |
| 164 |