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

daycry / jobs / 24568210769

05 Apr 2026 08:48AM UTC coverage: 53.938% (-2.2%) from 56.164%
24568210769

push

github

daycry
Optimize

50 of 192 new or added lines in 14 files covered. (26.04%)

3 existing lines in 3 files now uncovered.

1219 of 2260 relevant lines covered (53.94%)

4.42 hits per line

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

80.85
/src/Job.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;
15

16
use DateTime;
17
use DateTimeZone;
18
use Daycry\Jobs\Traits\ActivityTrait;
19
use Daycry\Jobs\Traits\CallbackTrait;
20
use Daycry\Jobs\Traits\EnqueuableTrait;
21
use Daycry\Jobs\Traits\EnvironmentTrait;
22
use Daycry\Jobs\Traits\FrequenciesTrait;
23
use Daycry\Jobs\Traits\IdentityTrait;
24
use Daycry\Jobs\Traits\StateTrait;
25
use Throwable;
26

27
/**
28
 * Core Job definition aggregating scheduling traits, queue metadata, naming,
29
 * environment constraints, retry limits, notification flags and more.
30
 *
31
 * This class is a mutable builder used prior to execution. Execution runtime
32
 * state/results are captured separately by immutable DTOs (ExecutionResult, LifecycleOutcome).
33
 *
34
 * Properties (selected):
35
 *  - job: handler key referencing an entry in config('Jobs')->jobs
36
 *  - payload: mixed user data passed to the handler
37
 *  - singleInstance: whether only one concurrent execution is allowed
38
 *  - schedule: optional DateTime for queue delayed execution
39
 *  - attempts: internal retry attempt counter for queue contexts
40
 */
41
class Job
42
{
43
    use EnvironmentTrait;
44
    use FrequenciesTrait;
45
    use IdentityTrait;
46
    use ActivityTrait;
47
    use StateTrait;
48
    use EnqueuableTrait;
49
    use CallbackTrait;
50

51
    protected string $job;
52
    protected mixed $payload;
53
    protected bool $singleInstance = false;
54

55
    /**
56
     * Origin of the job: 'cron' when created via Scheduler, 'queue' when pushed directly,
57
     * or custom (e.g. 'api') if set by integrator. Propagated into logs for observability.
58
     */
59
    protected ?string $source = null;
60

61
    /**
62
     * True when this job was generated as a callback child of another job.
63
     */
64
    protected bool $isCallbackChild = false;
65

66
    /**
67
     * Whether this callback child is permitted to trigger its own callback chain.
68
     */
69
    protected bool $callbackChainAllowed = false;
70

71
    /**
72
     * Middleware callables to wrap job execution.
73
     * Each middleware receives (Job $job, callable $next) and must call $next($job) to proceed.
74
     *
75
     * @var array<callable>
76
     */
77
    protected array $middleware = [];
78

79
    public function __construct(...$params)
80
    {
81
        $this->job     = $params['job'] ?? '';
90✔
82
        $this->payload = $params['payload'] ?? '';
90✔
83
    }
84

85
    public function getJob(): string
86
    {
87
        return $this->job;
46✔
88
    }
89

90
    public function getPayload(): mixed
91
    {
92
        return $this->payload;
36✔
93
    }
94

95
    /**
96
     * Internal helper allowing lifecycle components to adjust payload (e.g., injecting meta for callback jobs).
97
     */
98
    public function setPayload(mixed $payload): self
99
    {
100
        $this->payload = $payload;
8✔
101

102
        return $this;
8✔
103
    }
104

105
    public function singleInstance(bool $singleInstance = true): self
106
    {
107
        $this->singleInstance = $singleInstance;
58✔
108

109
        return $this;
58✔
110
    }
111

112
    public function isSingleInstance(): bool
113
    {
114
        return $this->singleInstance;
30✔
115
    }
116

117
    public function toObject(): object
118
    {
119
        $data = get_object_vars($this);
20✔
120

121
        unset($data['types'], $data['worker'], $data['middleware']);
20✔
122

123
        $data = json_decode(json_encode($data));
20✔
124

125
        if (isset($data->schedule->date)) {
20✔
126
            $data->schedule = new DateTime($data->schedule->date, new DateTimeZone($data->schedule->timezone));
6✔
127
        } else {
128
            $data->schedule = null;
15✔
129
        }
130

131
        return $data;
20✔
132
    }
133

134
    /**
135
     * Define origin source for logging/analytics.
136
     */
137
    public function source(string $source): self
138
    {
139
        $this->source = $source;
26✔
140

141
        return $this;
26✔
142
    }
143

144
    public function getSource(): ?string
145
    {
146
        return $this->source;
23✔
147
    }
148

149
    public function markAsCallbackChild(bool $allowChain = false): self
150
    {
151
        $this->isCallbackChild      = true;
8✔
152
        $this->callbackChainAllowed = $allowChain;
8✔
153

154
        return $this;
8✔
155
    }
156

157
    public function isCallbackChild(): bool
158
    {
159
        return $this->isCallbackChild;
×
160
    }
161

162
    public function isCallbackChainAllowed(): bool
163
    {
164
        return $this->callbackChainAllowed;
×
165
    }
166

167
    /**
168
     * Register middleware callables to wrap job execution.
169
     * Each middleware receives (Job $job, callable $next) and must call $next($job) to proceed.
170
     */
171
    public function middleware(callable ...$fns): self
172
    {
NEW
173
        $this->middleware = array_merge($this->middleware, $fns);
×
174

NEW
175
        return $this;
×
176
    }
177

178
    /**
179
     * @return array<callable>
180
     */
181
    public function getMiddleware(): array
182
    {
183
        return $this->middleware;
28✔
184
    }
185

186
    /**
187
     * Rebuild a Job instance from a queue record object.
188
     * The $record must at minimum provide 'job' and 'payload'. Optional fields restored:
189
     *  name, queue, priority, schedule (DateTime or serialized), attempts.
190
     */
191
    public static function fromQueueRecord(object $record): self
192
    {
193
        $instance = new self(job: $record->job ?? '', payload: $record->payload ?? null);
3✔
194

195
        // Restaurar propiedades conocidas si existen
196
        if (isset($record->name)) {
3✔
197
            $instance->named($record->name);
1✔
198
        }
199
        if (isset($record->queue)) {
3✔
200
            $instance->setQueue($record->queue);
2✔
201
        }
202
        if (isset($record->priority)) {
3✔
203
            try {
204
                $instance->priority((int) $record->priority);
2✔
205
            } catch (Throwable) { // ignore invalid
×
206
            }
207
        }
208
        if (isset($record->schedule) && $record->schedule) {
3✔
209
            try {
210
                $dt = new DateTime($record->schedule->date ?? $record->schedule, new DateTimeZone($record->schedule->timezone ?? date_default_timezone_get()));
2✔
211
                $instance->scheduled($dt);
2✔
212
            } catch (Throwable) {
×
213
                // ignorar si no se puede parsear
214
            }
215
        }
216
        // Attempts (si estuviera presente en versiones futuras)
217
        if (isset($record->attempts) && is_numeric($record->attempts)) {
3✔
218
            // Forzar el contador interno sumando tantas veces como sea necesario
219
            for ($i = 0; $i < (int) $record->attempts; $i++) {
2✔
220
                $instance->addAttempt();
×
221
            }
222
        }
223

224
        // Restore maxRetries configuration
225
        if (isset($record->maxRetries) && is_numeric($record->maxRetries)) {
3✔
226
            $instance->maxRetries((int) $record->maxRetries);
×
227
        }
228

229
        // Restore callback flags if present (for enqueued callback children)
230
        if (isset($record->isCallbackChild) && $record->isCallbackChild) {
3✔
231
            $instance->markAsCallbackChild((bool) ($record->callbackChainAllowed ?? false));
×
232
        }
233

234
        return $instance;
3✔
235
    }
236
}
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