SimpleEventBus::publish()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3;
7
8
use Broadway\Domain\DomainEventStreamInterface;
9
10
/**
11
 * Class SimpleEventBus
12
 *
13
 * Extension of Broadway's SimpleEventBus with a configurable callback to be
14
 * executed before the first message is published. This callback can be used to
15
 * subscribe listeners.
16
 */
17
class SimpleEventBus extends \Broadway\EventHandling\SimpleEventBus
18
{
19
    private $first = true;
20
21
    /**
22
     * @var null|callable
23
     */
24
    private $beforeFirstPublicationCallback;
25
26
    /**
27
     * @param callable $callback
28
     */
29
    public function beforeFirstPublication($callback)
30
    {
31
        $this->beforeFirstPublicationCallback = $callback;
32
    }
33
34
    private function callBeforeFirstPublicationCallback()
35
    {
36
        if ($this->beforeFirstPublicationCallback) {
37
            $callback = $this->beforeFirstPublicationCallback;
38
            $callback($this);
39
        }
40
    }
41
42
    public function publish(DomainEventStreamInterface $domainMessages)
43
    {
44
        if ($this->first) {
45
            $this->first = false;
46
            $this->callBeforeFirstPublicationCallback();
47
        }
48
49
        parent::publish($domainMessages);
50
    }
51
}
52