Completed
Pull Request — master (#2)
by
unknown
38:19
created

JobContract   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 2
dl 0
loc 249
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A fire() 0 6 1
A delete() 0 4 1
A isDeleted() 0 4 1
A release() 0 4 1
A isReleased() 0 4 1
A isDeletedOrReleased() 0 4 2
A hasFailed() 0 4 1
A markAsFailed() 0 4 1
A failed() 0 8 2
A payload() 0 4 1
A maxTries() 0 4 1
A timeout() 0 4 1
A timeoutAt() 0 4 1
A getName() 0 4 1
A getData() 0 4 1
A getConnectionName() 0 4 1
A getQueue() 0 4 1
getJobId() 0 1 ?
getRawBody() 0 1 ?
resolve() 0 1 ?
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
9
/**
10
 * Class Job
11
 *
12
 * @author Alexey Orlov <[email protected]>
13
 * @author Virchenko Maksim <[email protected]>
14
 *
15
 * @package SfCod\QueueBundle\Base
16
 */
17
abstract class JobContract implements JobContractInterface
18
{
19
    use InteractWithTimeTrait;
20
21
    /**
22
     * The job handler instance.
23
     *
24
     * @var JobInterface
25
     */
26
    protected $instance;
27
28
    /**
29
     * Indicates if the job has been deleted.
30
     *
31
     * @var bool
32
     */
33
    protected $deleted = false;
34
35
    /**
36
     * Indicates if the job has been released.
37
     *
38
     * @var bool
39
     */
40
    protected $released = false;
41
42
    /**
43
     * Indicates if the job has failed.
44
     *
45
     * @var bool
46
     */
47
    protected $failed = false;
48
49
    /**
50
     * The name of the connection the job belongs to.
51
     *
52
     * @var string
53
     */
54
    protected $connectionName;
55
56
    /**
57
     * The name of the queue the job belongs to.
58
     *
59
     * @var string
60
     */
61
    protected $queue;
62
63
    /**
64
     * Fire the job.
65
     *
66
     * @return void
67
     */
68
    public function fire()
69
    {
70
        $handler = $this->resolve($this->getName());
71
72
        $this->instance = $handler->fire($this, $this->getData());
73
    }
74
75
    /**
76
     * Delete the job from the queue.
77
     *
78
     * @return void
79
     */
80
    public function delete()
81
    {
82
        $this->deleted = true;
83
    }
84
85
    /**
86
     * Determine if the job has been deleted.
87
     *
88
     * @return bool
89
     */
90
    public function isDeleted(): bool
91
    {
92
        return $this->deleted;
93
    }
94
95
    /**
96
     * Release the job back into the queue.
97
     *
98
     * @param int $delay
99
     *
100
     * @return void
101
     */
102
    public function release(int $delay = 0)
103
    {
104
        $this->released = true;
105
    }
106
107
    /**
108
     * Determine if the job was released back into the queue.
109
     *
110
     * @return bool
111
     */
112
    public function isReleased(): bool
113
    {
114
        return $this->released;
115
    }
116
117
    /**
118
     * Determine if the job has been deleted or released.
119
     *
120
     * @return bool
121
     */
122
    public function isDeletedOrReleased(): bool
123
    {
124
        return $this->isDeleted() || $this->isReleased();
125
    }
126
127
    /**
128
     * Determine if the job has been marked as a failure.
129
     *
130
     * @return bool
131
     */
132
    public function hasFailed(): bool
133
    {
134
        return $this->failed;
135
    }
136
137
    /**
138
     * Mark the job as "failed".
139
     *
140
     * @return void
141
     */
142
    public function markAsFailed()
143
    {
144
        $this->failed = true;
145
    }
146
147
    /**
148
     * Process an exception that caused the job to fail.
149
     *
150
     * @param Exception $e
151
     *
152
     * @return void
153
     */
154
    public function failed($e)
155
    {
156
        $this->markAsFailed();
157
158
        if (method_exists($this->instance = $this->resolve($this->getName()), 'failed')) {
159
            $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...
160
        }
161
    }
162
163
    /**
164
     * Get the decoded body of the job.
165
     *
166
     * @return array
167
     */
168
    public function payload(): array
169
    {
170
        return json_decode($this->getRawBody(), true);
171
    }
172
173
    /**
174
     * Get the number of times to attempt a job.
175
     *
176
     * @return int|null
177
     */
178
    public function maxTries(): ?int
179
    {
180
        return $this->payload()['maxTries'] ?? null;
181
    }
182
183
    /**
184
     * Get the number of seconds the job can run.
185
     *
186
     * @return int|null
187
     */
188
    public function timeout(): ?int
189
    {
190
        return $this->payload()['timeout'] ?? null;
191
    }
192
193
    /**
194
     * Get the timestamp indicating when the job should timeout.
195
     *
196
     * @return int|null
197
     */
198
    public function timeoutAt(): ?int
199
    {
200
        return $this->payload()['timeoutAt'] ?? null;
201
    }
202
203
    /**
204
     * Get the name of the queued job class.
205
     *
206
     * @return string
207
     */
208
    public function getName(): string
209
    {
210
        return $this->payload()['job'];
211
    }
212
213
    /**
214
     * Get data of queued job.
215
     *
216
     * @return array
217
     */
218
    public function getData(): array
219
    {
220
        return $this->payload()['data'];
221
    }
222
223
    /**
224
     * Get the name of the connection the job belongs to.
225
     *
226
     * @return string
227
     */
228
    public function getConnectionName(): ?string
229
    {
230
        return $this->connectionName;
231
    }
232
233
    /**
234
     * Get the name of the queue the job belongs to.
235
     *
236
     * @return string
237
     */
238
    public function getQueue(): ?string
239
    {
240
        return $this->queue;
241
    }
242
243
    /**
244
     * Get the job identifier.
245
     *
246
     * @return string
247
     */
248
    abstract public function getJobId(): string;
249
250
    /**
251
     * Get the raw body of the job.
252
     *
253
     * @return string
254
     */
255
    abstract public function getRawBody(): string;
256
257
    /**
258
     * Resolve the given class
259
     *
260
     * @param string $class
261
     *
262
     * @return JobInterface
263
     */
264
    abstract protected function resolve(string $class): JobInterface;
265
}
266