Completed
Push — master ( 130d29...c9c04e )
by Tobias
187:22 queued 181:29
created

StoragePass   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 77.27%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 50
ccs 17
cts 22
cp 0.7727
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefinition() 0 8 2
B process() 0 28 7
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 2
    public function process(ContainerBuilder $container)
32
    {
33 2
        $services = $container->findTaggedServiceIds('php_translation.storage');
34 2
        foreach ($services as $id => $tags) {
35 1
            foreach ($tags as $tag) {
36 1
                if (!isset($tag['name'])) {
37
                    $tag['name'] = 'default';
38
                }
39 1
                if (!isset($tag['type'])) {
40
                    throw new \LogicException('The tag "php_translation.storage" must have a "type".');
41
                }
42
43 1
                $def = $this->getDefinition($container, $tag['name']);
44 1
                switch ($tag['type']) {
45 1
                    case 'remote':
46 1
                        $def->addMethodCall('addRemoteStorage', [new Reference($id)]);
47
48 1
                        break;
49
                    case 'local':
50
                        $def->addMethodCall('addLocalStorage', [new Reference($id)]);
51
52
                        break;
53
                    default:
54 1
                        throw new \LogicException(sprintf('The tag "php_translation.storage" must have a "type" of value "local" or "remote". Value "%s" was provided', $tag['type']));
55
                }
56
            }
57
        }
58 2
    }
59
60
    /**
61
     * @param ContainerBuilder $container
62
     *
63
     * @return Definition
64
     */
65 1
    private function getDefinition(ContainerBuilder $container, $name)
66
    {
67 1
        if (!isset($this->definitions[$name])) {
68 1
            $this->definitions[$name] = $container->getDefinition('php_translation.storage.'.$name);
69
        }
70
71 1
        return $this->definitions[$name];
72
    }
73
}
74