MessageDispatcher   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 7 3
A __construct() 0 5 1
1
<?php
2
3
namespace Chocofamily\LaravelEventSauce;
4
5
use EventSauce\EventSourcing\Message;
6
use EventSauce\EventSourcing\MessageDispatcher as EventSauceMessageDispatcher;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
9
final class MessageDispatcher implements EventSauceMessageDispatcher
10
{
11
    /** @var array */
12
    private $consumers;
13
14
    /** @var string */
15
    private $handlerClass;
16
17
    public function __construct(string $handlerClass, array $consumers)
18
    {
19
        $this->handlerClass = $handlerClass;
20
21
        $this->consumers = $consumers;
22
    }
23
24
    public function dispatch(Message ...$messages)
25
    {
26
        foreach ($this->consumers as $consumer) {
27
            if (is_a($consumer, ShouldQueue::class, true)) {
28
                dispatch(new $this->handlerClass($consumer, ...$messages));
29
            } else {
30
                dispatch_now(new $this->handlerClass($consumer, ...$messages));
31
            }
32
        }
33
    }
34
}
35