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

SourcesCompilerPass::process()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 14
cts 14
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 9
nop 1
crap 7
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