Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.47% |
13 / 17 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| QueueableJobsTrait | |
76.47% |
13 / 17 |
|
33.33% |
1 / 3 |
5.33 | |
0.00% |
0 / 1 |
| queueJob | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| queueJobs | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| queueDelayedJob | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Model\Table; |
| 5 | |
| 6 | use Cake\Queue\QueueManager; |
| 7 | |
| 8 | /** |
| 9 | * QueueableJobsTrait |
| 10 | * |
| 11 | * Provides job queuing functionality for Table classes. |
| 12 | * This trait consolidates duplicate job queuing logic that was |
| 13 | * previously scattered across multiple table classes. |
| 14 | */ |
| 15 | trait QueueableJobsTrait |
| 16 | { |
| 17 | /** |
| 18 | * Queues a job with the provided job class and data |
| 19 | * |
| 20 | * @param string $job The fully qualified job class name |
| 21 | * @param array<string, mixed> $data The data to be passed to the job |
| 22 | * @param array<string, mixed> $options Additional options for the job (delay, queue name, etc.) |
| 23 | * @return void |
| 24 | */ |
| 25 | public function queueJob(string $job, array $data, array $options = []): void |
| 26 | { |
| 27 | // Set default queue config |
| 28 | $defaultOptions = ['config' => 'default']; |
| 29 | $options = array_merge($defaultOptions, $options); |
| 30 | |
| 31 | QueueManager::push($job, $data, $options); |
| 32 | |
| 33 | $this->log( |
| 34 | sprintf( |
| 35 | 'Queued a %s with data: %s%s', |
| 36 | $job, |
| 37 | json_encode($data), |
| 38 | !empty($options['delay']) ? sprintf(' (delayed by %d seconds)', $options['delay']) : '', |
| 39 | ), |
| 40 | 'info', |
| 41 | ['group_name' => $job], |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Queue multiple jobs at once |
| 47 | * |
| 48 | * @param array<array{job: string, data: array, options?: array}> $jobs Array of job configurations |
| 49 | * @return void |
| 50 | */ |
| 51 | public function queueJobs(array $jobs): void |
| 52 | { |
| 53 | foreach ($jobs as $jobConfig) { |
| 54 | $options = $jobConfig['options'] ?? []; |
| 55 | $this->queueJob($jobConfig['job'], $jobConfig['data'], $options); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Queue a job with a delay |
| 61 | * |
| 62 | * @param string $job The fully qualified job class name |
| 63 | * @param array<string, mixed> $data The data to be passed to the job |
| 64 | * @param int $delaySeconds Delay in seconds |
| 65 | * @return void |
| 66 | */ |
| 67 | public function queueDelayedJob(string $job, array $data, int $delaySeconds): void |
| 68 | { |
| 69 | $this->queueJob($job, $data, ['delay' => $delaySeconds]); |
| 70 | } |
| 71 | } |