ClientCompilerPass::process()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Guzzle\ConfigOperationsBundle\DependencyInjection\CompilerPass;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * Compiler pass that will add our definitions to Guzzle clients.
12
 *
13
 * @author Pierre Rolland <[email protected]>
14
 */
15
class ClientCompilerPass implements CompilerPassInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function process(ContainerBuilder $container)
21
    {
22
        $taggedServices = $container->findTaggedServiceIds(
23
            'guzzle.client'
24
        );
25
26
        $definitions = [];
27
        foreach ($taggedServices as $id => $tags) {
28
            foreach ($tags as $attributes) {
29
                $definitionId = sprintf('guzzle_client.%s', $attributes['alias']);
30
                if (!$container->has($definitionId)) {
31
                    $definition = new Definition();
32
                    $definition->setClass('Guzzle\ConfigOperationsBundle\GuzzleClient');
33
                    $definition->setFactory([
34
                        new Reference('guzzle_config_operations.factory'),
35
                        'getClient'
36
                    ]);
37
                    $definition->setPublic(true);
38
                    $definition->addArgument($id);
39
                    $definitions[$definitionId] = $definition;
40
                }
41
            }
42
        }
43
44
        $container->addDefinitions($definitions);
45
    }
46
}
47