1 | <?php |
||
11 | class BeanstalkdQueueStoreAdapter implements QueueStoreAdapterInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var string the queue name |
||
15 | */ |
||
16 | protected $queueName; |
||
17 | /** |
||
18 | * @var int the time to run. Defaults to Pheanstalkd::DEFAULT_TTR. |
||
19 | */ |
||
20 | protected $timeToRun; |
||
21 | /** |
||
22 | * @var BeanstalkdQueueStoreConnection |
||
23 | */ |
||
24 | protected $connection; |
||
25 | /** |
||
26 | * @var int Reserves/locks a ready job in a watched tube. A timeout value of 0 will cause the server to immediately |
||
27 | * return either a response or TIMED_OUT. A positive value of timeout will limit the amount of time the client will |
||
28 | * block on the reserve request until a job becomes available. |
||
29 | * |
||
30 | * We highly recommend a non-zero value. Defaults to 5. |
||
31 | */ |
||
32 | protected $reserveTimeout; |
||
33 | |||
34 | /** |
||
35 | * BeanstalkdQueueStoreAdapter constructor. |
||
36 | * |
||
37 | * @param BeanstalkdQueueStoreConnection $connection |
||
38 | * @param string $queueName |
||
39 | * @param int $timeToRun |
||
40 | * @param int $reserveTimeOut |
||
41 | */ |
||
42 | 4 | public function __construct( |
|
54 | |||
55 | /** |
||
56 | * @return BeanstalkdQueueStoreAdapter |
||
57 | */ |
||
58 | 4 | public function init() |
|
64 | |||
65 | /** |
||
66 | * @return BeanstalkdQueueStoreConnection |
||
67 | */ |
||
68 | 4 | public function getConnection() |
|
72 | |||
73 | /** |
||
74 | * @param BeanstalkdMailJob|MailJobInterface $mailJob |
||
75 | * |
||
76 | * @return int |
||
77 | */ |
||
78 | 3 | public function enqueue(MailJobInterface $mailJob) |
|
89 | |||
90 | /** |
||
91 | * @return BeanstalkdMailJob|null |
||
92 | */ |
||
93 | 3 | public function dequeue() |
|
94 | { |
||
95 | 3 | $job = $this->getConnection()->getInstance()->watch($this->queueName)->reserve($this->reserveTimeout); |
|
96 | 3 | if ($job instanceof PheanstalkJob) { |
|
97 | 3 | $data = json_decode($job->getData(), true); |
|
98 | |||
99 | 3 | return new BeanstalkdMailJob( |
|
100 | [ |
||
101 | 3 | 'id' => $data['id'], |
|
102 | 3 | 'attempt' => $data['attempt'], |
|
103 | 3 | 'message' => $data['message'], |
|
104 | 3 | 'pheanstalkJob' => $job, |
|
105 | ] |
||
106 | 3 | ); |
|
107 | } |
||
108 | |||
109 | 2 | return null; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param BeanstalkdMailJob|MailJobInterface $mailJob |
||
114 | */ |
||
115 | 4 | public function ack(MailJobInterface $mailJob) |
|
116 | { |
||
117 | 4 | if ($mailJob->isNewRecord()) { |
|
118 | 1 | throw new InvalidCallException('BeanstalkdMailJob cannot be a new object to be acknowledged'); |
|
119 | } |
||
120 | |||
121 | 3 | $pheanstalk = $this->getConnection()->getInstance()->useTube($this->queueName); |
|
122 | 3 | if ($mailJob->isCompleted()) { |
|
123 | 2 | $pheanstalk->delete($mailJob->getPheanstalkJob()); |
|
124 | 2 | } else { |
|
125 | 1 | $timestamp = $mailJob->getTimeToSend(); |
|
126 | 1 | $delay = max(0, $timestamp - time()); |
|
127 | |||
128 | // add back to the queue as it wasn't completed maybe due to some transitory error |
||
129 | // could also be failed. |
||
130 | 1 | $pheanstalk->release($mailJob->getPheanstalkJob(), Pheanstalk::DEFAULT_PRIORITY, $delay); |
|
131 | } |
||
132 | 3 | } |
|
133 | |||
134 | /** |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | 2 | public function isEmpty() |
|
146 | |||
147 | /** |
||
148 | * @param BeanstalkdMailJob|MailJobInterface $mailJob |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 3 | protected function createPayload(MailJobInterface $mailJob) |
|
162 | } |
||
163 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: