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

ListenerProvider::listen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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