• 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

89.29
/src/Config/Services.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\Config;
15

16
use CodeIgniter\I18n\Time;
17
use Config\Services as BaseServices;
18
use DateTime;
19
use DateTimeInterface;
20
use Daycry\Jobs\Cronjob\Scheduler;
21
use Daycry\Jobs\Exceptions\JobException;
22
use Daycry\Jobs\Job;
23

24
class Services extends BaseServices
25
{
26
    public static function scheduler(bool $getShared = true): Scheduler
27
    {
28
        if ($getShared) {
57✔
29
            $shared = static::getSharedInstance('scheduler');
57✔
30
            if ($shared instanceof Scheduler) {
57✔
31
                return $shared;
57✔
32
            }
33
        }
34

35
        return new Scheduler();
57✔
36
    }
37

38
    /**
39
     * Convenience helper to create and immediately enqueue a Job directly
40
     * through the configured worker, bypassing the Scheduler (cron) layer.
41
     *
42
     * Typical usage:
43
     *  $id = \Daycry\Jobs\Config\Services::queueJob('command', 'jobs:test', null, function(Job $job) {
44
     *      $job->named('on_demand')->priority(3)->singleInstance();
45
     *  });
46
     *
47
     * The optional $configure callback receives the mutable Job builder so the
48
     * caller can chain methods (named(), priority(), scheduled(), etc.) before
49
     * it is pushed to the underlying queue worker. The Job's source will be
50
     * automatically marked as 'queue' (handled inside push()).
51
     *
52
     * @param string        $job       Handler key (must exist in config('Jobs')->jobs)
53
     * @param mixed         $payload   Arbitrary payload for the handler
54
     * @param string|null   $queue     Optional explicit queue name; if null first configured queue is used
55
     * @param callable|null $configure function(Job $job): void — mutate Job before enqueue
56
     *
57
     * @return string Queue identifier returned by the worker implementation
58
     *
59
     * @throws JobException If the handler key is invalid
60
     */
61
    public static function queueJob(
62
        string $job,
63
        mixed $payload = null,
64
        ?string $queue = null,
65
        ?callable $configure = null,
66
        DateTimeInterface|int|string|Time|null $when = null,
67
    ): string {
68
        $cfg = config('Jobs');
5✔
69
        if (! isset($cfg->jobs[$job])) {
5✔
70
            throw JobException::forInvalidJob($job);
1✔
71
        }
72

73
        $instance = new Job(job: $job, payload: $payload);
4✔
74

75
        if ($configure) {
4✔
76
            $configure($instance);
2✔
77
        }
78

79
        // Normalize scheduling time if provided
80
        if ($when !== null) {
4✔
81
            $dt = null;
2✔
82
            if ($when instanceof DateTime) {
2✔
83
                $dt = $when;
×
84
            } elseif ($when instanceof DateTimeInterface) {
2✔
NEW
85
                $dt = DateTime::createFromInterface($when);
×
86
            } elseif ($when instanceof Time) {
2✔
87
                $dt = $when->toDateTime();
×
88
            } elseif (is_int($when)) {
2✔
89
                $dt = (new DateTime())->modify('+' . $when . ' seconds');
1✔
90
            } elseif (is_string($when)) {
1✔
91
                $dt = new DateTime($when);
1✔
92
            }
93
            if ($dt instanceof DateTime) {
2✔
94
                $instance->scheduled($dt);
2✔
95
            }
96
        }
97

98
        // Only set queue if not already configured by $configure callback
99
        if ($instance->getQueue() === null) {
4✔
100
            $instance->enqueue($queue);
4✔
101
        }
102

103
        return (string) $instance->push();
4✔
104
    }
105
}
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