MessageDispatchingEventDispatcher::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing;
6
7
class MessageDispatchingEventDispatcher implements EventDispatcher
8
{
9
    private MessageDispatcher $dispatcher;
10
    private MessageDecorator $decorator;
11
12
    public function __construct(MessageDispatcher $dispatcher, ?MessageDecorator $decorator = null)
13
    {
14
        $this->dispatcher = $dispatcher;
15
        $this->decorator = $decorator ?: new DefaultHeadersDecorator();
16
    }
17
18
    public function dispatch(object ...$events): void
19 2
    {
20
        $this->dispatchWithHeaders([], ...$events);
21 2
    }
22 2
23 2
    public function dispatchWithHeaders(array $headers, object ...$events): void
24
    {
25 1
        $messages = [];
26
27 1
        foreach ($events as $event) {
28 1
            $messages[] = $this->decorator->decorate(new Message($event, $headers));
29
        }
30 2
31
        $this->dispatcher->dispatch(...$messages);
32 2
    }
33
}
34