EventBus::handleListenersForEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace PhpDDD\Domain\Event\Bus;
4
5
use PhpDDD\Domain\Event\EventInterface;
6
use PhpDDD\Domain\Event\Listener\EventListenerCollection;
7
use PhpDDD\Domain\Event\Listener\EventListenerInterface;
8
use PhpDDD\Domain\Event\Listener\Locator\EventListenerLocatorInterface;
9
10
/**
11
 * Implementation of EventBusInterface.
12
 */
13
class EventBus implements EventBusInterface
14
{
15
    /**
16
     * @var EventListenerLocatorInterface
17
     */
18
    private $locator;
19
20
    /**
21
     * @param EventListenerLocatorInterface $locator
22
     */
23
    public function __construct(EventListenerLocatorInterface $locator)
24
    {
25
        $this->locator = $locator;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function publish(EventInterface $event)
32
    {
33
        $listeners = $this->locator->getEventListenersForEvent($event);
34
35
        return $this->handleListenersForEvent($event, $listeners);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getRegisteredEventListeners()
42
    {
43
        return $this->locator->getRegisteredEventListeners();
44
    }
45
46
    /**
47
     * @param EventInterface                                   $event
48
     * @param EventListenerCollection|EventListenerInterface[] $listeners
49
     *
50
     * @return array
51
     */
52
    private function handleListenersForEvent(EventInterface $event, $listeners)
53
    {
54
        $return = array();
55
56
        foreach ($listeners as $listener) {
57
            $return[] = $listener->handle($event);
58
        }
59
60
        return $return;
61
    }
62
}
63