Completed
Push — master ( 3a5f88...05006a )
by Thomas Mauro
10:18 queued 07:14
created

Connection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace AMQPAL\Adapter\AMQP;
4
5
use Traversable;
6
use AMQPConnection;
7
use AMQPAL\Adapter\Exception;
8
use AMQPAL\Adapter\ConnectionInterface;
9
use AMQPAL\Adapter\AMQP\Options\ConnectionOptions;
10
11
/**
12
 * Class Connection
13
 *
14
 * @package AMQPAL\Adapter\AMQP
15
 */
16
class Connection implements ConnectionInterface
17
{
18
    /**
19
     * @var AMQPConnection
20
     */
21
    protected $resource;
22
    /**
23
     * @var ConnectionOptions
24
     */
25
    protected $options;
26
27
    /**
28
     * Connection constructor.
29
     *
30
     * @param AMQPConnection|ConnectionOptions $connection
31
     * @throws Exception\BadMethodCallException
32
     * @throws Exception\InvalidArgumentException
33
     */
34 10
    public function __construct($connection)
35
    {
36 10
        if (!$connection instanceof AMQPConnection) {
37 1
            $this->setOptions($connection);
38 1
            $connection = $this->createResource($this->getOptions());
39
        }
40
41 10
        $this->setResource($connection);
42 10
    }
43
44
    /**
45
     * @return ConnectionOptions
46
     */
47 8
    public function getOptions()
48
    {
49 8
        return $this->options;
50
    }
51
52
    /**
53
     * @param ConnectionOptions|Traversable|array $options
54
     * @return $this
55
     * @throws Exception\InvalidArgumentException
56
     * @throws Exception\BadMethodCallException
57
     */
58 8
    public function setOptions($options)
59
    {
60 8
        if (!$options instanceof ConnectionOptions) {
61 1
            $options = new ConnectionOptions($options);
62
        }
63 8
        $this->options = $options;
64
65 8
        return $this;
66
    }
67
68
    /**
69
     * @param ConnectionOptions $options
70
     * @return AMQPConnection
71
     */
72 1
    protected function createResource(ConnectionOptions $options)
73
    {
74
        $params = [
75 1
            'host'  => $options->getHost(),
76 1
            'port'  => $options->getPort(),
77 1
            'vhost' => $options->getVhost(),
78 1
            'login' => $options->getUsername(),
79 1
            'password' => $options->getPassword(),
80 1
            'read_timeout'  => $options->getReadTimeout(),
81 1
            'write_timeout' => $options->getWriteTimeout(),
82 1
            'connect_timeout' => $options->getConnectTimeout(),
83 1
            'channel_max' => $options->getChannelMax(),
84 1
            'frame_max' => $options->getFrameMax(),
85 1
            'heartbeat' => $options->getHeartbeat()
86
        ];
87
88 1
        return new AMQPConnection(array_filter($params, [$this, 'filterConnectionParam']));
89
    }
90
91
    /**
92
     * @param mixed $paramValue
93
     * @return bool
94
     */
95 1
    protected function filterConnectionParam($paramValue)
96
    {
97 1
        return null !== $paramValue;
98
    }
99
100
    /**
101
     * @param AMQPConnection $resource
102
     * @return $this
103
     */
104 10
    public function setResource(AMQPConnection $resource)
105
    {
106 10
        $this->resource = $resource;
107 10
        return $this;
108
    }
109
110
    /**
111
     * @return AMQPConnection
112
     */
113 8
    public function getResource()
114
    {
115 8
        return $this->resource;
116
    }
117
118
    /**
119
     * Establish a connection with the AMQP broker.
120
     *
121
     * @return $this
122
     * @throws \AMQPConnectionException
123
     */
124 2
    public function connect()
125
    {
126 2
        if ($this->getOptions()->isPersistent()) {
127 1
            $this->getResource()->pconnect();
128
        } else {
129 1
            $this->getResource()->connect();
130
        }
131
132 2
        return $this;
133
    }
134
135
    /**
136
     * Close any open connections and initiate a new one with the AMQP broker.
137
     *
138
     * @return $this
139
     */
140 2
    public function reconnect()
141
    {
142 2
        if ($this->getOptions()->isPersistent()) {
143 1
            $this->getResource()->preconnect();
0 ignored issues
show
Bug introduced by
The method preconnect() does not exist on AMQPConnection. Did you maybe mean connect()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
144
        } else {
145 1
            $this->getResource()->reconnect();
146
        }
147
148 2
        return $this;
149
    }
150
151
    /**
152
     * Closes the connection with the AMQP broker.
153
     *
154
     * @return $this
155
     */
156 2
    public function disconnect()
157
    {
158 2
        if ($this->getOptions()->isPersistent()) {
159
            // @todo: should disconnect on persistent connection?
160 1
            $this->getResource()->pdisconnect();
161
        } else {
162 1
            $this->getResource()->disconnect();
163
        }
164
165 2
        return $this;
166
    }
167
168
    /**
169
     * Check whether the connection to the AMQP broker is still valid.
170
     *
171
     * @return bool
172
     */
173 1
    public function isConnected()
174
    {
175 1
        return $this->getResource()->isConnected();
176
    }
177
}
178