Completed
Push — master ( 20fe45...9d479c )
by Daniel
03:03
created

RpcClient::setConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc;
4
5
use Cmobi\RabbitmqBundle\ConnectionManagerInterface;
6
use PhpAmqpLib\Channel\AbstractChannel;
7
use PhpAmqpLib\Connection\AMQPStreamConnection;
8
use PhpAmqpLib\Message\AMQPMessage;
9
10
abstract class RpcClient
11
{
12
    private $queue;
13
    private $connection;
14
    private $body;
15
    private $channel;
16
    private $callbackQueue;
17
    private $response;
18
    private $correlationId;
19
20
    public function __construct($queueName, ConnectionManagerInterface $manager)
21
    {
22
        $this->body = '';
23
        $this->queue = $queueName;
24
        $this->connection = $manager->getConnection();
25
        $this->channel = $this->connection->channel();
26
    }
27
28
    /**
29
     * @param AMQPMessage $rep
30
     */
31
    public function onResponse(AMQPMessage $rep)
32
    {
33
        if($rep->get('correlation_id') == $this->correlationId) {
34
            $this->response = $rep->body;
35
        }
36
    }
37
38
    /**
39
     * @return null|string
40
     */
41
    public function call()
42
    {
43
        list($callbackQueue, ,) = $this->getChannel()->queue_declare(
44
            '', false, false, true, false
45
        );
46
        $this->callbackQueue = $callbackQueue;
47
        $this->getChannel()->basic_consume(
48
            $this->callbackQueue, '', false, false, false, false,
49
            [$this, 'onResponse']
50
        );
51
        $this->response = null;
52
        $this->correlationId = uniqid();
53
54
        $msg = new AMQPMessage(
55
            $this->getMessage(),
56
            [
57
                'correlation_id' => $this->correlationId,
58
                'reply_to' => $this->callbackQueue
59
            ]
60
        );
61
        $this->getChannel()->basic_publish($msg, '', $this->getQueueName());
62
63
        while(!$this->response) {
64
            $this->getChannel()->wait();
65
        }
66
        return $this->response;
67
    }
68
69
    /**
70
     * @param string $body
71
     */
72
    public function declareMessage($body)
73
    {
74
        $this->body = (string)$body;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getMessage()
81
    {
82
        return $this->body;
83
    }
84
85
    /**
86
     * @param AbstractChannel $channel
87
     */
88
    public function setChannel(AbstractChannel $channel)
89
    {
90
        $this->channel = $channel;
91
    }
92
93
    /**
94
     * @return \PhpAmqpLib\Channel\AMQPChannel
95
     */
96
    public function getChannel()
97
    {
98
        return $this->channel;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getQueueName()
105
    {
106
        return $this->queue;
107
    }
108
109
    /**
110
     * @return ConnectionManagerInterface
111
     */
112
    public function getConnection()
113
    {
114
        return $this->connection;
115
    }
116
117
    /**
118
     * @param \PhpAmqpLib\Connection\AMQPStreamConnection $connection
119
     */
120
    public function setConnection(AMQPStreamConnection $connection)
121
    {
122
        $this->connection = $connection;
123
    }
124
}