Passed
Push — release/1.0.0 ( 02b4c6...f79e53 )
by Yo
01:58 queued 34s
created

RequestDocNormalizer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 50
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 28 2
A __construct() 0 6 1
1
<?php
2
namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component;
3
4
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Resolver\DefinitionRefResolver;
5
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc;
6
7
/**
8
 * Class RequestDocNormalizer
9
 */
10
class RequestDocNormalizer
11
{
12
    /** @var DefinitionRefResolver */
13
    private $definitionRefResolver;
14
    /** @var ShapeNormalizer */
15
    private $shapeNormalizer;
16
17
    /**
18
     * @param DefinitionRefResolver $definitionRefResolver
19
     * @param ShapeNormalizer       $shapeNormalizer
20
     */
21 2
    public function __construct(
22
        DefinitionRefResolver $definitionRefResolver,
23
        ShapeNormalizer $shapeNormalizer
24
    ) {
25 2
        $this->definitionRefResolver = $definitionRefResolver;
26 2
        $this->shapeNormalizer = $shapeNormalizer;
27 2
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function normalize(MethodDoc $method)
33
    {
34 2
        $operationProperties = ['method' => ['example' => $method->getMethodName()]];
35
36 2
        $requestSchema = ['allOf' => [$this->shapeNormalizer->getRequestShapeDefinition()]];
37
        // Append custom if params required
38 2
        if (null !== $method->getParamsDoc()) {
39 1
            $requestSchema['allOf'][] = [
40 1
                'type' => 'object',
41
                'required' => ['params'],
42
                'properties' => [
43
                    'params' => [
44 1
                        '$ref' => $this->definitionRefResolver->getDefinitionRef(
45 1
                            $this->definitionRefResolver->getMethodDefinitionId(
46 1
                                $method,
47 1
                                DefinitionRefResolver::METHOD_PARAMS_DEFINITION_TYPE
48
                            )
49
                        )
50
                    ],
51
                ],
52
            ];
53
        }
54 2
        $requestSchema['allOf'][] = [
55 2
            'type' => 'object',
56 2
            'properties' => $operationProperties,
57
        ];
58
59 2
        return $requestSchema;
60
    }
61
}
62