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.

BatchProducer::produce()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 8.5906
cc 6
eloc 13
nc 10
nop 1
1
<?php
2
3
namespace AmqpWorkers;
4
5
use PhpAmqpLib\Connection\AbstractConnection;
6
7
/**
8
 * BatchProducer treats given message as a list of messages and send them to RabbitMQ channel
9
 *
10
 * @package AmqpWorkers
11
 * @author Alex Panshin <[email protected]>
12
 * @since 1.0
13
 */
14
class BatchProducer extends Producer
15
{
16
    private $limit = 0;
17
    private $queued = 0;
18
19
    public static function factory(AbstractConnection $connection, $limit = 100)
20
    {
21
        /** @var BatchProducer $producer */
22
        $producer = parent::factory($connection);
23
        $producer->limit = $limit;
24
25
        return $producer;
26
    }
27
28
    /**
29
     * @param array|\Traversable $messages
30
     * @throws \AmqpWorkers\Exception\ProducerNotProperlyConfigured
31
     */
32
    public function produce($messages)
33
    {
34
        $this->selfCheck();
35
36
        $channel = $this->getChannel();
37
        foreach ($messages as $payload) {
38
            if ($this->isExchange()) {
39
                $channel->batch_basic_publish($this->createMessage($payload), $this->exchange->getName());
40
            } else {
41
                $channel->batch_basic_publish($this->createMessage($payload), '', $this->queue->getName());
42
            }
43
44
            $this->queued++;
45
46
            if ($this->limit && $this->limit <= $this->queued) {
47
                $this->flush();
48
            }
49
        }
50
51
        if ($this->queued) {
52
            $this->flush();
53
        }
54
    }
55
56
    protected function flush()
57
    {
58
        $this->getChannel()->publish_batch();
59
        $this->queued = 0;
60
    }
61
}
62