Failed Conditions
Pull Request — release/1.0.0 (#3)
by Yo
01:42
created

OperationDocNormalizer::normalize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3.0473

Importance

Changes 0
Metric Value
cc 3
eloc 32
nc 4
nop 2
dl 0
loc 52
ccs 19
cts 23
cp 0.8261
crap 3.0473
rs 9.408
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component;
3
4
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Resolver\DefinitionRefResolver;
5
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc;
6
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc;
7
use Yoanm\JsonRpcServerDoc\Domain\Model\ServerDoc;
8
9
/**
10
 * Class OperationDocNormalizer
11
 */
12
class OperationDocNormalizer
13
{
14
    /** @var RequestDocNormalizer */
15
    private $requestDocTransformer;
16
    /** @var ResponseDocNormalizer */
17
    private $responseDocNormalizer;
18
    /** @var DefinitionRefResolver */
19
    private $definitionRefResolver;
20
21
    /**
22
     * @param DefinitionRefResolver $definitionRefResolver
23
     * @param RequestDocNormalizer  $requestDocTransformer
24
     * @param ResponseDocNormalizer $responseDocNormalizer
25
     */
26 4
    public function __construct(
27
        DefinitionRefResolver $definitionRefResolver,
28
        RequestDocNormalizer $requestDocTransformer,
29
        ResponseDocNormalizer $responseDocNormalizer
30
    ) {
31 4
        $this->requestDocTransformer = $requestDocTransformer;
32 4
        $this->responseDocNormalizer = $responseDocNormalizer;
33 4
        $this->definitionRefResolver = $definitionRefResolver;
34 4
    }
35
36
    /**
37
     * @param MethodDoc $method
38
     * @param ServerDoc $serverDoc
39
     *
40
     * @return array
41
     */
42 4
    public function normalize(MethodDoc $method, ServerDoc $serverDoc) : array
43
    {
44 4
        $self = $this
45
        ;
46
47 4
        $extraErrorDefinitionIdRefList = array_map(
48
            function (ErrorDoc $errorDoc) use ($self) {
49
                return [
50
                    '$ref' => $self->definitionRefResolver->getDefinitionRef(
51
                        $self->definitionRefResolver->getErrorDefinitionId(
52
                            $errorDoc,
53
                            DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE
54
                        )
55
                    )
56
                ];
57 4
            },
58 4
            $serverDoc->getServerErrorList()
59
        );
60
61 4
        $docDescription = $docTags = [];
62
63 4
        if (null !== $method->getDescription()) {
64 2
            $docDescription['description'] = $method->getDescription();
65
        }
66
67 4
        if (count($method->getTagList())) {
68 2
            $docTags['tags'] = $method->getTagList();
69
        }
70
71
        return [
72 4
                'summary' => sprintf('"%s" json-rpc method', $method->getMethodName()),
73
            ]
74 4
            + $docDescription
75 4
            + $docTags
76
            + [
77 4
                'operationId' => $method->getIdentifier(),
78
                'requestBody' => [
79
                    'required' => true,
80
                    'content' => [
81
                        'application/json' => [
82 4
                            'schema' => $this->requestDocTransformer->normalize($method)
83
                        ]
84
                    ]
85
                ],
86
                'responses' => [
87
                    '200' => [
88 4
                        'description' => 'JSON-RPC response',
89
                        'content' => [
90
                            'application/json' => [
91 4
                                'schema' => $this->responseDocNormalizer->normalize(
92 4
                                    $method,
93 4
                                    $extraErrorDefinitionIdRefList
94
                                )
95
                            ],
96
                        ]
97
                    ]
98
                ]
99
            ]
100
        ;
101
    }
102
}
103