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

daycry / queues / 11215608412

13 Aug 2024 10:37AM UTC coverage: 78.701%. Remained the same
11215608412

push

github

daycry
Fix: upgrade

7 of 8 new or added lines in 2 files covered. (87.5%)

2 existing lines in 1 file now uncovered.

303 of 385 relevant lines covered (78.7%)

7.78 hits per line

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

94.55
/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\Queues;
15

16
use CodeIgniter\I18n\Time;
17
use DateTime;
18
use DateTimeZone;
19
use Daycry\Queues\Exceptions\JobException;
20
use Daycry\Queues\Traits\CallableTrait;
21
use Daycry\Queues\Traits\EnqueuableTrait;
22
use Daycry\Queues\Traits\ExecutableTrait;
23
use JsonSerializable;
24

25
class Job implements JsonSerializable
26
{
27
    use EnqueuableTrait;
28
    use ExecutableTrait;
29
    use CallableTrait;
30

31
    private array $types          = [];
32
    protected ?string $type       = null;
33
    protected mixed $action       = null;
34
    protected ?DateTime $schedule = null;
35

36
    public function __construct(?object $data = null)
37
    {
38
        $this->types = service('settings')->get('Queue.jobTypes');
38✔
39

40
        $this->checkWorker();
38✔
41

42
        if ($data) {
37✔
43
            foreach ($data as $attribute => $value) {
23✔
44
                if (property_exists($this, $attribute)) {
23✔
45
                    if ($attribute === 'schedule') {
23✔
46
                        if ($value && $value instanceof object) {
23✔
UNCOV
47
                            $this->{$attribute} = new DateTime($value->date, new DateTimeZone($value->timezone));
×
48
                        }
49
                    } else {
50
                        $this->{$attribute} = $value;
23✔
51
                    }
52
                }
53
            }
54

55
            if ($this->type && ! in_array($this->type, $this->types, true)) {
23✔
56
                throw JobException::forInvalidTaskType($this->type);
1✔
57
            }
58
        }
59
    }
60

61
    /**
62
     * Returns the type.
63
     */
64
    public function getType(): string
65
    {
66
        return $this->type;
6✔
67
    }
68

69
    /**
70
     * Returns the saved action.
71
     *
72
     * @return mixed
73
     */
74
    public function getAction()
75
    {
76
        return $this->action;
22✔
77
    }
78

79
    public function command(string $command, array|object $options = []): Job
80
    {
81
        $this->type = 'command';
7✔
82
        $options    = $this->_prepareJobOptions($options);
7✔
83

84
        $this->action = json_decode(json_encode(['command' => $command, 'options' => $options]));
7✔
85

86
        return $this;
7✔
87
    }
88

89
    public function shell(string $command, array|object $options = []): Job
90
    {
91
        $this->type = 'shell';
8✔
92
        $options    = $this->_prepareJobOptions($options);
8✔
93

94
        $this->action = json_decode(json_encode(['command' => $command, 'options' => $options]));
8✔
95

96
        return $this;
8✔
97
    }
98

99
    /**
100
     * @param string $name Name of the event to trigger
101
     */
102
    public function event(string $name, array|object $options = []): Job
103
    {
104
        $this->type = 'event';
5✔
105
        $options    = $this->_prepareJobOptions($options);
5✔
106

107
        $this->action = json_decode(json_encode(['event' => $name, 'options' => $options]));
5✔
108

109
        return $this;
5✔
110
    }
111

112
    /**
113
     * @param array $options
114
     */
115
    public function url(string $url, array|object $options = []): Job
116
    {
117
        $data    = [];
8✔
118
        $options = $this->_prepareJobOptions($options);
8✔
119

120
        $data         = array_merge(['url' => $url], $options);
8✔
121
        $this->type   = 'url';
8✔
122
        $this->action = json_decode(json_encode($data));
8✔
123

124
        return $this;
8✔
125
    }
126

127
    /**
128
     * @param array $options
129
     */
130
    public function classes(string $class, string $method, array|object $options = []): Job
131
    {
132
        $data            = [];
5✔
133
        $data['class']   = $class;
5✔
134
        $data['method']  = $method;
5✔
135
        $data['options'] = $options;
5✔
136

137
        $this->type   = 'classes';
5✔
138
        $this->action = json_decode(json_encode($data));
5✔
139

140
        return $this;
5✔
141
    }
142

143
    public function scheduled(DateTime|Time $schedule)
144
    {
145
        if ($schedule instanceof Time) {
4✔
UNCOV
146
            $schedule = $schedule->toDateTime();
×
147
        }
148

149
        $this->schedule = $schedule;
4✔
150

151
        return $this;
4✔
152
    }
153

154
    public function toObject(): object
155
    {
156
        $data = get_object_vars($this);
29✔
157

158
        unset($data['types'], $data['worker']);
29✔
159

160
        $data = json_decode(json_encode($data));
29✔
161

162
        if (isset($data->schedule->date)) {
29✔
163
            $data->schedule = new DateTime($data->schedule->date, new DateTimeZone($data->schedule->timezone));
4✔
164
        } else {
165
            $data->schedule = null;
25✔
166
        }
167

168
        return $data;
29✔
169
    }
170

171
    public function jsonSerialize(): mixed
172
    {
NEW
173
        return $this->format('c');
×
174
    }
175

176
    private function _prepareJobOptions(array|object $options = [])
177
    {
178
        if ($options) {
28✔
179
            if (! is_array($options)) {
10✔
180
                $options = json_decode(json_encode($options), true);
3✔
181
            }
182

183
            return $options;
10✔
184
        }
185

186
        return [];
18✔
187
    }
188
}
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

© 2025 Coveralls, Inc