@@ -119,3 +119,37 @@ Do not run this with production ones, though.
119119
120120The console kill commands are also registered here. So if you run a worker locally,
121121and 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