GpsLabDomainEventExtension   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 0
loc 84
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 27 2
A busRealName() 0 8 2
A queueRealName() 0 8 2
A locatorRealName() 0 8 2
A getAlias() 0 4 1
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\Listener\Subscriber;
14
use GpsLab\Domain\Event\Queue\EventQueue;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Loader;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
20
class GpsLabDomainEventExtension extends Extension
21
{
22
    /**
23
     * @param array            $configs
24
     * @param ContainerBuilder $container
25
     */
26 4
    public function load(array $configs, ContainerBuilder $container)
27
    {
28 4
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
29 4
        $loader->load('queue.yml');
30 4
        $loader->load('bus.yml');
31 4
        $loader->load('locator.yml');
32 4
        $loader->load('publisher.yml');
33
34 4
        $config = $this->processConfiguration(new Configuration(), $configs);
35
36 4
        $container->setAlias('domain_event.bus', $this->busRealName($config['bus']));
37 4
        $container->setAlias('domain_event.queue', $this->queueRealName($config['queue']));
38 4
        $container->setAlias('domain_event.locator', $this->locatorRealName($config['locator']));
39 4
        $container->setAlias(EventBus::class, $this->busRealName($config['bus']));
40 4
        $container->setAlias(EventQueue::class, $this->queueRealName($config['queue']));
41
42 4
        $container->getDefinition('domain_event.publisher')->replaceArgument(2, $config['publish_on_flush']);
43
44
        // subscribers tagged automatically
45 4
        if (method_exists($container, 'registerForAutoconfiguration')) {
46
            $container
47 4
                ->registerForAutoconfiguration(Subscriber::class)
48 4
                ->addTag('domain_event.subscriber')
49 4
                ->setAutowired(true)
50
            ;
51
        }
52 4
    }
53
54
    /**
55
     * @param string $name
56
     *
57
     * @return string
58
     */
59 4
    private function busRealName($name)
60
    {
61 4
        if (in_array($name, ['listener_located', 'queue'])) {
62 3
            return 'domain_event.bus.'.$name;
63
        }
64
65 1
        return $name;
66
    }
67
68
    /**
69
     * @param string $name
70
     *
71
     * @return string
72
     */
73 4
    private function queueRealName($name)
74
    {
75 4
        if (in_array($name, ['pull_memory', 'subscribe_executing'])) {
76 3
            return 'domain_event.queue.'.$name;
77
        }
78
79 1
        return $name;
80
    }
81
82
    /**
83
     * @param string $name
84
     *
85
     * @return string
86
     */
87 4
    private function locatorRealName($name)
88
    {
89 4
        if (in_array($name, ['direct_binding', 'container', 'symfony'])) {
90 3
            return 'domain_event.locator.'.$name;
91
        }
92
93 1
        return $name;
94
    }
95
96
    /**
97
     * @return string
98
     */
99 1
    public function getAlias()
100
    {
101 1
        return 'gpslab_domain_event';
102
    }
103
}
104