AsyncEventBus   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 11 4
A __construct() 0 8 1
1
<?php
2
3
/*
4
 * event-async (https://github.com/phpgears/event-async).
5
 * Async decorator for Event bus.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event-async
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\Event\Async;
15
16
use Gears\Event\Async\Discriminator\EventDiscriminator;
17
use Gears\Event\Event;
18
use Gears\Event\EventBus;
19
20
class AsyncEventBus implements EventBus
21
{
22
    /**
23
     * Wrapped event bus.
24
     *
25
     * @var EventBus
26
     */
27
    private $wrappedEventBus;
28
29
    /**
30
     * Event queue.
31
     *
32
     * @var EventQueue
33
     */
34
    private $queue;
35
36
    /**
37
     * Event discriminator.
38
     *
39
     * @var EventDiscriminator
40
     */
41
    private $discriminator;
42
43
    /**
44
     * AsyncEventBus constructor.
45
     *
46
     * @param EventBus           $wrappedEventBus
47
     * @param EventQueue         $queue
48
     * @param EventDiscriminator $discriminator
49
     */
50
    public function __construct(
51
        EventBus $wrappedEventBus,
52
        EventQueue $queue,
53
        EventDiscriminator $discriminator
54
    ) {
55
        $this->wrappedEventBus = $wrappedEventBus;
56
        $this->discriminator = $discriminator;
57
        $this->queue = $queue;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @throws \Gears\Event\Async\Exception\EventQueueException
64
     */
65
    final public function dispatch(Event $event): void
66
    {
67
        if (!$event instanceof ReceivedEvent && $this->discriminator->shouldEnqueue($event)) {
68
            $this->queue->send($event);
69
        }
70
71
        if ($event instanceof ReceivedEvent) {
72
            $event = $event->getOriginalEvent();
73
        }
74
75
        $this->wrappedEventBus->dispatch($event);
76
    }
77
}
78