|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Connection; |
|
4
|
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Connection\Exception\InvalidAMQPConnectionClassException; |
|
6
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class based on AMQPConnectionFactory from OldSoundRabbitmqBundle |
|
10
|
|
|
* Class ConnectionFactory. |
|
11
|
|
|
*/ |
|
12
|
|
|
class ConnectionFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \ReflectionClass */ |
|
15
|
|
|
private $class; |
|
16
|
|
|
|
|
17
|
|
|
/** @var array */ |
|
18
|
|
|
private $parameters = [ |
|
19
|
|
|
'host' => 'localhost', |
|
20
|
|
|
'port' => 5672, |
|
21
|
|
|
'user' => 'guest', |
|
22
|
|
|
'password' => 'guest', |
|
23
|
|
|
'vhost' => '/', |
|
24
|
|
|
'connection_timeout' => 3, |
|
25
|
|
|
'read_write_timeout' => 3, |
|
26
|
|
|
'ssl_context' => null, |
|
27
|
|
|
'keepalive' => false, |
|
28
|
|
|
'heartbeat' => 0, |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param $class string FQCN of AMQPConnection class to instantiate. |
|
33
|
|
|
* @param array $parameters |
|
34
|
|
|
* |
|
35
|
|
|
* @throws InvalidAMQPConnectionClassException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($class, array $parameters) |
|
38
|
|
|
{ |
|
39
|
|
|
if (!is_a($class, AMQPStreamConnection::class, true)) { |
|
40
|
|
|
throw new InvalidAMQPConnectionClassException('$class not instance of AMQPStreamConnection'); |
|
41
|
|
|
} |
|
42
|
|
|
$this->class = $class; |
|
43
|
|
|
$this->parameters = array_merge($this->parameters, $parameters); |
|
44
|
|
|
|
|
45
|
|
|
if (is_array($this->parameters['ssl_context'])) { |
|
46
|
|
|
$this->parameters['ssl_context'] = null; |
|
47
|
|
|
|
|
48
|
|
|
if (!empty($this->parameters['ssl_context'])) { |
|
49
|
|
|
stream_context_create(['ssl' => $this->parameters['ssl_context']]); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getParameters() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->parameters; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return \ReflectionClass|string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getClass() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->class; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return CmobiAMQPConnectionInterface |
|
72
|
|
|
*/ |
|
73
|
|
|
public function createConnection() |
|
74
|
|
|
{ |
|
75
|
|
|
return new $this->class( |
|
76
|
|
|
$this->parameters['host'], |
|
77
|
|
|
$this->parameters['port'], |
|
78
|
|
|
$this->parameters['user'], |
|
79
|
|
|
$this->parameters['password'], |
|
80
|
|
|
$this->parameters['vhost'], |
|
81
|
|
|
false, // insist |
|
82
|
|
|
'AMQPLAIN', // login_method |
|
83
|
|
|
null, // login_response |
|
84
|
|
|
'en_US', // locale |
|
85
|
|
|
$this->parameters['connection_timeout'], |
|
86
|
|
|
$this->parameters['read_write_timeout'], |
|
87
|
|
|
$this->parameters['ssl_context'], |
|
88
|
|
|
$this->parameters['keepalive'], |
|
89
|
|
|
$this->parameters['heartbeat'] |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|