MessageDispatcher::dispatch()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
rs 10
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