Completed
Push — master ( ab6445...ad7ac0 )
by Oscar
01:18
created

ListenerProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 24
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A listen() 0 8 1
A getListenersForEvent() 0 10 3
1
<?php
2
3
namespace SimpleCrud\EventDispatcher;
4
5
use Psr\EventDispatcher\ListenerProviderInterface;
6
7
class ListenerProvider implements ListenerProviderInterface
8
{
9
    private $listeners = [];
10
11
    public function listen(string $type, callable $callback): self
12
    {
13
        $listeners = $this->listeners[$type] ?? [];
14
        $listeners[] = $callback;
15
        $this->listeners[$type] = $listeners;
16
17
        return $this;
18
    }
19
20
    public function getListenersForEvent(object $event): iterable
21
    {
22
        foreach ($this->listeners as $type => $listeners) {
23
            if ($event instanceof $type) {
24
                return $listeners;
25
            }
26
        }
27
28
        return [];
29
    }
30
}
31