Completed
Push — master ( 66b3f3...5e7167 )
by Nikola
03:23
created

SourcesCompilerPass   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 54
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C process() 0 30 7
A getRequiredSources() 0 11 2
1
<?php
2
/*
3
 * This file is part of the Exchange Rate Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\ExchangeRate\DependencyInjection\CompilerPass;
11
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
15
use Symfony\Component\DependencyInjection\Reference;
16
17
/**
18
 * Class SourcesCompilerPass
19
 *
20
 * Compiler pass for sources
21
 *
22
 * @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection\CompilerPass
23
 */
24
class SourcesCompilerPass implements CompilerPassInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 3
    public function process(ContainerBuilder $container)
30
    {
31 3
        if ($container->hasDefinition('run_open_code.exchange_rate.registry.sources')) {
32
33 2
            $definition = $container->getDefinition('run_open_code.exchange_rate.registry.sources');
34
35 2
            $requiredSources = $this->getRequiredSources($container);
36
37 2
            foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.source') as $id => $tags) {
38
39 2
                foreach ($tags as $attributes) {
40
41
                    if (
42 2
                        !empty($attributes['name'])
43
                        &&
44 2
                        isset($requiredSources[$attributes['name']])
45
                    ) {
46 2
                        $definition->addMethodCall('add', [new Reference($id)]);
47 2
                        unset($requiredSources[$attributes['name']]);
48
49 2
                        continue 2;
50
                    }
51
                }
52
            }
53
54 2
            if (count($requiredSources) > 0) {
55 1
                throw new ServiceNotFoundException(reset($requiredSources));
56
            }
57
        }
58 2
    }
59
60
    /**
61
     * Get list of required sources services.
62
     *
63
     * @param ContainerBuilder $container
64
     * @return array
65
     */
66 2
    protected function getRequiredSources(ContainerBuilder $container)
67
    {
68 2
        $sources = [];
69
70 2
        foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.rate_configuration') as $id => $tags) {
71 2
            $source = $container->getDefinition($id)->getArgument(2);
72 2
            $sources[$source] = $source;
73
        }
74
75 2
        return $sources;
76
    }
77
}
78