Skip to content

Commit 755d3ab

Browse files
committed
Dto Docs
1 parent a8ba128 commit 755d3ab

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

docs/sections/misc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Logging
44

5-
By default errors are always logged, and with log enabled also the execution of a job.
5+
By default, errors are always logged, and with log enabled also the execution of a job.
66
Make sure you add this to your config:
77
```php
88
'Log' => [

docs/sections/tips.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,37 @@ Do not run this with production ones, though.
119119

120120
The console kill commands are also registered here. So if you run a worker locally,
121121
and you enter `Ctrl+C` or alike, it will also hard-kill this worker process.
122+
123+
## Use DTOs
124+
Using [CakeDto](https://github.com/dereuromark/cakephp-dto) plugin you can make your code much more reliable, testable
125+
and developer-friendly.
126+
127+
Set up a DTO per task in your `dto.xml`, e.g.
128+
```xml
129+
<dto name="OrderUpdateNotificationQueueData" immutable="true">
130+
<field name="orderId" type="int" required="true"/>
131+
<field name="type" type="string" required="true"/>
132+
...
133+
</dto>
134+
```
135+
Instead of a plain array you can now rely on a clean API for input:
136+
```php
137+
$data = OrderUpdateNotificationQueueDataDto::createFromArray([
138+
'orderId' => $order->id,
139+
'type' => 'orderConfirmationToCustomer',
140+
])->toArray();
141+
$this->getTableLocator()->get('Queue.QueuedJobs')->createJob('OrderUpdateNotification', $data);
142+
```
143+
Any of the fields not provided or defined will throw a clear exception.
144+
145+
Same then for the counterpart within the task:
146+
```php
147+
public function run(array $data, int $jobId): void {
148+
$queueData = OrderUpdateNotificationQueueDataDto::createFromArray($data);
149+
150+
$order = $this->fetchTable('Orders')->get($queueData->getOrderId(), contain: ['OrderItems']);
151+
$this->getMailer('OrderConfirmation')->send($queueData->getType(), [$order]);
152+
}
153+
```
154+
155+
PHPStan together with tests can now fully monitor and assert necessary data.

0 commit comments

Comments
 (0)