Completed
Push — master ( 9fa027...1fc283 )
by Andrew
02:18
created

Emitter::emit()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 18
rs 8.8571
cc 5
eloc 9
nc 7
nop 1
1
<?php namespace Cairns\Radiate;
2
3
use Cairns\Radiate\Inflector\MethodNameInflector;
4
5
final class Emitter
6
{
7
    private $inflector;
8
9
    private $listeners = [];
10
11
    public function __construct(MethodNameInflector $inflector)
12
    {
13
        $this->inflector = $inflector;
14
    }
15
16
    public function addListener($listener)
17
    {
18
        $this->listeners[] = $listener;
19
    }
20
21
    public function getListeners()
22
    {
23
        return $this->listeners;
24
    }
25
26
    public function emit($event)
27
    {
28
        foreach ($this->listeners as $listener) {
29
            $methods = $this->inflector->inflect($event, $listener);
30
31
            if (! is_array($methods)) {
32
                $methods = [$methods];
33
            }
34
35
            foreach ($methods as $method) {
36
                if (! method_exists($listener, $method)) {
37
                    continue;
38
                }
39
40
                $listener->{$method}($event);
41
            }
42
        }
43
    }
44
}
45