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 (#388)
by Ben
04:21
created

Driver   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 167
Duplicated Lines 1.8 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 53.73%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 3
loc 167
ccs 36
cts 67
cp 0.5373
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A listQueues() 0 3 1
A createQueue() 0 7 1
A countMessages() 0 6 1
A pushMessage() 0 5 1
A popMessage() 3 18 4
A acknowledgeMessage() 0 4 1
A peekQueue() 0 3 1
A removeQueue() 0 4 1
A info() 0 3 1
A __destruct() 0 6 2
A getChannel() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bernard\Driver\Amqp;
4
5
use PhpAmqpLib\Channel\AMQPChannel;
6
use PhpAmqpLib\Connection\AbstractConnection;
7
use PhpAmqpLib\Message\AMQPMessage;
8
9
final class Driver implements \Bernard\Driver
10
{
11
    /**
12
     * @var AbstractConnection
13
     */
14
    private $connection;
15
16
    /**
17
     * @var AMQPChannel
18
     */
19
    private $channel;
20
21
    /**
22
     * @var string
23
     */
24
    private $exchange;
25
26
    /**
27
     * @var array|null
28
     */
29
    private $defaultMessageParams;
30
31
    /**
32
     * @param AbstractConnection $connection
33
     * @param string             $exchange
34
     * @param array              $defaultMessageParams
35
     */
36 7
    public function __construct(AbstractConnection $connection, $exchange, array $defaultMessageParams = null)
37
    {
38 7
        $this->connection = $connection;
39 7
        $this->exchange = $exchange;
40 7
        $this->defaultMessageParams = $defaultMessageParams;
41 7
    }
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 1
    public function createQueue($queueName)
58
    {
59 1
        $channel = $this->getChannel();
60 1
        $channel->exchange_declare($this->exchange, 'direct', false, true, false);
61 1
        $channel->queue_declare($queueName, false, true, false, false);
62 1
        $channel->queue_bind($queueName, $this->exchange, $queueName);
63 1
    }
64
65
    /**
66
     * Count the number of messages in queue. This can be a approximately number.
67
     *
68
     * @param string $queueName
69
     *
70
     * @return int
71
     */
72
    public function countMessages($queueName)
73
    {
74
        list(, $messageCount) = $this->getChannel()->queue_declare($queueName, true);
75
76
        return $messageCount;
77
    }
78
79
    /**
80
     * Insert a message at the top of the queue.
81
     *
82
     * @param string $queueName
83
     * @param string $message
84
     */
85 2
    public function pushMessage($queueName, $message)
86
    {
87 2
        $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...
88 2
        $this->getChannel()->basic_publish($amqpMessage, $this->exchange, $queueName);
89 2
    }
90
91
    /**
92
     * Remove the next message in line. And if no message is available
93
     * wait $duration seconds.
94
     *
95
     * @param string $queueName
96
     * @param int    $duration
97
     *
98
     * @return array An array like array($message, $receipt);
99
     */
100 2
    public function popMessage($queueName, $duration = 5)
101
    {
102 2
        $startAt = microtime(true);
103
104 2
        while (true) {
105 2
            $message = $this->getChannel()->basic_get($queueName);
106
107 2
            if ($message) {
108 1
                return [$message->body, $message->get('delivery_tag')];
109
            }
110
111 1
            usleep(10000);
112
113 1 View Code Duplication
            if ((microtime(true) - $startAt) >= $duration) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114 1
                return [null, null];
115
            }
116 1
        }
117
    }
118
119
    /**
120
     * If the driver supports it, this will be called when a message
121
     * have been consumed.
122
     *
123
     * @param string $queueName
124
     * @param mixed  $receipt
125
     */
126 1
    public function acknowledgeMessage($queueName, $receipt)
127
    {
128 1
        $this->getChannel()->basic_ack($receipt);
129 1
    }
130
131
    /**
132
     * Returns a $limit numbers of messages without removing them
133
     * from the queue.
134
     *
135
     * @param string $queueName
136
     * @param int    $index
137
     * @param int    $limit
138
     */
139
    public function peekQueue($queueName, $index = 0, $limit = 20)
140
    {
141
    }
142
143
    /**
144
     * Removes the queue.
145
     *
146
     * @param string $queueName
147
     */
148
    public function removeQueue($queueName)
149
    {
150
        $this->getChannel()->queue_delete($queueName);
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function info()
157
    {
158
    }
159
160 1
    public function __destruct()
161
    {
162 1
        if (null !== $this->channel) {
163
            $this->channel->close();
164
        }
165 1
    }
166
167 6
    private function getChannel()
168
    {
169 6
        if (null === $this->channel) {
170 6
            $this->channel = $this->connection->channel();
171 6
        }
172
173 6
        return $this->channel;
174
    }
175
}
176