Completed
Push — master ( f32f25...d91936 )
by John
03:08
created

RpcClient   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 2
Metric Value
wmc 7
c 5
b 2
f 2
lcom 1
cbo 3
dl 0
loc 106
ccs 43
cts 43
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processMessage() 0 4 1
A setTimeout() 0 4 1
A initClient() 0 6 1
A addRequest() 0 20 2
A getReplies() 0 23 2
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 RpcClient extends BaseAmqp
39
{
40
    /**
41
     * @var int
42
     */
43
    protected $requests;
44
45
    /**
46
     * @var string[]
47
     */
48
    protected $replies;
49
50
    /**
51
     * @var string
52
     */
53
    protected $queueName;
54
55
    /**
56
     * @var int
57
     */
58
    protected $requestTimeout = null;
59
60
    /**
61
     * Initialize client.
62
     */
63 5
    public function initClient()
64
    {
65 5
        list($this->queueName, , ) = $this->channel->queue_declare('', false, false, true, true);
66 5
        $this->requests = 0;
67 5
        $this->replies = array();
68 5
    }
69
70
    /**
71
     * Add request to be sent to RPC Server.
72
     *
73
     * @param string $messageBody
74
     * @param string $server
75
     * @param string $requestId
76
     * @param string $routingKey
77
     */
78 25
    public function addRequest($messageBody, $server, $requestId, $routingKey = '')
79
    {
80 25
        if (empty($requestId)) {
81 20
            throw new \InvalidArgumentException("You must provide a request ID");
82
        }
83
84 5
        $message = new AMQPMessage(
85 4
            $messageBody,
86
            array(
87 5
                'content_type' => 'text/plain',
88 5
                'reply_to' => $this->queueName,
89 1
                'correlation_id' => $requestId
90 4
            )
91 4
        );
92
93 5
        $this->channel
94 5
            ->basic_publish($message, $server . '-exchange', $routingKey);
95
96 5
        $this->requests++;
97 5
    }
98
99
    /**
100
     * Get replies.
101
     *
102
     * @return array
103
     */
104 15
    public function getReplies()
105
    {
106 15
        $this->channel
107 15
            ->basic_consume(
108 15
                $this->queueName,
109 15
                $this->queueName,
110 15
                false,
111 15
                true,
112 15
                false,
113 15
                false,
114 15
                array($this, 'processMessage')
115 12
            );
116
117 15
        while (count($this->replies) < $this->requests) {
118 10
            $this->channel
119 10
                ->wait(null, false, $this->requestTimeout);
120 8
        }
121
122 15
        $this->channel
123 15
            ->basic_cancel($this->queueName);
124
125 15
        return $this->replies;
126
    }
127
128
    /**
129
     * @param AMQPMessage $message
130
     */
131 5
    public function processMessage(AMQPMessage $message)
132
    {
133 5
        $this->replies[$message->get('correlation_id')] = $message->body;
134 5
    }
135
136
    /**
137
     * @param int $timeout
138
     */
139 5
    public function setTimeout($timeout)
140
    {
141 5
        $this->requestTimeout = $timeout;
142 5
    }
143
}
144