-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert envelope trait to abstract class #231
base: master
Are you sure you want to change the base?
Changes from all commits
8700123
3ecc37e
bff438d
e4e4723
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,11 @@ | |
|
||
namespace Yiisoft\Queue\Message; | ||
|
||
trait EnvelopeTrait | ||
abstract class Envelope implements EnvelopeInterface | ||
{ | ||
private MessageInterface $message; | ||
|
||
/** | ||
* A mirror of {@see MessageInterface::fromData()} | ||
*/ | ||
abstract public static function fromMessage(MessageInterface $message): self; | ||
public function __construct(protected MessageInterface $message) | ||
{ | ||
} | ||
|
||
public static function fromData(string $handlerName, mixed $data, array $metadata = []): MessageInterface | ||
{ | ||
|
@@ -23,14 +20,6 @@ public function getMessage(): MessageInterface | |
return $this->message; | ||
} | ||
|
||
public function withMessage(MessageInterface $message): self | ||
{ | ||
$instance = clone $this; | ||
$instance->message = $message; | ||
|
||
return $instance; | ||
} | ||
|
||
public function getHandlerName(): string | ||
{ | ||
return $this->message->getHandlerName(); | ||
|
@@ -48,15 +37,12 @@ public function getMetadata(): array | |
[ | ||
EnvelopeInterface::ENVELOPE_STACK_KEY => array_merge( | ||
$this->message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY] ?? [], | ||
[self::class], | ||
[static::class], | ||
), | ||
], | ||
$this->getEnvelopeMetadata(), | ||
); | ||
} | ||
|
||
public function getEnvelopeMetadata(): array | ||
{ | ||
return []; | ||
} | ||
abstract protected function getEnvelopeMetadata(): array; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's good to keep default empty array |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,9 @@ | |
final class Message implements MessageInterface | ||
{ | ||
/** | ||
* @param string $handlerName A name of a handler which should handle this message. | ||
* @param mixed $data Message data, encodable by a queue adapter | ||
* @param array $metadata Message metadata, encodable by a queue adapter | ||
* @param string|null $id Message id | ||
*/ | ||
public function __construct( | ||
private string $handlerName, | ||
|
@@ -37,12 +37,4 @@ public function getMetadata(): array | |
{ | ||
return $this->metadata; | ||
} | ||
|
||
public function withMetadata(array $metadata): self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
||
{ | ||
$instance = clone $this; | ||
$instance->metadata = $metadata; | ||
|
||
return $instance; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,32 +4,28 @@ | |||||
|
||||||
namespace Yiisoft\Queue\Middleware\FailureHandling; | ||||||
|
||||||
use Yiisoft\Queue\Message\EnvelopeInterface; | ||||||
use Yiisoft\Queue\Message\EnvelopeTrait; | ||||||
use Yiisoft\Arrays\ArrayHelper; | ||||||
use Yiisoft\Queue\Message\Envelope; | ||||||
use Yiisoft\Queue\Message\MessageInterface; | ||||||
|
||||||
final class FailureEnvelope implements EnvelopeInterface | ||||||
final class FailureEnvelope extends Envelope | ||||||
{ | ||||||
use EnvelopeTrait; | ||||||
|
||||||
public const FAILURE_META_KEY = 'failure-meta'; | ||||||
|
||||||
public function __construct( | ||||||
private MessageInterface $message, | ||||||
private array $meta = [], | ||||||
MessageInterface $message, | ||||||
private readonly array $failureMeta = [], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) { | ||||||
parent::__construct($message); | ||||||
} | ||||||
|
||||||
public static function fromMessage(MessageInterface $message): self | ||||||
{ | ||||||
return new self($message, $message->getMetadata()[self::FAILURE_META_KEY] ?? []); | ||||||
} | ||||||
|
||||||
public function getMetadata(): array | ||||||
protected function getEnvelopeMetadata(): array | ||||||
{ | ||||||
$meta = $this->message->getMetadata(); | ||||||
$meta[self::FAILURE_META_KEY] = array_merge($meta[self::FAILURE_META_KEY] ?? [], $this->meta); | ||||||
|
||||||
return $meta; | ||||||
return [self::FAILURE_META_KEY => ArrayHelper::merge($this->message->getMetadata()[self::FAILURE_META_KEY] ?? [], $this->failureMeta)]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as you can see |
||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?