GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#249)
by Adrien
08:08
created

PhpAmqpDriver::peekQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 3
1
<?php
2
3
namespace Bernard\Driver;
4
5
use Bernard\Driver;
6
use PhpAmqpLib\Channel\AMQPChannel;
7
use PhpAmqpLib\Connection\AMQPStreamConnection;
8
use PhpAmqpLib\Message\AMQPMessage;
9
10
class PhpAmqpDriver implements Driver
11
{
12
    /**
13
     * @var AMQPStreamConnection
14
     */
15
    private $connection;
16
17
    /**
18
     * @var AMQPChannel
19
     */
20
    private $channel;
21
22
    /**
23
     * @var string
24
     */
25
    private $exchange;
26
    /**
27
     * @var array|null
28
     */
29
    private $defaultMessageParams;
30
31
    /**
32
     * @param AMQPStreamConnection $connection
33
     * @param string               $exchange
34
     * @param array                $defaultMessageParams
35
     */
36
    public function __construct(AMQPStreamConnection $connection, $exchange, array $defaultMessageParams = null)
37
    {
38
        $this->connection = $connection;
39
        $this->exchange = $exchange;
40
        $this->defaultMessageParams = $defaultMessageParams;
41
    }
42
43
    /**
44
     * Returns a list of all queue names.
45
     *
46
     * @return array
47
     */
48
    public function listQueues()
49
    {
50
    }
51
52
    /**
53
     * Create a queue.
54
     *
55
     * @param string $queueName
56
     */
57
    public function createQueue($queueName)
58
    {
59
        $channel = $this->getChannel();
60
        $channel->exchange_declare($this->exchange, 'direct', false, true, false);
61
        $channel->queue_declare($queueName, false, true, false, false);
62
        $channel->queue_bind($queueName, $this->exchange);
63
    }
64
65
    /**
66
     * Count the number of messages in queue. This can be a approximately number.
67
     *
68
     * @return int
69
     */
70
    public function countMessages($queueName)
71
    {
72
    }
73
74
    /**
75
     * Insert a message at the top of the queue.
76
     *
77
     * @param string $queueName
78
     * @param string $message
79
     */
80
    public function pushMessage($queueName, $message)
81
    {
82
        $amqpMessage = new AMQPMessage($message, $this->defaultMessageParams);
0 ignored issues
show
Bug introduced by
It seems like $this->defaultMessageParams can also be of type null; however, PhpAmqpLib\Message\AMQPMessage::__construct() does only seem to accept array, 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...
83
        $this->getChannel()->basic_publish($amqpMessage, $this->exchange);
84
    }
85
86
    /**
87
     * Remove the next message in line. And if no message is available
88
     * wait $interval seconds.
89
     *
90
     * @param string $queueName
91
     * @param int    $interval
92
     *
93
     * @return array An array like array($message, $receipt);
94
     */
95
    public function popMessage($queueName, $interval = 5)
96
    {
97
        $message = $this->getChannel()->basic_get($queueName);
98
        if (!$message) {
99
            // sleep for 10 ms to prevent hammering CPU
100
            usleep(10000);
101
102
            return [null, null];
103
        }
104
105
        return [$message->body, $message->get('delivery_tag')];
106
    }
107
108
    /**
109
     * If the driver supports it, this will be called when a message
110
     * have been consumed.
111
     *
112
     * @param string $queueName
113
     * @param mixed  $receipt
114
     */
115
    public function acknowledgeMessage($queueName, $receipt)
116
    {
117
        $this->getChannel()->basic_ack($receipt);
118
    }
119
120
    /**
121
     * Returns a $limit numbers of messages without removing them
122
     * from the queue.
123
     *
124
     * @param string $queueName
125
     * @param int    $index
126
     * @param int    $limit
127
     */
128
    public function peekQueue($queueName, $index = 0, $limit = 20)
129
    {
130
    }
131
132
    /**
133
     * Removes the queue.
134
     *
135
     * @param string $queueName
136
     */
137
    public function removeQueue($queueName)
138
    {
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function info()
145
    {
146
    }
147
148
    public function __destruct()
149
    {
150
        if (null !== $this->channel) {
151
            $this->channel->close();
152
        }
153
    }
154
155
    private function getChannel()
156
    {
157
        if (null === $this->channel) {
158
            $this->channel = $this->connection->channel();
159
        }
160
161
        return $this->channel;
162
    }
163
}
164