Passed
Push — refactor-setup-event-dispatche... ( 2ec724 )
by Chema
03:43
created

SetupEventDispatcher::getDispatcher()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Bootstrap;
6
7
use Gacela\Framework\Event\Dispatcher\ConfigurableEventDispatcher;
8
use Gacela\Framework\Event\Dispatcher\EventDispatcherInterface;
9
use Gacela\Framework\Event\Dispatcher\NullEventDispatcher;
10
11
final class SetupEventDispatcher
12
{
13 34
    public static function getDispatcher(SetupGacela $setupGacela): EventDispatcherInterface
14
    {
15 34
        if (!$setupGacela->canCreateEventDispatcher()) {
16 23
            return new NullEventDispatcher();
17
        }
18
19 11
        $dispatcher = new ConfigurableEventDispatcher();
20 11
        $dispatcher->registerGenericListeners($setupGacela->getGenericListeners() ?? []);
21
22 11
        foreach ($setupGacela->getSpecificListeners() ?? [] as $event => $listeners) {
23 5
            foreach ($listeners as $callable) {
24 5
                $dispatcher->registerSpecificListener($event, $callable);
25
            }
26
        }
27
28 11
        return $dispatcher;
29
    }
30
}
31