1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2010 Alvaro Videla |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
15
|
|
|
* all copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
* |
25
|
|
|
* PHP version 5.3 |
26
|
|
|
* |
27
|
|
|
* @category Thumper |
28
|
|
|
* @package Thumper |
29
|
|
|
* @author Alvaro Videla |
30
|
|
|
* @copyright 2010 Alvaro Videla. All rights reserved. |
31
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
32
|
|
|
* @link https://github.com/videlalvaro/Thumper |
33
|
|
|
*/ |
34
|
|
|
namespace Thumper; |
35
|
|
|
|
36
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
37
|
|
|
|
38
|
|
|
class Consumer extends BaseConsumer |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Number of messages consumed. |
42
|
|
|
* |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
public $consumed = 0; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Target number of messages to consume. |
49
|
|
|
* |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
private $target; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param int $numOfMessages |
56
|
|
|
*/ |
57
|
45 |
|
public function consume($numOfMessages) |
58
|
|
|
{ |
59
|
45 |
|
$this->target = $numOfMessages; |
60
|
|
|
|
61
|
45 |
|
$this->setUpConsumer(); |
62
|
|
|
|
63
|
5 |
|
while (count($this->channel->callbacks)) { |
64
|
5 |
|
$this->channel->wait(); |
65
|
4 |
|
} |
66
|
5 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param AMQPMessage $message |
70
|
|
|
*/ |
71
|
10 |
|
public function processMessage(AMQPMessage $message) |
72
|
|
|
{ |
73
|
10 |
|
call_user_func($this->callback, $message->body); |
74
|
10 |
|
$message->delivery_info['channel'] |
75
|
10 |
|
->basic_ack($message->delivery_info['delivery_tag']); |
|
|
|
|
76
|
10 |
|
$this->consumed++; |
77
|
10 |
|
$this->maybeStopConsumer($message); |
78
|
10 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param AMQPMessage $message |
82
|
|
|
*/ |
83
|
10 |
|
protected function maybeStopConsumer(AMQPMessage $message) |
84
|
|
|
{ |
85
|
10 |
|
if ($this->consumed == $this->target) { |
86
|
5 |
|
$message->delivery_info['channel'] |
87
|
5 |
|
->basic_cancel($message->delivery_info['consumer_tag']); |
|
|
|
|
88
|
4 |
|
} |
89
|
10 |
|
} |
90
|
|
|
} |
91
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: