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

DecoratingMessageDispatcher   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A dispatch() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing;
6
7
class DecoratingMessageDispatcher implements MessageDispatcher
8
{
9
    /**
10
     * @var MessageDispatcher
11
     */
12
    private $dispatcher;
13
14
    /**
15
     * @var MessageDecorator
16
     */
17
    private $decorator;
18
19
    public function __construct(MessageDispatcher $dispatcher, MessageDecorator $decorator = null)
20
    {
21
        $this->dispatcher = $dispatcher;
22
        $this->decorator = $decorator ?: new DefaultHeadersDecorator();
23
    }
24
25
    public function dispatch(Message ...$messages): void
26
    {
27
        $messages = array_map(fn (Message $message) => $this->decorator->decorate($message), $messages);
28
        $this->dispatcher->dispatch(...$messages);
29
    }
30
}
31