forked from swarrot/swarrot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxMessagesProcessor.php
62 lines (52 loc) · 1.5 KB
/
MaxMessagesProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Swarrot\Processor\MaxMessages;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Swarrot\Broker\Message;
use Swarrot\Processor\ConfigurableInterface;
use Swarrot\Processor\ProcessorInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class MaxMessagesProcessor implements ConfigurableInterface
{
private $processor;
private $logger;
/**
* @var int
*/
private $messagesProcessed = 0;
public function __construct(ProcessorInterface $processor, LoggerInterface $logger = null)
{
$this->processor = $processor;
$this->logger = $logger ?: new NullLogger();
}
/**
* {@inheritdoc}
*/
public function process(Message $message, array $options): bool
{
$return = $this->processor->process($message, $options);
if (++$this->messagesProcessed >= $options['max_messages']) {
$this->logger->info(
'[MaxMessages] The maximum number of messages has been reached',
[
'max_messages' => $options['max_messages'],
'swarrot_processor' => 'max_messages',
]
);
return false;
}
return $return;
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'max_messages' => 100,
])
->setAllowedTypes('max_messages', 'int')
;
}
}