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

OperationDocNormalizer::normalize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 67
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 3.1547

Importance

Changes 0
Metric Value
cc 3
eloc 41
nc 4
nop 2
dl 0
loc 67
ccs 23
cts 31
cp 0.7419
crap 3.1547
rs 9.264
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_merge(
48 4
            array_map(
49
                function (ErrorDoc $errorDoc) use ($self) {
50
                    return [
51
                        '$ref' => $self->definitionRefResolver->getDefinitionRef(
52
                            $self->definitionRefResolver->getErrorDefinitionId(
53
                                $errorDoc,
54
                                DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE
55
                            )
56
                        )
57
                    ];
58 4
                },
59 4
                $serverDoc->getServerErrorList()
60
            ),
61 4
            array_map(
62
                function (ErrorDoc $errorDoc) use ($self) {
63
                    return [
64
                        '$ref' => $self->definitionRefResolver->getDefinitionRef(
65
                            $self->definitionRefResolver->getErrorDefinitionId(
66
                                $errorDoc,
67
                                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
68
                            )
69
                        )
70
                    ];
71 4
                },
72 4
                $serverDoc->getGlobalErrorList()
73
            )
74
        );
75
76 4
        $docDescription = $docTags = [];
77
78 4
        if (null !== $method->getDescription()) {
79 2
            $docDescription['description'] = $method->getDescription();
80
        }
81
82 4
        if (count($method->getTagList())) {
83 2
            $docTags['tags'] = $method->getTagList();
84
        }
85
86
        return [
87 4
                'summary' => sprintf('"%s" json-rpc method', $method->getMethodName()),
88
            ]
89 4
            + $docDescription
90 4
            + $docTags
91
            + [
92 4
                'operationId' => $method->getIdentifier(),
93
                'requestBody' => [
94
                    'required' => true,
95
                    'content' => [
96
                        'application/json' => [
97 4
                            'schema' => $this->requestDocTransformer->normalize($method)
98
                        ]
99
                    ]
100
                ],
101
                'responses' => [
102
                    '200' => [
103 4
                        'description' => 'JSON-RPC response',
104
                        'content' => [
105
                            'application/json' => [
106 4
                                'schema' => $this->responseDocNormalizer->normalize(
107 4
                                    $method,
108 4
                                    $extraErrorDefinitionIdRefList
109
                                )
110
                            ],
111
                        ]
112
                    ]
113
                ]
114
            ]
115
        ;
116
    }
117
}
118