Completed
Push — master ( fcbd63...720fe1 )
by Peter
03:37
created

Bus   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 45
ccs 13
cts 13
cp 1
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 3
    public function __construct(LocatorInterface $locator)
27
    {
28 3
        $this->locator = $locator;
29 3
    }
30
31
    /**
32
     * Publishes the event $event to every EventListener that wants to.
33
     *
34
     * @param EventInterface $event
35
     */
36 2
    public function publish(EventInterface $event)
37
    {
38 2
        foreach ($this->locator->getListenersForEvent($event) as $listener) {
39 2
            $listener->handle($event);
40
        }
41 2
    }
42
43
    /**
44
     * @param AggregateEventsInterface $aggregator
45
     */
46 1
    public function pullAndPublish(AggregateEventsInterface $aggregator)
47
    {
48 1
        foreach ($aggregator->pullEvents() as $event) {
49 1
            $this->publish($event);
50
        }
51 1
    }
52
53
    /**
54
     * @return ListenerCollection
55
     */
56 1
    public function getRegisteredEventListeners()
57
    {
58 1
        return $this->locator->getRegisteredEventListeners();
59
    }
60
}
61