Passed
Pull Request — release/1.0.0 (#24)
by Yo
03:45
created

JsonRpcHttpServerExtension::injectMethodMappingToServiceNameResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 1
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
    const JSONRPC_METHOD_PARAMS_VALIDATOR_TAG = 'json_rpc_http_server.method_params_validator';
24
25
    // Server dispatcher - Use this tag and server dispatcher will be injected
26
    const JSONRPC_SERVER_DISPATCHER_AWARE_TAG = 'json_rpc_http_server.server_dispatcher_aware';
27
    // JSON-RPC Methods mapping - Use this tag and all JSON-RPC methods mapping will be injected
28
    const JSONRPC_METHOD_MAPPING_AWARE_TAG = 'json_rpc_http_server.method_mapping_aware';
29
    // JSON-RPC Methods - Use this tag and all JSON-RPC method instance will be injected
30
    const JSONRPC_METHOD_AWARE_TAG = 'json_rpc_http_server.method_aware';
31
32
33
    /** Method resolver */
34
    const METHOD_RESOLVER_ALIAS = 'json_rpc_http_server.alias.method_resolver';
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 5
    public function load(array $configs, ContainerBuilder $container)
40
    {
41 5
        $this->compileAndProcessConfigurations($configs, $container);
42
43 5
        $loader = new YamlFileLoader(
44 5
            $container,
45 5
            new FileLocator(__DIR__.'/../Resources/config')
46
        );
47 5
        $loader->load('sdk.services.app.yaml');
48 5
        $loader->load('sdk.services.infra.yaml');
49 5
        $loader->load('services.public.yaml');
50 5
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 5
    public function process(ContainerBuilder $container)
56
    {
57 5
        $this->bindJsonRpcServerDispatcher($container);
58 4
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 6
    public function getNamespace()
64
    {
65 6
        return 'http://example.org/schema/dic/'.$this->getAlias();
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function getXsdValidationBasePath()
72
    {
73 1
        return '';
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 6
    public function getAlias()
80
    {
81 6
        return self::EXTENSION_IDENTIFIER;
82
    }
83
84
    /**
85
     * @param ContainerBuilder $container
86
     *
87
     * @return Reference|null Null in case no dispatcher found
88
     */
89 5
    private function bindJsonRpcServerDispatcher(ContainerBuilder $container)
90
    {
91 5
        $dispatcherRef = new Reference('json_rpc_http_server.dispatcher.server');
92 5
        $dispatcherAwareServiceList = $container->findTaggedServiceIds(self::JSONRPC_SERVER_DISPATCHER_AWARE_TAG);
93 5
        foreach ($dispatcherAwareServiceList as $serviceId => $tagAttributeList) {
94 5
            $definition = $container->getDefinition($serviceId);
95
96 5
            if (!in_array(JsonRpcServerDispatcherAwareTrait::class, class_uses($definition->getClass()))) {
97 1
                throw new LogicException(
98 1
                    sprintf(
99 1
                        'Service "%s" is taggued with "%s" but does not use "%s"',
100 1
                        $serviceId,
101 1
                        self::JSONRPC_SERVER_DISPATCHER_AWARE_TAG,
102 1
                        JsonRpcServerDispatcherAwareTrait::class
103
                    )
104
                );
105
            }
106
107 4
            $definition->addMethodCall('setJsonRpcServerDispatcher', [$dispatcherRef]);
108
        }
109 4
    }
110
111
    /**
112
     * @param array            $configs
113
     * @param ContainerBuilder $container
114
     */
115 5
    private function compileAndProcessConfigurations(array $configs, ContainerBuilder $container)
116
    {
117 5
        $configuration = new Configuration();
118 5
        $config = (new Processor())->processConfiguration($configuration, $configs);
119
120 5
        $httpEndpointPath = $config['endpoint'];
121
122 5
        $container->setParameter(self::EXTENSION_IDENTIFIER.'.http_endpoint_path', $httpEndpointPath);
123 5
    }
124
}
125