Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.7",
"phpunit/phpunit": "^10.5",
"rector/rector": "^1.0.3",
"rector/rector": "^2.5.2",
"roave/infection-static-analysis-plugin": "^1.34",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.16",
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Yiisoft\Queue\Cli\LoopInterface;
use Yiisoft\Queue\Message\IdEnvelope;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Message\MessageSerializerInterface;
use Yiisoft\Queue\Message\Serializer\MessageSerializerInterface;
use Yiisoft\Queue\MessageStatus;

final class Adapter implements AdapterInterface
Expand Down Expand Up @@ -63,7 +63,7 @@ public function status(int|string $id): MessageStatus
public function push(MessageInterface $message): MessageInterface
{
$payload = $this->serializer->serialize($message);
$id = $this->provider->pushMessage($payload, $message->getMetadata());
$id = $this->provider->pushMessage($payload, $message->getMeta());
return new IdEnvelope($message, $id);
}

Expand Down
63 changes: 51 additions & 12 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@

use Yiisoft\Queue\Message\MessageInterface;

/**
* @psalm-import-type MessagePayload from MessageInterface
* @psalm-import-type MessageMeta from MessageInterface
*/
final class Message implements MessageInterface
{
/**
* @param array<string, mixed> $metadata
*/
public function __construct(
private string $handlerName,
private mixed $data,
/**
* @psalm-var MessagePayload
*/
private bool|int|float|string|array|null $data,
/**
* @psalm-var MessageMeta
*/
private array $metadata,
private int $delay = 0 //delay in seconds
int $delay = 0 //delay in seconds
) {
if ($this->delay > 0) {
if ($delay > 0) {
$this->metadata['delay'] = $delay;
}
}
Expand All @@ -39,30 +46,62 @@ public function getType(): string
return $this->handlerName;
}

public function getPayload(): bool|int|float|string|array|null
{
return $this->data;
}

public function getData(): mixed
{
return $this->data;
}

/**
* @return array<string, mixed>
* @psalm-return MessageMeta
*/
public function getMetadata(): array
public function getMeta(): array
{
return $this->metadata;
}

/**
* @param array<string, mixed> $metadata
* @psalm-return MessageMeta
*/
public function withMetadata(array $metadata): static
public function getMetadata(): array
{
return $this->getMeta();
}

/**
* @psalm-param MessageMeta $meta
*/
public function withMeta(array $meta): static
{
$message = clone $this;
$message->metadata = $metadata;
$message->metadata = $meta;
return $message;
}

public static function fromData(string $type, mixed $data): self
/**
* @psalm-param MessageMeta $metadata
*/
public function withMetadata(array $metadata): static
{
return $this->withMeta($metadata);
}

/**
* @psalm-param MessagePayload $payload
*/
public static function fromPayload(string $type, bool|int|float|string|array|null $payload): static
{
return new self($type, $payload, []);
}

/**
* @psalm-param MessagePayload $data
*/
public static function fromData(string $type, bool|int|float|string|array|null $data): self
{
return new self($type, $data, []);
}
Expand Down
11 changes: 6 additions & 5 deletions tests/Integration/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\Cli\LoopInterface;
use Yiisoft\Queue\Message\JsonMessageSerializer;
use Yiisoft\Queue\Message\GenericMessage as Message;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Message\MessageSerializerInterface;
use Yiisoft\Queue\Message\Serializer\JsonMessageEncoder;
use Yiisoft\Queue\Message\Serializer\MessageSerializer;
use Yiisoft\Queue\Message\Serializer\MessageSerializerInterface;
use Yiisoft\Queue\MessageStatus;
use Yiisoft\Queue\Queue;
use Yiisoft\Queue\Redis\Adapter;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function testStatus(): void

$mockReserved = $this->createMock(QueueProviderInterface::class);
$mockReserved->method('existInReserved')->willReturn(true);
$adapter = new Adapter($mockReserved, new JsonMessageSerializer(), $this->getLoop());
$adapter = new Adapter($mockReserved, new MessageSerializer(new JsonMessageEncoder()), $this->getLoop());

$status = $adapter->status('1');
$this->assertEquals(MessageStatus::RESERVED, $status);
Expand All @@ -83,7 +84,7 @@ public function testListen(): void
$adapter = new Adapter(
$queueProvider
->withChannelName('yii-queue'),
new JsonMessageSerializer(),
new MessageSerializer(new JsonMessageEncoder()),
$mockLoop,
);
$queue = $this->getDefaultQueue($adapter);
Expand Down Expand Up @@ -131,7 +132,7 @@ public function testAdapterNullMessage()

$adapter = new Adapter(
$provider,
new JsonMessageSerializer(),
new MessageSerializer(new JsonMessageEncoder()),
$mockLoop,
);
$notUseHandler = true;
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/ExtendedSimpleMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function __construct(private FileHelper $fileHelper)

public function handle(MessageInterface $message): void
{
$data = $message->getData();
if (null !== $data) {
$data = $message->getPayload();
if (is_array($data)) {
$this->fileHelper->put($data['file_name'], $data['payload']['time']);
}
}
Expand Down
9 changes: 5 additions & 4 deletions tests/Support/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\Cli\LoopInterface;
use Yiisoft\Queue\Cli\SignalLoop;
use Yiisoft\Queue\Message\JsonMessageSerializer;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Message\Serializer\JsonMessageEncoder;
use Yiisoft\Queue\Message\Serializer\MessageSerializer;
use Yiisoft\Queue\Middleware\CallableFactory;
use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher;
use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareFactory;
Expand Down Expand Up @@ -91,8 +92,8 @@ protected function getMessageHandlers(): array
return [
'ext-simple' => [new ExtendedSimpleMessageHandler(new FileHelper()), 'handle'],
'exception-listen' => static function (MessageInterface $message) {
$data = $message->getData();
if (null !== $data) {
$data = $message->getPayload();
if (is_array($data)) {
throw new \RuntimeException((string) $data['payload']['time']);
}
},
Expand Down Expand Up @@ -134,7 +135,7 @@ protected function getAdapter(): AdapterInterface
{
return $this->adapter ??= new Adapter(
$this->getQueueProvider(),
new JsonMessageSerializer(),
new MessageSerializer(new JsonMessageEncoder()),
$this->getLoop(),
);
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Cli\LoopInterface;
use Yiisoft\Queue\Message\JsonMessageSerializer;
use Yiisoft\Queue\Message\Message;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\Message\MessageSerializerInterface;
use Yiisoft\Queue\Message\Serializer\JsonMessageEncoder;
use Yiisoft\Queue\Message\Serializer\MessageSerializer;
use Yiisoft\Queue\Message\Serializer\MessageSerializerInterface;
use Yiisoft\Queue\Redis\Adapter;
use Yiisoft\Queue\Redis\QueueProviderInterface;

Expand Down Expand Up @@ -37,7 +38,7 @@ public function testAdapterNullMessage()

$adapter = new Adapter(
$provider,
new JsonMessageSerializer(),
new MessageSerializer(new JsonMessageEncoder()),
$mockLoop,
);
$notUseHandler = true;
Expand Down
Loading