Completed
Push — master ( ed369e...395bc6 )
by Daniel
04:23
created

EventBusConfiguration::configureEventBus()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 19
cts 19
cp 1
rs 9.2
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * Narrator Bundle
4
 *
5
 * @link      http://github.com/mleko/narrator-bundle
6
 * @copyright Copyright (c) 2017 Daniel Król
7
 * @license   MIT
8
 */
9
10
11
namespace Mleko\Narrator\Bundle\DependencyInjection\Configuration;
12
13
14
use Mleko\Narrator\BasicEventBus;
15
use Mleko\Narrator\ListenerResolver\InstanceOfResolver;
16
use Mleko\Narrator\ListenerResolver\NameBasedResolver;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
class EventBusConfiguration
22
{
23
    /**
24
     * @param $buses
25
     * @param ContainerBuilder $container
26
     */
27 3
    public function configureEventBus($buses, ContainerBuilder $container)
28
    {
29 3
        foreach ($buses as $busName => $busConfig) {
30 3
            $resolverServiceId = "narrator.event_bus.$busName.resolver";
31 3
            $resolverConfig = isset($busConfig['resolver']) ? $busConfig['resolver'] : [];
32 3
            $container->setDefinition(
33 3
                $resolverServiceId,
34 3
                $this->buildResolver($resolverConfig)
0 ignored issues
show
Bug introduced by
It seems like $this->buildResolver($resolverConfig) targeting Mleko\Narrator\Bundle\De...ration::buildResolver() can also be of type object<Symfony\Component...ncyInjection\Reference>; however, Symfony\Component\Depend...uilder::setDefinition() does only seem to accept object<Symfony\Component...cyInjection\Definition>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
35 3
            );
36
37 3
            $container->setDefinition(
38 3
                "narrator.event_bus.$busName",
39 3
                new Definition(
40 3
                    BasicEventBus::class,
41
                    [
42 3
                        new Reference($resolverServiceId),
43 3
                        []
44 3
                    ]
45 3
                )
46 3
            );
47 3
        }
48 3
    }
49
50 3
    private function buildResolver($resolverConfig)
51
    {
52 3
        $resolverType = isset($resolverConfig['type']) ? $resolverConfig['type'] : 'name';
53
        switch ($resolverType) {
54 3
            case 'instanceof':
55 1
                return new Definition(InstanceOfResolver::class);
56 3
            case 'service':
57
                return new Reference($resolverType['service_id']);
58 3
            default:
59 3
                $nameExtractorId = isset($resolverConfig['name_extractor']) ? $resolverConfig['name_extractor'] : "narrator.name_extractor.class_name";
60 3
                return new Definition(
61 3
                    NameBasedResolver::class,
62 3
                    [new Reference($nameExtractorId)]
63 3
                );
64 3
        }
65
    }
66
}
67