Completed
Push — master ( f55883...4285c1 )
by Thomas Mauro
13:12 queued 10:06
created

Connection   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 2
cbo 4
dl 0
loc 136
ccs 35
cts 35
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getOptions() 0 4 1
A setOptions() 0 9 2
A createResource() 0 7 1
A setResource() 0 5 1
A getResource() 0 4 1
A connect() 0 8 2
A reconnect() 0 6 1
A disconnect() 0 6 1
A isConnected() 0 4 1
1
<?php
2
3
namespace AMQPAL\Adapter\PhpAmqpLib;
4
5
use AMQPAL\Adapter\Exception;
6
use AMQPAL\Adapter\ConnectionInterface;
7
use AMQPAL\Adapter\PhpAmqpLib\Options\ConnectionOptions;
8
use PhpAmqpLib\Connection\AbstractConnection;
9
use Traversable;
10
11
/**
12
 * Class Connection
13
 *
14
 * @package AMQPAL\Adapter\PhpAmqpLib
15
 */
16
class Connection implements ConnectionInterface
17
{
18
    /**
19
     * @var AbstractConnection
20
     */
21
    protected $resource;
22
    /**
23
     * @var ConnectionOptions
24
     */
25
    protected $options;
26
27
    /**
28
     * Connection constructor.
29
     *
30
     * @param AbstractConnection|ConnectionOptions $connection
31
     * @throws Exception\RuntimeException
32
     * @throws Exception\InvalidArgumentException
33
     * @throws Exception\BadMethodCallException
34
     */
35 8
    public function __construct($connection)
36
    {
37 8
        if (!$connection instanceof AbstractConnection) {
38 1
            $this->setOptions($connection);
39 1
            $connection = $this->createResource();
40
        }
41
42 8
        $this->setResource($connection);
43 8
    }
44
45
    /**
46
     * @return ConnectionOptions
47
     */
48 2
    public function getOptions()
49
    {
50 2
        return $this->options;
51
    }
52
53
    /**
54
     * @param ConnectionOptions|Traversable|array $options
55
     * @return $this
56
     * @throws Exception\InvalidArgumentException
57
     * @throws Exception\BadMethodCallException
58
     */
59 3
    public function setOptions($options)
60
    {
61 3
        if (!$options instanceof ConnectionOptions) {
62 1
            $options = new ConnectionOptions($options);
63
        }
64 3
        $this->options = $options;
65
66 3
        return $this;
67
    }
68
69
    /**
70
     * @return AbstractConnection
71
     * @throws Exception\RuntimeException
72
     * @throws Exception\InvalidArgumentException
73
     */
74 1
    protected function createResource()
75
    {
76 1
        $factory = $this->getOptions()
77 1
            ->getConnectionFactoryFactory()
78 1
            ->createFactory($this->getOptions()->getType());
79 1
        return $factory->createConnection($this->getOptions());
80
    }
81
82
    /**
83
     * @param AbstractConnection $resource
84
     * @return $this
85
     */
86 8
    public function setResource(AbstractConnection $resource)
87
    {
88 8
        $this->resource = $resource;
89 8
        return $this;
90
    }
91
92
    /**
93
     * @return AbstractConnection
94
     */
95 1
    public function getResource()
96
    {
97 1
        return $this->resource;
98
    }
99
100
    /**
101
     * Establish a connection with the AMQP broker.
102
     *
103
     * @return $this
104
     * @throws Exception\RuntimeException
105
     * @throws Exception\InvalidArgumentException
106
     */
107 1
    public function connect()
108
    {
109 1
        if (!$this->resource->isConnected()) {
110 1
            $this->resource->reconnect();
111
        }
112
113 1
        return $this;
114
    }
115
116
    /**
117
     * Close any open connections and initiate a new one with the AMQP broker.
118
     *
119
     * @return $this
120
     */
121 1
    public function reconnect()
122
    {
123 1
        $this->resource->reconnect();
124
125 1
        return $this;
126
    }
127
128
    /**
129
     * Closes the connection with the AMQP broker.
130
     *
131
     * @return $this
132
     * @throws Exception\RuntimeException
133
     */
134 1
    public function disconnect()
135
    {
136 1
        $this->resource->close();
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * Check whether the connection to the AMQP broker is still valid.
143
     *
144
     * @return bool
145
     * @throws Exception\RuntimeException
146
     */
147 1
    public function isConnected()
148
    {
149 1
        return $this->resource->isConnected();
150
    }
151
}
152