Completed
Push — master ( 52d799...bb7198 )
by Tobias
13:11
created

StoragePass::process()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7.5034

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 18
cts 23
cp 0.7826
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 10
nop 1
crap 7.5034
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 3
    public function process(ContainerBuilder $container)
32
    {
33 3
        $services = $container->findTaggedServiceIds('php_translation.storage');
34 3
        foreach ($services as $id => $tags) {
35 2
            foreach ($tags as $tag) {
36 2
                if (!isset($tag['name'])) {
37
                    $tag['name'] = 'default';
38
                }
39 2
                if (!isset($tag['type'])) {
40
                    throw new \LogicException('The tag "php_translation.storage" must have a "type".');
41
                }
42
43 2
                $def = $this->getDefinition($container, $tag['name']);
44 2
                switch ($tag['type']) {
45 2
                    case 'remote':
46 1
                        $def->addMethodCall('addRemoteStorage', [new Reference($id)]);
47 1
                        break;
48 1
                    case 'local':
49 1
                        $def->addMethodCall('addLocalStorage', [new Reference($id)]);
50 1
                        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 2
                }
54 2
            }
55 3
        }
56 3
    }
57
58
    /**
59
     * @param ContainerBuilder $container
60
     *
61
     * @return Definition
62
     */
63 2
    private function getDefinition(ContainerBuilder $container, $name)
64
    {
65 2
        if (!isset($this->definitions[$name])) {
66 2
            $this->definitions[$name] = $container->getDefinition('php_translation.storage.'.$name);
67 2
        }
68
69 2
        return $this->definitions[$name];
70
    }
71
}
72