AndiKickBoxExtension::processClientDefinitions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 16
ccs 13
cts 13
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Kickbox Bundle.
5
 *
6
 * (c) Abdoul Ndiaye <[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 Andi\KickBoxBundle\DependencyInjection;
13
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
use Symfony\Component\DependencyInjection\Loader;
20
21
/**
22
 * This is the class that loads and manages your bundle configuration
23
 *
24
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
25
 */
26
class AndiKickBoxExtension extends Extension
27
{
28
    const DEFAULT_CLIENT_SERVICE_NAME = 'kickbox_client';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 4
    public function load(array $configs, ContainerBuilder $container)
34
    {
35 4
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
36 4
        $loader->load('services.xml');
37
38 4
        $configuration = new Configuration();
39 4
        $config = $this->processConfiguration($configuration, $configs);
40
41 1
        $this->processClientDefinitions($container, $config);
42 1
        $this->createDefaultClientAlias($container, $config);
43 2
    }
44
45
    /**
46
     * Process and create the kickbox client services.
47
     *
48
     * @param ContainerBuilder $container A ContainerBuilder instance
49
     * @param array            $config    The configuration of the kickbox bundle.
50
     */
51 1
    protected function processClientDefinitions(ContainerBuilder $container, array $config)
52
    {
53 1
        $clientClass = $container->getParameter('kickbox.http.client.class');
54 1
        $endPoint    = $config['endpoint'];
55 1
        $apiKeys     = $config['api_keys'];
56
57 1
        foreach ($apiKeys as $name => $node) {
58 1
            $clientDefinition = new Definition($clientClass);
59 1
            $clientDefinition->addArgument(new Reference('kickbox.guzzle.client'));
60 1
            $clientDefinition->addArgument(new Reference('kickbox.http.response.factory'));
61 1
            $clientDefinition->addArgument($endPoint);
62 1
            $clientDefinition->addArgument($node['key']);
63
64 1
            $container->setDefinition($this->getClientServiceName($name), $clientDefinition);
65 1
        }
66 1
    }
67
68
    /**
69
     * Create an alias for the default service definition.
70
     *
71
     * @param ContainerBuilder $container A ContainerBuilder instance
72
     * @param array            $config    The configuration of the kickbox bundle.
73
     */
74 1
    protected function createDefaultClientAlias(ContainerBuilder $container, array $config)
75
    {
76 1
        $apiKeys     = $config['api_keys'];
77 1
        $defaultName = isset($config['default_api_name']) ? $config['default_api_name'] : null;
78
79 1
        if (null !== $defaultName && array_key_exists($defaultName, $apiKeys)) {
80
            $defaultClientReference = $defaultName;
81
        } else {
82 1
            $names = array_keys($apiKeys);
83 1
            $defaultClientReference = $names[0];
84
        }
85
86 1
        $container->setAlias(static::DEFAULT_CLIENT_SERVICE_NAME, $this->getClientServiceName($defaultClientReference));
87 1
    }
88
89
    /**
90
     * Returns the name of a client service according to a given name.
91
     *
92
     * @param  string $name The service name
93
     *
94
     * @return string
95
     */
96 1
    protected function getClientServiceName($name)
97 1
    {
98 1
        return static::DEFAULT_CLIENT_SERVICE_NAME . '.' . $name;
99
    }
100
}
101