Completed
Push — develop ( 7ec6a7...e72b38 )
by Nicolas
11:34
created

Channel::bindQueueExchange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace devtransition\rabbitmq\base;
4
5
use PhpAmqpLib\Channel\AMQPChannel;
6
use PhpAmqpLib\Message\AMQPMessage;
7
use yii\base\Component;
8
use yii\helpers\ArrayHelper;
9
10
11
class Channel extends Component
12
{
13
    const EXCHANGE_TYPE_TOPIC = 'topic';
14
    const EXCHANGE_TYPE_DIRECT = 'direct';
15
    const EXCHANGE_TYPE_HEADERS = 'headers';
16
    const EXCHANGE_TYPE_FANOUT = 'fanout';
17
18
    /**
19
     * @var Connection
20
     */
21
    protected $connection;
22
23
    /**
24
     * @var AMQPChannel
25
     */
26
    protected $amqpChannel;
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function __construct(ConnectionInterface $connection, $config = [])
32
    {
33
        $this->connection = $connection;
0 ignored issues
show
Documentation Bug introduced by
$connection is of type object<devtransition\rab...se\ConnectionInterface>, but the property $connection was declared to be of type object<devtransition\rabbitmq\base\Connection>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
34
        $this->amqpChannel = $this->connection->getAmqpInstance()->channel();
35
        parent::__construct($config);
36
    }
37
38
    public function __destruct()
39
    {
40
        $this->amqpChannel->close();
41
    }
42
43
    /**
44
     * Server should continue sending data to us. Default on new channels.
45
     */
46
    public function enableFlow()
47
    {
48
        $this->amqpChannel->flow(true);
49
    }
50
51
    /**
52
     * Server should stop sending data to us.
53
     */
54
    public function disableFlow()
55
    {
56
        $this->amqpChannel->flow(true);
57
    }
58
59
    public function send()
60
    {
61
        $msg = new AMQPMessage('test');
62
        $this->amqpChannel->basic_publish($msg, '', 'test');
63
    }
64
65
    public function getMessageOne($queue, $no_ack = false)
66
    {
67
        $msg = $this->amqpChannel->basic_get($queue, $no_ack = false);
68
        if ($msg instanceof AMQPMessage) {
69
            $res = new AMQPMessage();
70
            return $res;
71
        }
72
73
        throw new InvalidValueException("response was not a valid amqp message object");
74
    }
75
76
    public function getMessageConsume($queue, Callable $callback, Array $options = [])
77
    {
78
        return $this->amqpChannel->basic_consume(
79
            $queue = '',
80
            $consumer_tag = ArrayHelper::getValue($options, 'consumer_tag', ''),
81
            $no_local = ArrayHelper::getValue($options, 'no_local', false),
82
            $no_ack = ArrayHelper::getValue($options, 'no_ack', false),
83
            $exclusive = ArrayHelper::getValue($options, 'exclusive', false),
84
            $nowait = ArrayHelper::getValue($options, 'nowait', false),
85
            $callback,
86
            $ticket = null,
87
            $arguments = array()
88
        );
89
90
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
        while (count($this->channel->callbacks)) {
92
            $this->channel->wait();
93
        }
94
        */
95
96
    }
97
98
    public function declareExchange($name, $type, $passive = false, $durable = false, $auto_delete = true, $internal = false)
99
    {
100
        return $this->amqpChannel->exchange_declare($name, $type, $passive, $durable, $auto_delete, $internal);
101
    }
102
103
    public function deleteExchange($name, $if_unused = false, $if_empty = false, $nowait = false)
104
    {
105
        return $this->amqpChannel->exchange_delete($name, $if_unused, $if_empty, $nowait);
106
    }
107
108
    public function declareQueue($name, $passive = false, $durable = false, $auto_delete = true, $internal = false)
109
    {
110
        return $this->amqpChannel->queue_declare($name, $passive, $durable, $auto_delete, $internal);
111
    }
112
113
    public function deleteQueue($name, $if_unused = false, $if_empty = false, $nowait = false)
114
    {
115
        return $this->amqpChannel->queue_delete($name, $if_unused, $if_empty, $nowait);
116
    }
117
118
    public function purgeQueue($name, $nowait = false)
119
    {
120
        return $this->amqpChannel->queue_purge($name, $nowait);
121
    }
122
123
    public function bindQueueExchange($queue, $exchange, $routing_key)
124
    {
125
        return $this->amqpChannel->queue_bind($queue, $exchange, $routing_key);
126
    }
127
}
128