Completed
Push — master ( 4d50ae...84674b )
by Tobias
06:42
created

StoragePass::process()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 30.4863

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 5
cts 23
cp 0.2174
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 10
nop 1
crap 30.4863
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\DependencyInjection\CompilerPass;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
/**
20
 * Register all storages in the StorageService.
21
 *
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
class StoragePass implements CompilerPassInterface
25
{
26
    /**
27
     * @var Definition[]
28
     */
29
    private $definitions;
30
31 1
    public function process(ContainerBuilder $container)
32
    {
33 1
        $services = $container->findTaggedServiceIds('php_translation.storage');
34 1
        foreach ($services as $id => $tags) {
35
            foreach ($tags as $tag) {
36
                if (!isset($tag['name'])) {
37
                    $tag['name'] = 'default';
38
                }
39
                if (!isset($tag['type'])) {
40
                    throw new \LogicException('The tag "php_translation.storage" must have a "type".');
41
                }
42
43
                $def = $this->getDefinition($container, $tag['name']);
44
                switch ($tag['type']) {
45
                    case 'remote':
46
                        $def->addMethodCall('addRemoteStorage', [new Reference($id)]);
47
                        break;
48
                    case 'local':
49
                        $def->addMethodCall('addLocalStorage', [new Reference($id)]);
50
                        break;
51
                    default:
52
                        throw new \LogicException(sprintf('The tag "php_translation.storage" must have a "type" of value "local" or "remote". Value "%s" was provided', $tag['type']));
53
                }
54
            }
55 1
        }
56 1
    }
57
58
    /**
59
     * @param ContainerBuilder $container
60
     *
61
     * @return Definition
62
     */
63
    private function getDefinition(ContainerBuilder $container, $name)
64
    {
65
        if (!isset($this->definitions[$name])) {
66
            $this->definitions[$name] = $container->getDefinition('php_translation.storage.'.$name);
67
        }
68
69
        return $this->definitions[$name];
70
    }
71
}
72