Passed
Pull Request — release/2.0.0 (#29)
by Yo
01:36
created

validateJsonRpcMethodDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
namespace Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection;
3
4
use Symfony\Component\DependencyInjection\ContainerBuilder;
5
use Symfony\Component\DependencyInjection\Definition;
6
use Symfony\Component\DependencyInjection\Exception\LogicException;
7
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
8
9
/**
10
 * Class JsonRpcMethodDefinitionHelper
11
 */
12
class JsonRpcMethodDefinitionHelper
13
{
14
    /**
15
     * @param ContainerBuilder $container
16
     *
17
     * @return array
18
     * @throws \ReflectionException
19
     */
20 3
    public function findAndValidateJsonRpcMethodDefinition(ContainerBuilder $container) : array
21
    {
22 3
        $definitionList = [];
23
24 3
        $taggedServiceList = $container->findTaggedServiceIds(JsonRpcHttpServerExtension::JSONRPC_METHOD_TAG);
25
26 3
        foreach ($taggedServiceList as $serviceId => $tagAttributeList) {
27 3
            $this->validateJsonRpcMethodDefinition($serviceId, $container->getDefinition($serviceId));
28
29 3
            foreach ($tagAttributeList as $tagAttributeKey => $tagAttributeData) {
30 3
                $this->validateJsonRpcMethodTagAttributes($serviceId, $tagAttributeData);
31 3
                $methodName = $tagAttributeData[JsonRpcHttpServerExtension::JSONRPC_METHOD_TAG_METHOD_NAME_KEY];
32 3
                $definitionList[$serviceId][] = $methodName;
33
            }
34
        }
35
36 1
        return $definitionList;
37
    }
38
39
    /**
40
     * @param string $serviceId
41
     * @param array  $tagAttributeData
42
     */
43 3
    private function validateJsonRpcMethodTagAttributes(string $serviceId, array $tagAttributeData) : void
44
    {
45 3
        if (!isset($tagAttributeData[JsonRpcHttpServerExtension::JSONRPC_METHOD_TAG_METHOD_NAME_KEY])) {
46 1
            throw new LogicException(sprintf(
47
                'Service "%s" is taggued as JSON-RPC method but does not have'
48 1
                . ' method name defined under "%s" tag attribute key',
49 1
                $serviceId,
50 1
                JsonRpcHttpServerExtension::JSONRPC_METHOD_TAG_METHOD_NAME_KEY
51
            ));
52
        }
53 3
    }
54
55
    /**
56
     * @param string     $serviceId
57
     * @param Definition $definition
58
     *
59
     * @throws \ReflectionException
60
     * @throws \LogicException      In case definition is not valid
61
     */
62 3
    private function validateJsonRpcMethodDefinition(string $serviceId, Definition $definition) : void
63
    {
64 3
        if (!in_array(JsonRpcMethodInterface::class, class_implements($definition->getClass()))) {
65 1
            throw new LogicException(sprintf(
66 1
                'Service "%s" is taggued as JSON-RPC method but does not implement %s',
67 1
                $serviceId,
68 1
                JsonRpcMethodInterface::class
69
            ));
70
        }
71 3
    }
72
}
73