Passed
Branch master (d6c481)
by Cody
02:22
created

Connection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A connect() 0 4 1
A getChannel() 0 4 1
A getStream() 0 23 2
1
<?php
2
3
namespace Ccovey\RabbitMQ\Connection;
4
5
use Ccovey\RabbitMQ\Channel;
6
use Ccovey\RabbitMQ\ChannelInterface;
7
use PhpAmqpLib\Connection\AMQPStreamConnection;
8
9
class Connection implements ConnectionInterface
10
{
11
    /**
12
     * @var ConnectionParameters
13
     */
14
    private $parameters;
15
16
    /**
17
     * @var AMQPStreamConnection
18
     */
19
    private $stream;
20
21
    public function __construct(ConnectionParameters $parameters)
22
    {
23
        $this->parameters = $parameters;
24
    }
25
26
    public function connect()
27
    {
28
        $this->getStream()->reconnect();
29
    }
30
31
    public function getChannel(string $channelId = '') : ChannelInterface
32
    {
33
        return new Channel($this->getStream()->channel($channelId));
34
    }
35
36
    protected function getStream()
37
    {
38
        if (!$this->stream) {
39
            $this->stream = new AMQPStreamConnection(
40
                $this->parameters->getHost(),
41
                $this->parameters->getPort(),
42
                $this->parameters->getUser(),
43
                $this->parameters->getPassword(),
44
                $this->parameters->getVhost(),
45
                $this->parameters->shouldInsist(),
46
                $this->parameters->getLoginMethod(),
47
                $this->parameters->getLoginResponse(),
48
                $this->parameters->getLocale(),
49
                $this->parameters->getConnectionTimeout(),
50
                $this->parameters->getReadWriteTimeout(),
51
                $this->parameters->getContext(),
52
                $this->parameters->shouldKeepalive(),
53
                $this->parameters->getHeartbeat()
54
            );
55
        }
56
57
        return $this->stream;
58
    }
59
}
60