|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of graze/queue. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @license https://github.com/graze/queue/blob/master/LICENSE MIT |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/graze/queue |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Graze\Queue\Handler; |
|
17
|
|
|
|
|
18
|
|
|
use Graze\Queue\Adapter\AdapterInterface; |
|
19
|
|
|
use Graze\Queue\Message\MessageInterface; |
|
20
|
|
|
|
|
21
|
|
|
class ResultAcknowledgementHandler extends AbstractAcknowledgementHandler |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var callable */ |
|
24
|
|
|
private $validator; |
|
25
|
|
|
|
|
26
|
|
|
/** @var AbstractAcknowledgementHandler */ |
|
27
|
|
|
private $handler; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* ResultAcknowledgementHandler constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param callable $validator |
|
33
|
|
|
* @param AbstractAcknowledgementHandler $handler |
|
34
|
|
|
*/ |
|
35
|
3 |
|
public function __construct(callable $validator, AbstractAcknowledgementHandler $handler) |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* This callable should accept the mixed result returned by a worker |
|
39
|
|
|
* and return a boolean value. |
|
40
|
|
|
* |
|
41
|
|
|
* @var callable |
|
42
|
|
|
*/ |
|
43
|
3 |
|
$this->validator = $validator; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The handler to call `acknowledge` or `reject` on if {@see $validator} returns a |
|
47
|
|
|
* truthy value for the given result. |
|
48
|
|
|
* |
|
49
|
|
|
* @var AbstractAcknowledgementHandler |
|
50
|
|
|
*/ |
|
51
|
3 |
|
$this->handler = $handler; |
|
52
|
3 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param MessageInterface $message |
|
56
|
|
|
* @param AdapterInterface $adapter |
|
57
|
|
|
* @param mixed $result |
|
58
|
|
|
*/ |
|
59
|
3 |
|
protected function acknowledge( |
|
60
|
|
|
MessageInterface $message, |
|
61
|
|
|
AdapterInterface $adapter, |
|
62
|
|
|
$result = null |
|
63
|
|
|
) { |
|
64
|
3 |
|
if (call_user_func($this->validator, $result) === true) { |
|
65
|
2 |
|
$this->handler->acknowledge($message, $adapter, $result); |
|
66
|
2 |
|
} else { |
|
67
|
1 |
|
$this->handler->reject($message, $adapter, $result); |
|
68
|
|
|
} |
|
69
|
3 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param MessageInterface $message |
|
73
|
|
|
* @param AdapterInterface $adapter |
|
74
|
|
|
* @param int $duration Number of seconds to ensure that this message is not seen by any other clients |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function extend( |
|
77
|
|
|
MessageInterface $message, |
|
78
|
|
|
AdapterInterface $adapter, |
|
79
|
|
|
$duration |
|
80
|
|
|
) { |
|
81
|
|
|
$this->handler->extend($message, $adapter, $duration); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param MessageInterface $message |
|
86
|
|
|
* @param AdapterInterface $adapter |
|
87
|
|
|
* @param mixed $result |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function reject( |
|
90
|
|
|
MessageInterface $message, |
|
91
|
|
|
AdapterInterface $adapter, |
|
92
|
|
|
$result = null |
|
93
|
|
|
) { |
|
94
|
|
|
$this->handler->reject($message, $adapter, $result); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param AdapterInterface $adapter |
|
99
|
|
|
*/ |
|
100
|
3 |
|
protected function flush(AdapterInterface $adapter) |
|
101
|
|
|
{ |
|
102
|
3 |
|
$this->handler->flush($adapter); |
|
103
|
3 |
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|