Failed Conditions
Push — master ( dc9ff3...929e65 )
by Yo
03:56
created

JsonRpcMethodDefinitionHelper.php (1 issue)

Labels
Severity
1
<?php
2
namespace Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection;
3
4
use Symfony\Component\DependencyInjection\ContainerBuilder;
1 ignored issue
show
The type Symfony\Component\Depend...ection\ContainerBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
     */
19 5
    public function findAndValidateJsonRpcMethodDefinition(ContainerBuilder $container) : array
20
    {
21 5
        $definitionList = [];
22
23 5
        $taggedServiceList = $container->findTaggedServiceIds(JsonRpcHttpServerExtension::JSONRPC_METHOD_TAG);
24
25 5
        foreach ($taggedServiceList as $serviceId => $tagAttributeList) {
26 5
            $this->validateJsonRpcMethodDefinition($serviceId, $container->getDefinition($serviceId));
27
28 5
            foreach ($tagAttributeList as $tagAttributeKey => $tagAttributeData) {
29 5
                $this->validateJsonRpcMethodTagAttributes($serviceId, $tagAttributeData);
30 5
                $methodName = $tagAttributeData[JsonRpcHttpServerExtension::JSONRPC_METHOD_TAG_METHOD_NAME_KEY];
31 5
                $definitionList[$serviceId][] = $methodName;
32
            }
33
        }
34
35 3
        return $definitionList;
36
    }
37
38
    /**
39
     * @param string $serviceId
40
     * @param array  $tagAttributeData
41
     */
42 5
    private function validateJsonRpcMethodTagAttributes(string $serviceId, array $tagAttributeData) : void
43
    {
44 5
        if (!isset($tagAttributeData[JsonRpcHttpServerExtension::JSONRPC_METHOD_TAG_METHOD_NAME_KEY])) {
45 1
            throw new LogicException(sprintf(
46 1
                'Service "%s" is taggued as JSON-RPC method but does not have'
47 1
                . ' method name defined under "%s" tag attribute key',
48 1
                $serviceId,
49 1
                JsonRpcHttpServerExtension::JSONRPC_METHOD_TAG_METHOD_NAME_KEY
50 1
            ));
51
        }
52
    }
53
54
    /**
55
     * @param string     $serviceId
56
     * @param Definition $definition
57
     *
58
     * @throws \LogicException      In case definition is not valid
59
     */
60 5
    private function validateJsonRpcMethodDefinition(string $serviceId, Definition $definition) : void
61
    {
62 5
        if (!in_array(JsonRpcMethodInterface::class, class_implements($definition->getClass()))) {
63 1
            throw new LogicException(sprintf(
64 1
                'Service "%s" is taggued as JSON-RPC method but does not implement %s',
65 1
                $serviceId,
66 1
                JsonRpcMethodInterface::class
67 1
            ));
68
        }
69
    }
70
}
71