Passed
Pull Request — release/2.0.0 (#26)
by Yo
01:38
created

JsonRpcHttpServerExtension::cleanExternalServiceIdString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace Yoanm\SymfonyJsonRpcHttpServer\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\Exception\LogicException;
9
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
10
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
11
use Symfony\Component\DependencyInjection\Reference;
12
use Yoanm\JsonRpcServer\App\Dispatcher\JsonRpcServerDispatcherAwareTrait;
13
14
/**
15
 * Class JsonRpcHttpServerExtension
16
 */
17
class JsonRpcHttpServerExtension implements ExtensionInterface, CompilerPassInterface
18
{
19
    // Extension identifier (used in configuration for instance)
20
    const EXTENSION_IDENTIFIER = 'json_rpc_http_server';
21
22
    /** Tags */
23
    // Server dispatcher - Use this tag and server dispatcher will be injected
24
    const JSONRPC_SERVER_DISPATCHER_AWARE_TAG = 'json_rpc_http_server.server_dispatcher_aware';
25
26
27
    /** Method resolver */
28
    const METHOD_RESOLVER_ALIAS = 'json_rpc_http_server.alias.method_resolver';
29
    /** Params validator */
30
    const PARAMS_VALIDATOR_ALIAS = 'json_rpc_http_server.alias.params_validator';
31
32
    const REQUEST_HANDLER_SERVICE_ID = 'json_rpc_server_sdk.app.handler.jsonrpc_request';
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 6
    public function load(array $configs, ContainerBuilder $container)
38
    {
39 6
        $this->compileAndProcessConfigurations($configs, $container);
40
41 6
        $loader = new YamlFileLoader(
42 6
            $container,
43 6
            new FileLocator(__DIR__.'/../Resources/config')
44
        );
45 6
        $loader->load('sdk.services.app.yaml');
46 6
        $loader->load('sdk.services.infra.yaml');
47 6
        $loader->load('services.public.yaml');
48 6
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 6
    public function process(ContainerBuilder $container)
54
    {
55 6
        $this->bindJsonRpcServerDispatcher($container);
56 5
        $this->bindValidatorIfDefined($container);
57 5
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 7
    public function getNamespace()
63
    {
64 7
        return 'http://example.org/schema/dic/'.$this->getAlias();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    public function getXsdValidationBasePath()
71
    {
72 1
        return '';
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 7
    public function getAlias()
79
    {
80 7
        return self::EXTENSION_IDENTIFIER;
81
    }
82
83
    /**
84
     * @param ContainerBuilder $container
85
     *
86
     * @return Reference|null Null in case no dispatcher found
87
     */
88 6
    private function bindJsonRpcServerDispatcher(ContainerBuilder $container)
89
    {
90 6
        $dispatcherRef = new Reference('json_rpc_http_server.dispatcher.server');
91 6
        $dispatcherAwareServiceList = $container->findTaggedServiceIds(self::JSONRPC_SERVER_DISPATCHER_AWARE_TAG);
92 6
        foreach ($dispatcherAwareServiceList as $serviceId => $tagAttributeList) {
93 6
            $definition = $container->getDefinition($serviceId);
94
95 6
            if (!in_array(JsonRpcServerDispatcherAwareTrait::class, class_uses($definition->getClass()))) {
96 1
                throw new LogicException(
97 1
                    sprintf(
98 1
                        'Service "%s" is taggued with "%s" but does not use "%s"',
99 1
                        $serviceId,
100 1
                        self::JSONRPC_SERVER_DISPATCHER_AWARE_TAG,
101 1
                        JsonRpcServerDispatcherAwareTrait::class
102
                    )
103
                );
104
            }
105
106 5
            $definition->addMethodCall('setJsonRpcServerDispatcher', [$dispatcherRef]);
107
        }
108 5
    }
109
110
    /**
111
     * @param array            $configs
112
     * @param ContainerBuilder $container
113
     */
114 6
    private function compileAndProcessConfigurations(array $configs, ContainerBuilder $container)
115
    {
116 6
        $configuration = new Configuration();
117 6
        $config = (new Processor())->processConfiguration($configuration, $configs);
118
119 6
        $httpEndpointPath = $config['endpoint'];
120
121 6
        $container->setParameter(self::EXTENSION_IDENTIFIER.'.http_endpoint_path', $httpEndpointPath);
122 6
    }
123
124 5
    private function bindValidatorIfDefined(ContainerBuilder $container)
125
    {
126 5
        if ($container->hasAlias(self::PARAMS_VALIDATOR_ALIAS)) {
127 1
            $container->getDefinition(self::REQUEST_HANDLER_SERVICE_ID)
128 1
                ->addMethodCall(
129 1
                    'setMethodParamsValidator',
130
                    [
131 1
                        new Reference(self::PARAMS_VALIDATOR_ALIAS)
132
                    ]
133
                )
134
            ;
135
        }
136 5
    }
137
}
138