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
spiralbot committed Apr 25, 2024
1 parent 8952733 commit 58da6cc
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Task.php
Original file line number Diff line number Diff line change
@@ -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/TaskInterface.php
Original file line number Diff line number Diff line change
@@ -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 tests/TaskTest.php
Original file line number Diff line number Diff line change
@@ -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 58da6cc

Please sign in to comment.