RpcClient   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 2 Features 2
Metric Value
wmc 7
c 6
b 2
f 2
lcom 1
cbo 3
dl 0
loc 104
ccs 42
cts 42
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initClient() 0 6 1
A addRequest() 0 18 2
A getReplies() 0 23 2
A processMessage() 0 4 1
A setTimeout() 0 4 1
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 5
        $this->setParameter('correlation_id', $requestId);
84 5
        $this->setParameter('reply_to', $this->queueName);
85
86 5
        $message = new AMQPMessage(
87 4
            $messageBody,
88 5
            $this->getParameters()
89 4
        );
90
91 5
        $this->channel
92 5
            ->basic_publish($message, $server . '-exchange', $routingKey);
93
94 5
        $this->requests++;
95 5
    }
96
97
    /**
98
     * Get replies.
99
     *
100
     * @return array
101
     */
102 15
    public function getReplies()
103
    {
104 15
        $this->channel
105 15
            ->basic_consume(
106 15
                $this->queueName,
107 15
                $this->queueName,
108 15
                false,
109 15
                true,
110 15
                false,
111 15
                false,
112 15
                array($this, 'processMessage')
113 12
            );
114
115 15
        while (count($this->replies) < $this->requests) {
116 10
            $this->channel
117 10
                ->wait(null, false, $this->requestTimeout);
118 8
        }
119
120 15
        $this->channel
121 15
            ->basic_cancel($this->queueName);
122
123 15
        return $this->replies;
124
    }
125
126
    /**
127
     * @param AMQPMessage $message
128
     */
129 5
    public function processMessage(AMQPMessage $message)
130
    {
131 5
        $this->replies[$message->get('correlation_id')] = $message->body;
132 5
    }
133
134
    /**
135
     * @param int $timeout
136
     */
137 5
    public function setTimeout($timeout)
138
    {
139 5
        $this->requestTimeout = $timeout;
140 5
    }
141
}
142