Completed
Push — master ( d627e9...5e8af2 )
by Peter
57:14 queued 45:06
created

GpsLabDomainEventExtension::busRealName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Bundle\DomainEvent\DependencyInjection;
11
12
use GpsLab\Domain\Event\Bus\EventBus;
13
use GpsLab\Domain\Event\Queue\EventQueue;
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Loader;
17
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18
19
class GpsLabDomainEventExtension extends Extension
20
{
21
    /**
22
     * @param array $configs
23
     * @param ContainerBuilder $container
24
     */
25
    public function load(array $configs, ContainerBuilder $container)
26
    {
27
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
28
        $loader->load('queue.yml');
29
        $loader->load('bus.yml');
30
        $loader->load('locator.yml');
31
        $loader->load('publisher.yml');
32
33
        $config = $this->processConfiguration(new Configuration(), $configs);
34
35
        $container->setAlias('domain_event.bus', $this->busRealName($config['bus']));
36
        $container->setAlias('domain_event.queue', $this->queueRealName($config['queue']));
37
        $container->setAlias('domain_event.locator', $this->locatorRealName($config['locator']));
38
        $container->setAlias(EventBus::class, $this->busRealName($config['bus']));
39
        $container->setAlias(EventQueue::class, $this->queueRealName($config['queue']));
40
41
        $container->getDefinition('domain_event.publisher')->replaceArgument(2, $config['publish_on_flush']);
42
    }
43
44
    /**
45
     * @param string $name
46
     *
47
     * @return string
48
     */
49
    private function busRealName($name)
50
    {
51
        if (in_array($name, ['listener_located', 'queue'])) {
52
            return 'domain_event.bus.'.$name;
53
        }
54
55
        return $name;
56
    }
57
58
    /**
59
     * @param string $name
60
     *
61
     * @return string
62
     */
63
    private function queueRealName($name)
64
    {
65
        if (in_array($name, ['pull_memory', 'subscribe_executing'])) {
66
            return 'domain_event.queue.'.$name;
67
        }
68
69
        return $name;
70
    }
71
72
    /**
73
     * @param string $name
74
     *
75
     * @return string
76
     */
77
    private function locatorRealName($name)
78
    {
79
        if (in_array($name, ['direct_binding', 'container', 'symfony'])) {
80
            return 'domain_event.locator.'.$name;
81
        }
82
83
        return $name;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getAlias()
90
    {
91
        return 'gpslab_domain_event';
92
    }
93
}
94