Test Failed
Pull Request — master (#39)
by Aleksandr
02:42
created

BatchExecuteReveiverStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace OldSound\RabbitMqBundle\ExecuteCallbackStrategy;
4
5
use OldSound\RabbitMqBundle\Declarations\QueueConsuming;
6
use PhpAmqpLib\Exception\AMQPTimeoutException;
7
use PhpAmqpLib\Message\AMQPMessage;
8
9
class BatchExecuteReveiverStrategy extends AbstractExecuteCallbackStrategy
0 ignored issues
show
Bug introduced by
The type OldSound\RabbitMqBundle\...ExecuteCallbackStrategy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
{
11
    /** @var int */
12
    private $batchCount;
13
    /** @var AMQPMessage[] */
14
    protected $messagesBatch = [];
15
16
    public function __construct(int $batchCount)
17
    {
18
        $this->batchCount = $batchCount;
19
    }
20
21
    public function canPrecessMultiMessages(): bool
22
    {
23
        return true;
24
    }
25
26
    public function consumeCallback(AMQPMessage $message)
27
    {
28
        $this->messagesBatch[$message->getDeliveryTag()] = $message;
29
30
        if ($this->isBatchCompleted()) {
31
            $this->proccessMessages($this->messagesBatch);
32
        }
33
    }
34
35
    public function onMessageProcessed(AMQPMessage $message)
36
    {
37
        unset($this->messagesBatch[array_search($message, $this->messagesBatch, true)]);
38
    }
39
40
    public function onCatchTimeout(AMQPTimeoutException $e)
41
    {
42
        if (!$this->isBatchEmpty()) {
43
            $this->proccessMessages($this->messagesBatch);
44
        }
45
    }
46
47
    public function onStopConsuming()
48
    {
49
        if (!$this->isBatchEmpty()) {
50
            $this->proccessMessages($this->messagesBatch);
51
        }
52
    }
53
54
    protected function isBatchCompleted(): bool
55
    {
56
        return count($this->messagesBatch) === $this->batchCount;
57
    }
58
59
    /**
60
     * @return  bool
61
     */
62
    protected function isBatchEmpty()
63
    {
64
        return count($this->messagesBatch) === 0;
65
    }
66
}