appendDocumentationProvider()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace Yoanm\SymfonyJsonRpcHttpServerDoc\DependencyInjection;
3
4
use Symfony\Component\Config\Definition\Processor;
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
9
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
/**
13
 * Class JsonRpcHttpServerDocExtension
14
 */
15
class JsonRpcHttpServerDocExtension implements ExtensionInterface, CompilerPassInterface
16
{
17
    /** Tags */
18
    /** Use this tag to inject your custom documentation creator */
19
    const DOC_PROVIDER_TAG = 'json_rpc_server_doc.doc_provider';
20
21
    // Extension identifier (used in configuration for instance)
22
    const EXTENSION_IDENTIFIER = 'json_rpc_http_server_doc';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 3
    public function load(array $configs, ContainerBuilder $container)
28
    {
29 3
        $this->compileAndProcessConfigurations($configs, $container);
30
31 3
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
32
33 3
        $loader->load('services.sdk.infra.yaml');
34 3
        $loader->load('services.private.yaml');
35 3
        $loader->load('services.public.yaml');
36 3
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 3
    public function process(ContainerBuilder $container)
42
    {
43 3
        $this->appendDocumentationProvider($container);
44 3
    }
45
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 4
    public function getNamespace()
51
    {
52 4
        return 'http://example.org/schema/dic/'.$this->getAlias();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function getXsdValidationBasePath()
59
    {
60 1
        return '';
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 4
    public function getAlias()
67
    {
68 4
        return self::EXTENSION_IDENTIFIER;
69
    }
70
71
    /**
72
     * @param ContainerBuilder $container
73
     */
74 3
    private function appendDocumentationProvider(ContainerBuilder $container)
75
    {
76 3
        $docProviderDefinition = $container->getDefinition('json_rpc_http_server_doc.finder.normalized_doc');
77 3
        $docCreatorServiceList = $container->findTaggedServiceIds(self::DOC_PROVIDER_TAG);
78 3
        foreach (array_keys($docCreatorServiceList) as $serviceId) {
79 3
            $docProviderDefinition->addMethodCall('addProvider', [new Reference($serviceId)]);
80
        }
81 3
    }
82
83
    /**
84
     * @param array            $configs
85
     * @param ContainerBuilder $container
86
     */
87 3
    private function compileAndProcessConfigurations(array $configs, ContainerBuilder $container)
88
    {
89 3
        $configuration = new Configuration();
90 3
        $config = (new Processor())->processConfiguration($configuration, $configs);
91
92 3
        $httpEndpointPath = $config['endpoint'];
93
94 3
        $container->setParameter(self::EXTENSION_IDENTIFIER.'.http_endpoint_path', $httpEndpointPath);
95 3
    }
96
}
97