Skip to content

Commit

Permalink
Merge pull request #1097: [spiral/queue] Add TaskInterface and Task f…
Browse files Browse the repository at this point in the history
…or the Queue component
  • Loading branch information
roxblnfk committed Apr 25, 2024
2 parents 8a05712 + 31c0458 commit 9857b74
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# CHANGELOG

## Unreleased

- **Other Features**
- [spiral/queue] Added `Spiral\Queue\TaskInterface` and `Spiral\Queue\Task` which will contain all the necessary data
for job processing.

## 3.12.0 - 2024-02-29

- **Medium Impact Changes**
Expand Down
61 changes: 61 additions & 0 deletions src/Queue/src/Task.php
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Spiral\Queue;

final class Task implements TaskInterface
{
/**
* @param non-empty-string $id Unique identifier of the task in the queue.
* @param non-empty-string $queue broker queue name.
* @param non-empty-string $name name of the task/job.
* @param mixed $payload payload of the task/job.
* @param array<non-empty-string, array<string>> $headers headers of the task/job.
*/
public function __construct(
private readonly string $id,
private readonly string $queue,
private readonly string $name,
private readonly mixed $payload,
private readonly array $headers,
) {
}

public function getPayload(): mixed
{
return $this->payload;
}

/**
* @return non-empty-string
*/
public function getName(): string
{
return $this->name;
}

/**
* @return array<non-empty-string, array<string>>
*/
public function getHeaders(): array
{
return $this->headers;
}

/**
* @return non-empty-string
*/
public function getQueue(): string
{
return $this->queue;
}

/**
* @return non-empty-string
*/
public function getId(): string
{
return $this->id;
}
}
41 changes: 41 additions & 0 deletions src/Queue/src/TaskInterface.php
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Spiral\Queue;

interface TaskInterface
{
/**
* Returns payload of the task/job.
*/
public function getPayload(): mixed;

/**
* Returns the name of the task/job.
*
* @return non-empty-string
*/
public function getName(): string;

/**
* Returns headers of the task/job.
*
* @return array<non-empty-string, array<string>>
*/
public function getHeaders(): array;

/**
* Returns the name of the queue.
*
* @return non-empty-string
*/
public function getQueue(): string;

/**
* Returns the unique identifier of the task in the queue.
*
* @return non-empty-string
*/
public function getId(): string;
}
43 changes: 43 additions & 0 deletions src/Queue/tests/TaskTest.php
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Queue;

use Spiral\Queue\Task;

final class TaskTest extends TestCase
{
public function testGetPayload(): void
{
$task = new Task('some-id', 'some-queue', 'some-name', ['key' => 'value'], ['header' => ['value']]);
$this->assertSame(['key' => 'value'], $task->getPayload());

$task = new Task('some-id', 'some-queue', 'some-name', 'string-payload', ['header' => ['value']]);
$this->assertSame('string-payload', $task->getPayload());
}

public function testGetName(): void
{
$task = new Task('some-id', 'some-queue', 'some-name', ['key' => 'value'], ['header' => ['value']]);
$this->assertSame('some-name', $task->getName());
}

public function testGetHeaders(): void
{
$task = new Task('some-id', 'some-queue', 'some-name', ['key' => 'value'], ['header' => ['value']]);
$this->assertSame(['header' => ['value']], $task->getHeaders());
}

public function testGetQueue(): void
{
$task = new Task('some-id', 'some-queue', 'some-name', ['key' => 'value'], ['header' => ['value']]);
$this->assertSame('some-queue', $task->getQueue());
}

public function testGetId(): void
{
$task = new Task('some-id', 'some-queue', 'some-name', ['key' => 'value'], ['header' => ['value']]);
$this->assertSame('some-id', $task->getId());
}
}

0 comments on commit 9857b74

Please sign in to comment.