• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / jobs / 24850441053

23 Apr 2026 05:54PM UTC coverage: 52.404% (-1.5%) from 53.938%
24850441053

push

github

daycry
Fixes

104 of 219 new or added lines in 42 files covered. (47.49%)

14 existing lines in 9 files now uncovered.

1210 of 2309 relevant lines covered (52.4%)

4.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.24
/src/Traits/CallbackTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Queues.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Jobs\Traits;
15

16
use Daycry\Jobs\Job;
17
use stdClass;
18

19
/**
20
 * Defines a post-execution callback Job descriptor that will be dispatched
21
 * (inline or enqueued) after the parent Job finishes.
22
 *
23
 * Legacy setCallback(url, options) removed in favor of setCallbackJob builder.
24
 */
25
trait CallbackTrait
26
{
27
    /**
28
     * Structured descriptor holding builder closure and inheritance options.
29
     *
30
     * @var object{builder:callable,inherit:array,filter:string,allowChain:bool}|null
31
     */
32
    protected ?object $callbackDescriptor = null;
33

34
    /**
35
     * Register a callback job builder.
36
     * The builder receives the parent Job and must return a configured child Job (without push()).
37
     * Inheritance options:
38
     *  - inherit: list of fields from parent to inject into child payload meta (output,error,attempts,name,source)
39
     *  - filter: 'always'|'success'|'failure'
40
     *  - allowChain: whether a callback job can itself define another callback (default false)
41
     */
42
    public function setCallbackJob(callable $builder, array $options = []): self
43
    {
44
        $descriptor          = new stdClass();
9✔
45
        $descriptor->builder = $builder;
9✔
46
        $descriptor->inherit = $options['inherit'] ?? ['output', 'error'];
9✔
47
        // Accept both 'filter' or shorthand 'on'
48
        $rawFilter = $options['filter'] ?? $options['on'] ?? 'always';
9✔
49
        // Normalize synonyms: 'error' -> 'failure'
50
        $normalized = match ($rawFilter) {
9✔
51
            'error'   => 'failure',
1✔
52
            'failure' => 'failure',
1✔
53
            'success' => 'success',
1✔
54
            default   => 'always',
6✔
55
        };
9✔
56
        $descriptor->filter       = $normalized;
9✔
57
        $descriptor->allowChain   = (bool) ($options['allowChain'] ?? false);
9✔
58
        $this->callbackDescriptor = $descriptor;
9✔
59

60
        return $this;
9✔
61
    }
62

63
    /**
64
     * Fluent API: Chain multiple jobs to execute sequentially.
65
     *
66
     * @param list<Job> $jobs Array of jobs to chain
67
     */
68
    public function chain(array $jobs): self
69
    {
NEW
70
        if ($jobs === []) {
×
71
            return $this;
×
72
        }
73

74
        // Build chain from last to first
75
        $previousJob = null;
×
76

77
        foreach (array_reverse($jobs) as $job) {
×
78
            if ($previousJob) {
×
79
                $job->then($previousJob);
×
80
            }
81
            $previousJob = $job;
×
82
        }
83

84
        // Set first job as callback of current job
85
        $this->then($jobs[0]);
×
86

87
        return $this;
×
88
    }
89

90
    /**
91
     * Fluent API: Execute job after successful completion.
92
     */
93
    public function then(Job $nextJob): self
94
    {
95
        return $this->setCallbackJob(
×
96
            static fn ($parent) => $nextJob,
×
97
            ['filter' => 'success', 'allowChain' => true],
×
98
        );
×
99
    }
100

101
    /**
102
     * Fluent API: Execute job on failure.
103
     */
104
    public function catch(Job $failureJob): self
105
    {
106
        return $this->setCallbackJob(
×
107
            static fn ($parent) => $failureJob,
×
108
            ['filter' => 'failure', 'allowChain' => false],
×
109
        );
×
110
    }
111

112
    /**
113
     * Fluent API: Execute job regardless of outcome.
114
     */
115
    public function finally(Job $finallyJob): self
116
    {
117
        return $this->setCallbackJob(
×
118
            static fn ($parent) => $finallyJob,
×
119
            ['filter' => 'always', 'allowChain' => false],
×
120
        );
×
121
    }
122

123
    public function hasCallbackJob(): bool
124
    {
125
        return $this->callbackDescriptor !== null;
28✔
126
    }
127

128
    public function getCallbackDescriptor(): ?object
129
    {
130
        return $this->callbackDescriptor;
9✔
131
    }
132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc