Completed
Push — master ( 7d78ad...f6b571 )
by
unknown
37:39
created

JobContract::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SfCod\QueueBundle\Job;
4
5
use Exception;
6
use SfCod\QueueBundle\Base\InteractWithTimeTrait;
7
use SfCod\QueueBundle\Base\JobInterface;
8
use SfCod\QueueBundle\Base\JobResolverInterface;
9
use SfCod\QueueBundle\Entity\Job;
10
use SfCod\QueueBundle\Queue\QueueInterface;
11
12
/**
13
 * Class Job
14
 *
15
 * @author Alexey Orlov <[email protected]>
16
 * @author Virchenko Maksim <[email protected]>
17
 *
18
 * @package SfCod\QueueBundle\Base
19
 */
20
class JobContract implements JobContractInterface
21
{
22
    use InteractWithTimeTrait;
23
24
    /**
25
     * The job handler instance.
26
     *
27
     * @var JobInterface
28
     */
29
    protected $instance;
30
31
    /**
32
     * Indicates if the job has been deleted.
33
     *
34
     * @var bool
35
     */
36
    protected $deleted = false;
37
38
    /**
39
     * Indicates if the job has been released.
40
     *
41
     * @var bool
42
     */
43
    protected $released = false;
44
45
    /**
46
     * Indicates if the job has failed.
47
     *
48
     * @var bool
49
     */
50
    protected $failed = false;
51
52
    /**
53
     * The name of the connection the job belongs to.
54
     *
55
     * @var string
56
     */
57
    protected $connectionName;
58
59
    /**
60
     * The name of the queue the job belongs to.
61
     *
62
     * @var string
63
     */
64
    protected $queue;
65
66
    /**
67
     * Job resolver
68
     *
69
     * @var JobResolverInterface
70
     */
71
    protected $resolver;
72
73
    /**
74
     * The database queue instance.
75
     *
76
     * @var QueueInterface
77
     */
78
    protected $database;
79
80
    /**
81
     * The database job payload.
82
     *
83
     * @var Job
84
     */
85
    protected $job;
86
87
    /**
88
     * Create a new job instance.
89
     *
90
     * @param JobResolverInterface $resolver
91
     * @param QueueInterface $database
92
     * @param StdClass|MongoDB\Model\BSONDocument $job
93
     */
94
    public function __construct(JobResolverInterface $resolver, QueueInterface $database, Job $job)
95
    {
96
        $this->resolver = $resolver;
97
        $this->database = $database;
98
        $this->job = $job;
99
    }
100
101
    /**
102
     * Fire the job.
103
     *
104
     * @return void
105
     */
106
    public function fire()
107
    {
108
        $handler = $this->resolve($this->getName());
109
110
        $this->instance = $handler->fire($this, $this->getData());
111
    }
112
113
    /**
114
     * Delete the job from the queue.
115
     */
116
    public function delete()
117
    {
118
        $this->deleted = true;
119
120
        $this->database->deleteReserved($this->job->getQueue(), $this->getJobId());
121
    }
122
123
    /**
124
     * Process an exception that caused the job to fail.
125
     *
126
     * @param Exception $e
127
     *
128
     * @return void
129
     */
130
    public function failed($e)
131
    {
132
        $this->markAsFailed();
133
134
        if (method_exists($this->instance = $this->resolve($this->getName()), 'failed')) {
135
            $this->instance->failed($this->getData(), $e);
0 ignored issues
show
Bug introduced by
The method failed() does not seem to exist on object<SfCod\QueueBundle\Base\JobInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
        }
137
    }
138
139
    /**
140
     * Determine if the job has been deleted.
141
     *
142
     * @return bool
143
     */
144
    public function isDeleted(): bool
145
    {
146
        return $this->deleted;
147
    }
148
149
    /**
150
     * Release the job back into the queue.
151
     *
152
     * @param int $delay
153
     *
154
     * @return void
155
     */
156
    public function release(int $delay = 0)
157
    {
158
        $this->released = true;
159
    }
160
161
    /**
162
     * Determine if the job was released back into the queue.
163
     *
164
     * @return bool
165
     */
166
    public function isReleased(): bool
167
    {
168
        return $this->released;
169
    }
170
171
    /**
172
     * Determine if the job has been deleted or released.
173
     *
174
     * @return bool
175
     */
176
    public function isDeletedOrReleased(): bool
177
    {
178
        return $this->isDeleted() || $this->isReleased();
179
    }
180
181
    /**
182
     * Determine if the job has been marked as a failure.
183
     *
184
     * @return bool
185
     */
186
    public function hasFailed(): bool
187
    {
188
        return $this->failed;
189
    }
190
191
    /**
192
     * Mark the job as "failed".
193
     *
194
     * @return void
195
     */
196
    public function markAsFailed()
197
    {
198
        $this->failed = true;
199
    }
200
201
    /**
202
     * Get the decoded body of the job.
203
     *
204
     * @return array
205
     */
206
    public function payload(): array
207
    {
208
        return $this->job->getPayload();
209
    }
210
211
    /**
212
     * Get the number of times to attempt a job.
213
     *
214
     * @return int|null
215
     */
216
    public function maxTries(): ?int
217
    {
218
        return $this->payload()['maxTries'] ?? null;
219
    }
220
221
    /**
222
     * Get the number of seconds the job can run.
223
     *
224
     * @return int|null
225
     */
226
    public function timeout(): ?int
227
    {
228
        return $this->payload()['timeout'] ?? null;
229
    }
230
231
    /**
232
     * Get the timestamp indicating when the job should timeout.
233
     *
234
     * @return int|null
235
     */
236
    public function timeoutAt(): ?int
237
    {
238
        return $this->payload()['timeoutAt'] ?? null;
239
    }
240
241
    /**
242
     * Get the name of the queued job class.
243
     *
244
     * @return string
245
     */
246
    public function getName(): string
247
    {
248
        return $this->payload()['job'];
249
    }
250
251
    /**
252
     * Get data of queued job.
253
     *
254
     * @return array
255
     */
256
    public function getData(): array
257
    {
258
        return $this->payload()['data'];
259
    }
260
261
    /**
262
     * Get the name of the connection the job belongs to.
263
     *
264
     * @return string
265
     */
266
    public function getConnectionName(): ?string
267
    {
268
        return $this->connectionName;
269
    }
270
271
    /**
272
     * Get the name of the queue the job belongs to.
273
     *
274
     * @return string
275
     */
276
    public function getQueue(): ?string
277
    {
278
        return $this->job->getQueue();
279
    }
280
281
    /**
282
     * Get the number of times the job has been attempted.
283
     *
284
     * @return int
285
     */
286
    public function attempts(): int
287
    {
288
        return $this->job->getAttempts();
289
    }
290
291
    /**
292
     * Check if job reserved
293
     *
294
     * @return bool
295
     */
296
    public function reserved(): bool
297
    {
298
        return $this->job->isReserved();
299
    }
300
301
    /**
302
     * Get reserved at time
303
     *
304
     * @return int
305
     */
306
    public function reservedAt(): int
307
    {
308
        return $this->job->getReservedAt();
309
    }
310
311
    /**
312
     * Get the job identifier.
313
     *
314
     * @return string
315
     */
316
    public function getJobId(): string
317
    {
318
        return $this->job->getId();
319
    }
320
321
    /**
322
     * Get the raw body string for the job.
323
     *
324
     * @return string
325
     */
326
    public function getRawBody(): string
327
    {
328
        return json_encode($this->job->getPayload());
329
    }
330
331
    /**
332
     * Resolve job
333
     *
334
     * @param string $class
335
     *
336
     * @return JobInterface
337
     */
338
    protected function resolve(string $class): JobInterface
339
    {
340
        return $this->resolver->resolve($class);
341
    }
342
}
343