Passed
Push — 26-use-psr-14-event-dispatcher ( ab95da )
by Jakub
02:36
created

TesterListenerProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 17
c 1
b 0
f 1
dl 0
loc 43
ccs 14
cts 14
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getListenersForEvent() 0 14 4
A __construct() 0 3 1
A registerListener() 0 6 2
1
<?php
2
declare(strict_types=1);
3
4
namespace MyTester;
5
6
use Psr\EventDispatcher\ListenerProviderInterface;
7
8
final class TesterListenerProvider implements ListenerProviderInterface
9
{
10
    /** @var ITesterExtension[] */
11
    private array $extensions;
12
13
    /**
14
     * @var array<class-string, callable[]>
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<class-string, callable[]> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable[]>.
Loading history...
15
     */
16
    private array $listeners = [];
17
18
    /**
19
     * @param ITesterExtension[] $extensions
20
     */
21
    public function __construct(array $extensions)
22
    {
23 1
        $this->extensions = $extensions;
24 1
    }
25
26
    /**
27
     * @param class-string $className
28
     */
29
    public function registerListener(string $className, callable $callback): void
30
    {
31 1
        if (!array_key_exists($className, $this->listeners)) {
32 1
            $this->listeners[$className] = [];
33
        }
34 1
        $this->listeners[$className][] = $callback;
35 1
    }
36
37
    public function getListenersForEvent(object $event): iterable
38
    {
39 1
        $listeners = $this->listeners[$event::class] ?? [];
40 1
        foreach ($this->extensions as $extension) {
41 1
            switch ($event::class) {
42
                case Events\TestsStartedEvent::class:
43 1
                    $listeners = array_merge($listeners, $extension->getEventsPreRun());
44 1
                    break;
45
                case Events\TestsFinishedEvent::class:
46 1
                    $listeners = array_merge($listeners, $extension->getEventsAfterRun());
47 1
                    break;
48
            }
49
        }
50 1
        return $listeners;
51
    }
52
}
53