EventDispatcherFactory::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 23
ccs 16
cts 16
cp 1
crap 4
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Event\Container;
6
7
use Antidot\Event\EventDispatcher;
8
use Antidot\Event\ListenerProvider;
9
use Psr\Container\ContainerInterface;
10
use Psr\EventDispatcher\EventDispatcherInterface;
11
use RuntimeException;
12
use Throwable;
13
14
class EventDispatcherFactory
15
{
16 4
    public function __invoke(ContainerInterface $container): EventDispatcherInterface
17
    {
18
        try {
19 4
            $config = $container->get('config')['app-events'];
20 3
            $listenerProvider = new ListenerProvider();
21 3
            foreach ($config['event-listeners'] ?? [] as $eventClass => $listeners) {
22 2
                foreach ($listeners ?? [] as $listenerId) {
23 1
                    $listenerProvider->addListener(
24 1
                        $eventClass,
25 1
                        static function () use ($container, $listenerId): callable {
26 1
                            return $container->get($listenerId);
27 1
                        }
28
                    );
29
                }
30
            }
31
32 3
            return new EventDispatcher($listenerProvider);
33 1
        } catch (Throwable $exception) {
34 1
            throw new RuntimeException(sprintf(
35
                'Something went wrong constructing an instance of %s, review config related to \'app-events\''
36 1
                . ' and see the previous exception for more info.',
37 1
                EventDispatcher::class
38 1
            ), 0, $exception);
39
        }
40
    }
41
}
42