Skip to content

Commit 722bafb

Browse files
authored
Support DTO like input. (dereuromark#413)
* Support DTO like input.
1 parent ff47bfb commit 722bafb

File tree

4 files changed

+99
-4
lines changed

4 files changed

+99
-4
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"cakephp/migrations": "^4.0.1",
3737
"dereuromark/cakephp-ide-helper": "^2.0.0",
3838
"dereuromark/cakephp-tools": "^3.0.0",
39+
"dereuromark/cakephp-dto": "^2.1.0",
3940
"fig-r/psr2r-sniffer": "dev-next",
4041
"friendsofcake/search": "^7.0.0",
4142
"phpunit/phpunit": "^10.1"

src/Model/Table/QueuedJobsTable.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Cake\ORM\Query\SelectQuery;
1414
use Cake\ORM\Table;
1515
use Cake\Validation\Validator;
16+
use CakeDto\Dto\FromArrayToArrayInterface;
1617
use InvalidArgumentException;
1718
use Queue\Config\JobConfig;
1819
use Queue\Model\Entity\QueuedJob;
@@ -217,18 +218,28 @@ public function createConfig(): JobConfig {
217218
* - notBefore: Optional date which must not be preceded
218219
* - group: Used to group similar QueuedJobs
219220
* - reference: An optional reference string
221+
* - status: To set an initial status text
220222
*
221-
* @param string $jobTask Job task name or FQCN
222-
* @param array<string, mixed>|null $data Array of data
223-
* @param \Queue\Config\JobConfig|array<string, mixed> $config Config to save along with the job
223+
* @param string $jobTask Job task name or FQCN.
224+
* @param object|array<string, mixed>|null $data Array of data or DTO like object.
225+
* @param \Queue\Config\JobConfig|array<string, mixed> $config Config to save along with the job.
224226
*
225227
* @return \Queue\Model\Entity\QueuedJob Saved job entity
226228
*/
227-
public function createJob(string $jobTask, ?array $data = null, array|JobConfig $config = []): QueuedJob {
229+
public function createJob(string $jobTask, array|object|null $data = null, array|JobConfig $config = []): QueuedJob {
228230
if (!$config instanceof JobConfig) {
229231
$config = $this->createConfig()->fromArray($config);
230232
}
231233

234+
if ($data instanceof FromArrayToArrayInterface) {
235+
$data = $data->toArray();
236+
} elseif (is_object($data) && method_exists($data, 'toArray')) {
237+
$data = $data->toArray();
238+
}
239+
if ($data !== null && !is_array($data)) {
240+
throw new InvalidArgumentException('Data must be `array|null`, implement `' . FromArrayToArrayInterface::class . '` or provide a `toArray()` method');
241+
}
242+
232243
$queuedJob = [
233244
'job_task' => $this->jobTask($jobTask),
234245
'data' => $data,

tests/TestCase/Model/Table/QueuedJobsTableTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cake\TestSuite\TestCase;
1717
use Queue\Model\Table\QueuedJobsTable;
1818
use Queue\Queue\Task\ExampleTask;
19+
use TestApp\Dto\MyTaskDto;
1920

2021
/**
2122
* Queue\Model\Table\QueuedJobsTable Test Case
@@ -223,6 +224,46 @@ public function testCreateWithSkipExistenceCheck() {
223224
$this->assertTrue((bool)$queuedJob);
224225
}
225226

227+
/**
228+
* @return void
229+
*/
230+
public function testCreateWithDto() {
231+
$array = [
232+
'some' => 'random',
233+
'test' => 'data',
234+
];
235+
$dto = new MyTaskDto($array);
236+
237+
$queuedJob = $this->QueuedJobs->createJob(ExampleTask::class, $dto);
238+
$this->assertTrue((bool)$queuedJob);
239+
$this->assertSame($array, $queuedJob->data);
240+
}
241+
242+
/**
243+
* @return void
244+
*/
245+
public function testCreateWithObject() {
246+
$array = [
247+
'some' => 'random',
248+
'test' => 'data',
249+
];
250+
$dto = new class() {
251+
/**
252+
* @return string[]
253+
*/
254+
public function toArray(): array {
255+
return [
256+
'some' => 'random',
257+
'test' => 'data',
258+
];
259+
}
260+
};
261+
262+
$queuedJob = $this->QueuedJobs->createJob(ExampleTask::class, $dto);
263+
$this->assertTrue((bool)$queuedJob);
264+
$this->assertSame($array, $queuedJob->data);
265+
}
266+
226267
/**
227268
* Test the delivery of jobs in sequence, skipping fetched but not completed tasks.
228269
*
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace TestApp\Dto;
5+
6+
use CakeDto\Dto\FromArrayToArrayInterface;
7+
8+
class MyTaskDto implements FromArrayToArrayInterface {
9+
10+
protected array $data;
11+
12+
/**
13+
* @param array $data
14+
*/
15+
public function __construct(array $data) {
16+
$this->data = $data;
17+
}
18+
19+
/**
20+
* @return string
21+
*/
22+
public function getFoo(): string {
23+
return 'foo';
24+
}
25+
26+
/**
27+
* @param array $array
28+
*
29+
* @return static
30+
*/
31+
public static function createFromArray(array $array): static {
32+
return new static($array);
33+
}
34+
35+
/**
36+
* @return array
37+
*/
38+
public function toArray(): array {
39+
return $this->data;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)