|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\Job; |
|
4
|
|
|
|
|
5
|
|
|
use SfCod\QueueBundle\Base\JobInterface; |
|
6
|
|
|
use SfCod\QueueBundle\Base\JobResolverInterface; |
|
7
|
|
|
use SfCod\QueueBundle\Queue\QueueInterface; |
|
8
|
|
|
use stdClass; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* MongoJob for laravel queue |
|
12
|
|
|
* |
|
13
|
|
|
* @author Alexey Orlov <[email protected]> |
|
14
|
|
|
* @author Virchenko Maksim <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class MongoJobContract extends JobContract implements JobContractInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Job resolver |
|
20
|
|
|
* |
|
21
|
|
|
* @var JobResolverInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $resolver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The database queue instance. |
|
27
|
|
|
* |
|
28
|
|
|
* @var QueueInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $database; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The database job payload. |
|
34
|
|
|
* |
|
35
|
|
|
* @var StdClass |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $job; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new job instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @param JobResolverInterface $resolver |
|
43
|
|
|
* @param QueueInterface $database |
|
44
|
|
|
* @param StdClass|MongoDB\Model\BSONDocument $job |
|
45
|
|
|
* @param string $queue |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(JobResolverInterface $resolver, QueueInterface $database, $job, string $queue) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->resolver = $resolver; |
|
50
|
|
|
$this->database = $database; |
|
51
|
|
|
$this->job = $job; |
|
|
|
|
|
|
52
|
|
|
$this->queue = $queue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Delete the job from the queue. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function delete() |
|
59
|
|
|
{ |
|
60
|
|
|
parent::delete(); |
|
61
|
|
|
|
|
62
|
|
|
if ($this->database->deleteReserved($this->queue, (string)$this->getJobId())) { |
|
63
|
|
|
$this->deleted = true; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the number of times the job has been attempted. |
|
69
|
|
|
* |
|
70
|
|
|
* @return int |
|
71
|
|
|
*/ |
|
72
|
|
|
public function attempts(): int |
|
73
|
|
|
{ |
|
74
|
|
|
return (int)$this->job->attempts; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Check if job reserved |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function reserved(): bool |
|
83
|
|
|
{ |
|
84
|
|
|
return (bool)$this->job->reserved; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get reserved at time |
|
89
|
|
|
* |
|
90
|
|
|
* @return int |
|
91
|
|
|
*/ |
|
92
|
|
|
public function reservedAt(): int |
|
93
|
|
|
{ |
|
94
|
|
|
return (int)$this->job->reserved_at; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the job identifier. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getJobId(): string |
|
103
|
|
|
{ |
|
104
|
|
|
return (string)$this->job->_id; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the raw body string for the job. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getRawBody(): string |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->job->payload; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Resolve job |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $class |
|
121
|
|
|
* |
|
122
|
|
|
* @return JobInterface |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function resolve(string $class): JobInterface |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->resolver->resolve($class); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.