Completed
Push — master ( 9bf5de...63a510 )
by Peter
22:57
created

EventBus::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Domain\Event\Bus;
11
12
use GpsLab\Domain\Event\Aggregator\AggregateEventsInterface;
13
use GpsLab\Domain\Event\EventInterface;
14
use GpsLab\Domain\Event\Listener\ListenerCollection;
15
use GpsLab\Domain\Event\Listener\ListenerInterface;
16
use GpsLab\Domain\Event\Listener\Locator\LocatorInterface;
17
18
class EventBus implements EventBusInterface
19
{
20
    /**
21
     * @var LocatorInterface
22
     */
23
    private $locator;
24
25
    /**
26
     * @param LocatorInterface $locator
27
     */
28
    public function __construct(LocatorInterface $locator)
29
    {
30
        $this->locator = $locator;
31
    }
32
33
    /**
34
     * Publishes the event $event to every EventListener that wants to.
35
     *
36
     * @param EventInterface $event
37
     */
38
    public function publish(EventInterface $event)
39
    {
40
        foreach ($this->locator->getListenersForEvent($event) as $listener) {
41
            $listener->handle($event);
42
        }
43
    }
44
45
    /**
46
     * @param AggregateEventsInterface $aggregator
47
     */
48
    public function pullAndPublish(AggregateEventsInterface $aggregator)
49
    {
50
        foreach ($aggregator->pullEvents() as $event) {
51
            $this->publish($event);
52
        }
53
    }
54
55
    /**
56
     * @return ListenerInterface[]|ListenerCollection
57
     */
58
    public function getRegisteredEventListeners()
59
    {
60
        return $this->locator->getRegisteredEventListeners();
61
    }
62
}
63