Completed
Push — master ( 42f99d...2335a8 )
by Nikola
18:20 queued 03:48
created

SourcesCompilerPass::getRequiredSources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace RunOpenCode\Bundle\ExchangeRate\DependencyInjection\CompilerPass;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * Class SourcesCompilerPass
12
 *
13
 * @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection\CompilerPass
14
 */
15
class SourcesCompilerPass implements CompilerPassInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function process(ContainerBuilder $container)
21
    {
22
        if ($container->hasDefinition('run_open_code.exchange_rate.registry.sources')) {
23
24
            $definition = $container->getDefinition('run_open_code.exchange_rate.registry.sources');
25
26
            $requiredSources = $this->getRequiredSources($container);
27
28
            $availableSources = array_merge(
29
                $this->getRegisteredContainerSources($container),
30
                $this->getRegisteredSimpleSources($container)
31
            );
32
33
            if (count($availableSources) === 0) {
34
                throw new \RuntimeException('There is no available exchange rate source service registered in Service Container.');
35
            }
36
37
38
            foreach ($requiredSources as $requiredSource) {
39
40
                if (array_key_exists($requiredSource, $availableSources)) {
41
42
                    $definition->addMethodCall('add', array(
43
                        new Reference($availableSources[$requiredSource])
44
                    ));
45
                } else {
46
                    throw new \RuntimeException(sprintf('Required source "%s" is not available in Service Container.', $requiredSource));
47
                }
48
            }
49
        }
50
    }
51
52
    protected function getRequiredSources(ContainerBuilder $container)
53
    {
54
        $rates = $container->getParameter('run_open_code.exchange_rate.registered_rates');
55
56
        return array_unique(array_map(function($rateConfiguration) {
57
            return $rateConfiguration['source'];
58
        }, $rates));
59
    }
60
61
    protected function getRegisteredContainerSources(ContainerBuilder $container)
62
    {
63
        $availableSources = array();
64
65
        foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.source') as $id => $tags) {
66
67
            foreach ($tags as $attributes) {
68
69
                if (!empty($attributes['alias'])) {
70
                    $availableSources[$attributes['alias']] = $id;
71
                    continue;
72
                }
73
            }
74
        }
75
76
        return $availableSources;
77
    }
78
79
    protected function getRegisteredSimpleSources(ContainerBuilder $container)
80
    {
81
        /**
82
         * @var array $sources
83
         */
84
        if ($container->hasParameter('run_open_code.exchange_rate.source.registered_simple_sources')) {
85
86
            $availableSources = array();
87
88
            foreach ($container->getParameter('run_open_code.exchange_rate.source.registered_simple_sources') as $key => $class) {
89
90
                $definition = new Definition($class);
91
92
                $definition
93
                    ->setPublic(false)
94
                    ->addTag('run_open_code.exchange_rate.source', array(
95
                        'alias' => $key
96
                    ));
97
98
                $container->setDefinition(($id = sprintf('run_open_code.exchange_rate.source.simple.%s', $key)), $definition);
99
100
                $availableSources[$key] = $id;
101
            }
102
103
            return $availableSources;
104
        }
105
106
        return array();
107
    }
108
}
109