Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
heyjorgedev committed Jul 15, 2024
1 parent c70c721 commit 919984a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Contracts/Resources/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace HeyJorgeDev\QStash\Contracts\Resources;

use HeyJorgeDev\QStash\Responses\MessageEnqueueResponse;
use HeyJorgeDev\QStash\Responses\MessagePublishResponse;
use HeyJorgeDev\QStash\ValueObjects\Message;
use HeyJorgeDev\QStash\ValueObjects\MessageToPublish;
Expand All @@ -10,7 +11,7 @@ interface MessageInterface
{
public function publish(MessageToPublish $message): MessagePublishResponse;

public function enqueue();
public function enqueue(string $queueName, MessageToPublish $message): MessageEnqueueResponse;

public function batch();

Expand Down
25 changes: 23 additions & 2 deletions src/Resources/MessageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use HeyJorgeDev\QStash\Contracts\Resources\MessageInterface;
use HeyJorgeDev\QStash\Contracts\TransporterInterface;
use HeyJorgeDev\QStash\Exceptions\NotImplementedException;
use HeyJorgeDev\QStash\Responses\MessageEnqueueResponse;
use HeyJorgeDev\QStash\Responses\MessagePublishResponse;
use HeyJorgeDev\QStash\ValueObjects\Message;
use HeyJorgeDev\QStash\ValueObjects\MessageToPublish;
Expand Down Expand Up @@ -39,9 +40,29 @@ public function publish(MessageToPublish $message): MessagePublishResponse
);
}

public function enqueue()
public function enqueue(string $queueName, MessageToPublish $message): MessageEnqueueResponse
{
throw NotImplementedException::askForContributions('enqueue messages');
$upstashHeaders = $message->toUpstashHeaders();

$request = Request::POST("/publish/{$queueName}/{$message->destination->toString()}")
->withBody($message->body)
->appendHeaders($upstashHeaders);

$response = $this->transporter->send($request);

if (! $response->isSuccessful()) {
return new MessageEnqueueResponse(
$response->statusCode,
[],
$response->body
);
}

return new MessageEnqueueResponse(
$response->statusCode,
$response->body,
[]
);
}

public function batch()
Expand Down
33 changes: 33 additions & 0 deletions src/Responses/MessageEnqueueResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace HeyJorgeDev\QStash\Responses;

use HeyJorgeDev\QStash\Contracts\ResponseWithDataInterface;
use HeyJorgeDev\QStash\ValueObjects\Message;

class MessageEnqueueResponse implements ResponseWithDataInterface
{
public function __construct(
protected int $statusCode,
protected array $data,
protected array $errors,
) {}

public function isSuccessful(): bool
{
return $this->statusCode === 200;
}

public function getErrors(): array
{
return $this->errors;
}

public function getData(): Message
{
return new Message(
id: $this->data['messageId'],
deduplicated: $this->data['deduplicated'] ?? false,
);
}
}
5 changes: 4 additions & 1 deletion src/Responses/MessagePublishResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function getErrors(): array

public function getData(): Message
{
return new Message(id: $this->data['messageId']);
return new Message(
id: $this->data['messageId'],
deduplicated: $this->data['deduplicated'] ?? false,
);
}
}
1 change: 1 addition & 0 deletions src/ValueObjects/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Message
{
public function __construct(
public readonly string $id,
public readonly bool $deduplicated = false,
) {}

public static function to(Url|TopicName $url): MessageToPublish
Expand Down
2 changes: 1 addition & 1 deletion src/ValueObjects/Transporter/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function DELETE(string|Url $url): self
public function withMethod(string $method): self
{
return new self(
method: $method,
method: strtoupper($method),
url: $this->url,
headers: $this->headers,
body: $this->body,
Expand Down

0 comments on commit 919984a

Please sign in to comment.