JsonRpcClientExtension::load()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.8333
cc 3
nc 4
nop 2
crap 3
1
<?php
2
/*
3
 * This file is part of JSON RPC Client.
4
 *
5
 * (c) Igor Lazarev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Strider2038\JsonRpcClient\Bridge\Symfony\DependencyInjection;
12
13
use Strider2038\JsonRpcClient\ClientFactoryInterface;
14
use Strider2038\JsonRpcClient\ClientInterface;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Extension\Extension;
19
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
20
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
/**
24
 * @author Igor Lazarev <[email protected]>
25
 */
26
class JsonRpcClientExtension extends Extension implements PrependExtensionInterface
27
{
28 1
    public function prepend(ContainerBuilder $container): void
29
    {
30 1
        $frameworkConfiguration = $container->getExtensionConfig('framework');
31
32 1
        if (empty($frameworkConfiguration)) {
33
            return;
34
        }
35
36 1
        if (!isset($frameworkConfiguration['serializer']['enabled'])) {
37 1
            $container->prependExtensionConfig('framework', ['serializer' => ['enabled' => true]]);
38
        }
39 1
    }
40
41 8
    public function load(array $configs, ContainerBuilder $container): void
42
    {
43 8
        $this->loadServices($container);
44 8
        $configuration = $this->loadConfiguration($configs);
45
46 3
        foreach ($configuration as $clientId => $clientConfig) {
47 3
            $definition = new Definition(ClientInterface::class);
48 3
            $definition->setPublic(true);
49 3
            $definition->setFactory([
50 3
                new Reference(ClientFactoryInterface::class),
51 3
                'createClient',
52
            ]);
53 3
            $definition->setArgument('$url', $clientConfig['url']);
54 3
            $definition->setArgument('$options', $clientConfig['options'] ?? []);
55
56 3
            $container->setDefinition('json_rpc_client.'.$clientId, $definition);
57
        }
58
59 3
        if (array_key_exists('default', $configuration)) {
60 3
            $container->setAlias(ClientInterface::class, 'json_rpc_client.default');
61
        }
62 3
    }
63
64 8
    private function loadServices(ContainerBuilder $container): void
65
    {
66 8
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
67 8
        $loader->load('services.xml');
68 8
    }
69
70 8
    private function loadConfiguration(array $configs): array
71
    {
72 8
        $configuration = new Configuration();
73
74 8
        return $this->processConfiguration($configuration, $configs);
75
    }
76
}
77