bernardphp /
bernard
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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
|
|||
| 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 | $runtime = microtime(true) + $duration; |
|
| 103 | |||
| 104 | 2 | while (microtime(true) < $runtime) { |
|
| 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 | // sleep for 10 ms to prevent hammering CPU |
||
| 112 | 1 | usleep(10000); |
|
| 113 | 1 | } |
|
| 114 | |||
| 115 | 1 | return [null, null]; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * If the driver supports it, this will be called when a message |
||
| 120 | * have been consumed. |
||
| 121 | * |
||
| 122 | * @param string $queueName |
||
| 123 | * @param mixed $receipt |
||
| 124 | */ |
||
| 125 | 1 | public function acknowledgeMessage($queueName, $receipt) |
|
| 126 | { |
||
| 127 | 1 | $this->getChannel()->basic_ack($receipt); |
|
| 128 | 1 | } |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Returns a $limit numbers of messages without removing them |
||
| 132 | * from the queue. |
||
| 133 | * |
||
| 134 | * @param string $queueName |
||
| 135 | * @param int $index |
||
| 136 | * @param int $limit |
||
| 137 | */ |
||
| 138 | public function peekQueue($queueName, $index = 0, $limit = 20) |
||
| 139 | { |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Removes the queue. |
||
| 144 | * |
||
| 145 | * @param string $queueName |
||
| 146 | */ |
||
| 147 | public function removeQueue($queueName) |
||
| 148 | { |
||
| 149 | $this->getChannel()->queue_delete($queueName); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | public function info() |
||
| 156 | { |
||
| 157 | } |
||
| 158 | |||
| 159 | 1 | public function __destruct() |
|
| 160 | { |
||
| 161 | 1 | if (null !== $this->channel) { |
|
| 162 | $this->channel->close(); |
||
| 163 | } |
||
| 164 | 1 | } |
|
| 165 | |||
| 166 | 6 | private function getChannel() |
|
| 167 | { |
||
| 168 | 6 | if (null === $this->channel) { |
|
| 169 | 6 | $this->channel = $this->connection->channel(); |
|
| 170 | 6 | } |
|
| 171 | |||
| 172 | 6 | return $this->channel; |
|
| 173 | } |
||
| 174 | } |
||
| 175 |
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.