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

PhpAmqpLib   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 78
ccs 23
cts 23
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createChannel() 0 17 3
A getConnection() 0 4 1
A __construct() 0 8 3
A registerConnection() 0 4 1
A registerChannel() 0 4 1
1
<?php
2
3
namespace AMQPAL\Adapter\PhpAmqpLib;
4
5
use PhpAmqpLib\Channel\AMQPChannel as LibChannel;
6
use PhpAmqpLib\Connection\AbstractConnection as LibConnection;
7
use AMQPAL\Adapter\AdapterInterface;
8
use AMQPAL\Adapter\Exception;
9
use AMQPAL\Adapter\PhpAmqpLib\Options\ConnectionOptions;
10
11
class PhpAmqpLib implements AdapterInterface
12
{
13
14
    /**
15
     * @var Connection
16
     */
17
    protected $connection;
18
    /**
19
     * @var Channel
20
     */
21
    protected $channelPrototype;
22
23
    /**
24
     * PhpAmqpLib constructor.
25
     *
26
     * @param Connection|ConnectionOptions|array|\Traversable|LibConnection $connection
27
     * @param Channel                                    $channelPrototype
28
     * @throws Exception\BadMethodCallException
29
     * @throws Exception\InvalidArgumentException
30
     * @throws Exception\RuntimeException
31
     */
32 4
    public function __construct($connection, Channel $channelPrototype = null)
33
    {
34 4
        if (!$connection instanceof Connection) {
35 1
            $connection = new Connection($connection);
0 ignored issues
show
Bug introduced by
It seems like $connection defined by new \AMQPAL\Adapter\PhpA...Connection($connection) on line 35 can also be of type array or object<Traversable>; however, AMQPAL\Adapter\PhpAmqpLi...nnection::__construct() does only seem to accept object<PhpAmqpLib\Connec...ions\ConnectionOptions>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
36
        }
37 4
        $this->registerConnection($connection);
38 4
        $this->registerChannel($channelPrototype ?: new Channel());
39 4
    }
40
41
    /**
42
     * @param Connection $connection
43
     */
44 4
    public function registerConnection(Connection $connection)
45
    {
46 4
        $this->connection = $connection;
47 4
    }
48
49
    /**
50
     * @param Channel $channel
51
     */
52 4
    public function registerChannel(Channel $channel)
53
    {
54 4
        $this->channelPrototype = $channel;
55 4
    }
56
57
    /**
58
     * @param LibChannel $resource
59
     * @return Channel
60
     * @throws Exception\RuntimeException
61
     * @throws Exception\InvalidArgumentException
62
     */
63 3
    public function createChannel($resource = null)
64
    {
65 3
        $channel = clone $this->channelPrototype;
66
67 3
        $channel->setConnection($this->getConnection());
68
69 3
        if ($resource instanceof LibChannel) {
70 1
            $channel->setResource($resource);
71
        } else {
72 2
            if (!$this->getConnection()->isConnected()) {
73 1
                $this->getConnection()->connect();
74
            }
75 2
            $channel->setResource($this->getConnection()->getResource()->channel());
76
        }
77
78 3
        return $channel;
79
    }
80
81
    /**
82
     * @return Connection
83
     */
84 4
    public function getConnection()
85
    {
86 4
        return $this->connection;
87
    }
88
}
89