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
|
|
|
} else { |
67
|
1 |
|
$this->handler->reject($message, $adapter, $result); |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param MessageInterface $message |
73
|
|
|
* @param AdapterInterface $adapter |
74
|
|
|
* @param mixed $result |
75
|
|
|
*/ |
76
|
|
|
protected function reject( |
77
|
|
|
MessageInterface $message, |
78
|
|
|
AdapterInterface $adapter, |
79
|
|
|
$result = null |
80
|
|
|
) { |
81
|
|
|
$this->handler->reject($message, $adapter, $result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param AdapterInterface $adapter |
86
|
|
|
*/ |
87
|
3 |
|
protected function flush(AdapterInterface $adapter) |
88
|
|
|
{ |
89
|
3 |
|
$this->handler->flush($adapter); |
90
|
3 |
|
} |
91
|
|
|
} |
92
|
|
|
|