Completed
Pull Request — master (#1)
by Daniel
12:19
created

ConnectionFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 80
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A getParameters() 0 4 1
A getClass() 0 4 1
A createConnection() 0 19 1
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
 * @package Cmobi\RabbitmqBundle\Amqp
12
 */
13
class ConnectionFactory
14
{
15
    /** @var \ReflectionClass */
16
    private $class;
17
18
    /** @var array */
19
    private $parameters = [
20
        'host'               => 'localhost',
21
        'port'               => 5672,
22
        'user'               => 'guest',
23
        'password'           => 'guest',
24
        'vhost'              => '/',
25
        'connection_timeout' => 3,
26
        'read_write_timeout' => 3,
27
        'ssl_context'        => null,
28
        'keepalive'          => false,
29
        'heartbeat'          => 0,
30
    ];
31
32
    /**
33
     * @param $class string FQCN of AMQPConnection class to instantiate.
34
     * @param array $parameters
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
}