Passed
Pull Request — master (#84)
by Frank
07:12
created

TestedMessageConsumer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 4 2
A numberOfMessagesProcessed() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing\TestUtilities\TestingMessageConsumers;
6
7
use EventSauce\EventSourcing\Message;
8
use EventSauce\EventSourcing\MessageConsumer;
9
use LogicException;
10
11
class TestedMessageConsumer implements MessageConsumer
12
{
13
    /**
14
     * @var int
15
     */
16
    private $failAfterNumberOfMessages;
17
18
    /**
19
     * @var int
20
     */
21
    private $numberOfMessagesProcessed = 0;
22
23
    public function __construct(int $failAfterNumberOfMessages)
24
    {
25
        $this->failAfterNumberOfMessages = $failAfterNumberOfMessages;
26
    }
27
28
    public function handle(Message $message): void
29
    {
30
        if (++$this->numberOfMessagesProcessed === $this->failAfterNumberOfMessages) {
31
            throw new LogicException('Too many messages');
32
        }
33
    }
34
35
    public function numberOfMessagesProcessed(): int
36
    {
37
        return $this->numberOfMessagesProcessed;
38
    }
39
}
40