Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1097: [spiral/queue] Add TaskInterface and Task f…
…or the Queue component
- Loading branch information
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |