Completed
Push — master ( 223a4c...fcbd63 )
by Peter
02:34
created

Bus   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 45
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A publish() 0 6 2
A pullAndPublish() 0 6 2
A getRegisteredEventListeners() 0 4 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
namespace GpsLab\Domain\Event\Bus;
10
11
use GpsLab\Domain\Event\Aggregator\AggregateEventsInterface;
12
use GpsLab\Domain\Event\EventInterface;
13
use GpsLab\Domain\Event\Listener\ListenerCollection;
14
use GpsLab\Domain\Event\Listener\Locator\LocatorInterface;
15
16
class Bus implements BusInterface
17
{
18
    /**
19
     * @var LocatorInterface
20
     */
21
    private $locator;
22
23
    /**
24
     * @param LocatorInterface $locator
25
     */
26
    public function __construct(LocatorInterface $locator)
27
    {
28
        $this->locator = $locator;
29
    }
30
31
    /**
32
     * Publishes the event $event to every EventListener that wants to.
33
     *
34
     * @param EventInterface $event
35
     */
36
    public function publish(EventInterface $event)
37
    {
38
        foreach ($this->locator->getListenersForEvent($event) as $listener) {
39
            $listener->handle($event);
40
        }
41
    }
42
43
    /**
44
     * @param AggregateEventsInterface $aggregator
45
     */
46
    public function pullAndPublish(AggregateEventsInterface $aggregator)
47
    {
48
        foreach ($aggregator->pullEvents() as $event) {
49
            $this->publish($event);
50
        }
51
    }
52
53
    /**
54
     * @return ListenerCollection
55
     */
56
    public function getRegisteredEventListeners()
57
    {
58
        return $this->locator->getRegisteredEventListeners();
59
    }
60
}
61