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

daycry / queues / 8122678457

04 Jan 2024 12:26PM UTC coverage: 80.811% (+0.3%) from 80.541%
8122678457

push

github

daycry
Fix: schedule propterty when worker is sync

3 of 4 new or added lines in 1 file covered. (75.0%)

299 of 370 relevant lines covered (80.81%)

8.24 hits per line

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

96.08
/src/Job.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Daycry\Queues;
6

7
use CodeIgniter\I18n\Time;
8
use DateTime;
9
use DateTimeZone;
10
use Daycry\Queues\Exceptions\JobException;
11
use Daycry\Queues\Traits\CallableTrait;
12
use Daycry\Queues\Traits\EnqueuableTrait;
13
use Daycry\Queues\Traits\ExecutableTrait;
14

15
class Job
16
{
17
    use EnqueuableTrait;
18
    use ExecutableTrait;
19
    use CallableTrait;
20

21
    private array $types = [];
22

23
    protected ?string $type = null;
24

25
    protected mixed $action = null;
26

27
    protected ?DateTime $schedule = null;
28

29
    public function __construct(?object $data = null)
30
    {
31
        $this->types = service('settings')->get('Queue.jobTypes');
48✔
32

33
        $this->checkWorker();
48✔
34

35
        if($data) {
47✔
36
            foreach($data as $attribute => $value) {
23✔
37
                if(property_exists($this, $attribute)) {
23✔
38
                    if($attribute == 'schedule') {
23✔
39
                        if($value && $value instanceof object) {
23✔
NEW
40
                            $this->{$attribute} = new DateTime($value->date, new DateTimeZone($value->timezone));
×
41
                        }
42
                    } else {
43
                        $this->{$attribute} = $value;
23✔
44
                    }
45
                }
46
            }
47

48
            if ($this->type && !in_array($this->type, $this->types, true)) {
23✔
49
                throw JobException::forInvalidTaskType($this->type);
1✔
50
            }
51
        }
52
    }
53

54
    /**
55
     * Returns the type.
56
     *
57
     * @return string
58
     */
59
    public function getType(): string
60
    {
61
        return $this->type;
6✔
62
    }
63

64
    /**
65
     * Returns the saved action.
66
     *
67
     * @return mixed
68
     */
69
    public function getAction()
70
    {
71
        return $this->action;
27✔
72
    }
73

74
    /**
75
     * @param string $command
76
     */
77
    public function command(string $command, array|object $options = []): Job
78
    {
79
        $this->type = 'command';
7✔
80
        $options = $this->_prepareJobOptions($options);
7✔
81

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

84
        return $this;
7✔
85
    }
86

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

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

97
        return $this;
8✔
98
    }
99

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

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

110
        return $this;
5✔
111
    }
112

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

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

126
        return $this;
8✔
127
    }
128

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

141
        $this->type = 'classes';
5✔
142
        $this->action = json_decode(json_encode($data));
5✔
143

144
        return $this;
5✔
145
    }
146

147
    public function scheduled(DateTime|Time $schedule)
148
    {
149
        if($schedule instanceof Time) {
4✔
150
            $schedule = $schedule->toDateTime();
×
151
        }
152

153
        $this->schedule = $schedule;
4✔
154

155
        return $this;
4✔
156
    }
157

158
    public function toObject(): object
159
    {
160
        $data = get_object_vars($this);
29✔
161
        unset($data['types'], $data['worker']);
29✔
162
        $data = json_decode(json_encode($data));
29✔
163

164
        return $data;
29✔
165
    }
166

167
    private function _prepareJobOptions(array|object $options = [])
168
    {
169
        if($options) {
28✔
170
            if(!is_array($options)) {
10✔
171
                $options = json_decode(json_encode($options), true);
3✔
172
            }
173

174
            return $options;
10✔
175
        }
176

177
        return [];
18✔
178
    }
179
}
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