Completed
Push — master ( cef50b...fa3cc9 )
by Thomas Mauro
02:58
created

PhpAmqpLib::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 2
crap 3
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 1
    public function __construct($connection, Channel $channelPrototype = null)
33
    {
34 1
        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 1
        $this->registerConnection($connection);
38 1
        $this->registerChannel($channelPrototype ?: new Channel());
39 1
    }
40
41
    /**
42
     * @param Connection $connection
43
     */
44 1
    public function registerConnection(Connection $connection)
45
    {
46 1
        $this->connection = $connection;
47 1
    }
48
49
    /**
50
     * @param Channel $channel
51
     */
52 1
    public function registerChannel(Channel $channel)
53
    {
54 1
        $this->channelPrototype = $channel;
55 1
    }
56
57
    /**
58
     * @param LibChannel $resource
59
     * @return Channel
60
     * @throws Exception\RuntimeException
61
     * @throws Exception\InvalidArgumentException
62
     */
63
    public function createChannel($resource = null)
64
    {
65
        $channel = clone $this->channelPrototype;
66
67
        $channel->setConnection($this->getConnection());
68
69
        if ($resource instanceof LibChannel) {
70
            $channel->setResource($resource);
71
        } else {
72
            if (!$this->getConnection()->isConnected()) {
73
                $this->getConnection()->connect();
74
            }
75
            $channel->setResource($this->getConnection()->getResource()->channel());
76
        }
77
78
        return $channel;
79
    }
80
81
    /**
82
     * @return Connection
83
     */
84 1
    public function getConnection()
85
    {
86 1
        return $this->connection;
87
    }
88
}
89