OperationDocNormalizer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 61
c 1
b 0
f 0
dl 0
loc 138
ccs 75
cts 75
cp 1
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A formatErrorForDescription() 0 8 1
A normalize() 0 29 2
A getResponseDescription() 0 26 2
A getDescriptionDoc() 0 21 6
1
<?php
2
namespace Yoanm\JsonRpcHttpServerSwaggerDoc\App\Normalizer\Component;
3
4
use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Resolver\DefinitionRefResolver;
5
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc;
6
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc;
7
8
/**
9
 * Class OperationDocNormalizer
10
 */
11
class OperationDocNormalizer
12
{
13
    /** @var RequestDocNormalizer */
14
    private $requestDocTransformer;
15
    /** @var ResponseDocNormalizer */
16
    private $responseDocNormalizer;
17
    /** @var DefinitionRefResolver */
18
    private $definitionRefResolver;
19
20
    /**
21
     * @param DefinitionRefResolver $definitionRefResolver
22
     * @param RequestDocNormalizer  $requestDocTransformer
23
     * @param ResponseDocNormalizer $responseDocNormalizer
24
     */
25 41
    public function __construct(
26
        DefinitionRefResolver $definitionRefResolver,
27
        RequestDocNormalizer $requestDocTransformer,
28
        ResponseDocNormalizer $responseDocNormalizer
29
    ) {
30 41
        $this->requestDocTransformer = $requestDocTransformer;
31 41
        $this->responseDocNormalizer = $responseDocNormalizer;
32 41
        $this->definitionRefResolver = $definitionRefResolver;
33
    }
34
35
    /**
36
     * @param MethodDoc $method
37
     *
38
     * @return array
39
     */
40 11
    public function normalize(MethodDoc $method) : array
41
    {
42 11
        $docTags = [];
43
44 11
        if (count($method->getTagList())) {
45 3
            $docTags['tags'] = $method->getTagList();
46
        }
47
48 11
        return [
49 11
                'summary' => sprintf('"%s" json-rpc method', $method->getMethodName()),
50 11
            ]
51 11
            + $this->getDescriptionDoc($method)
52 11
            + $docTags
53 11
            + [
54 11
                'operationId' => $method->getIdentifier(),
55 11
                'consumes' => ['application/json'],
56 11
                'produces' => ['application/json'],
57 11
                'parameters' => [
58 11
                    [
59 11
                        'in' => 'body',
60 11
                        'name' => 'JsonRpc request',
61 11
                        'required' => true,
62 11
                        'schema' => $this->requestDocTransformer->normalize($method)
63 11
                    ]
64 11
                ],
65 11
                'responses' => [
66 11
                    '200' => [
67 11
                        'description' => 'JSON-RPC response',
68 11
                        'schema' => $this->responseDocNormalizer->normalize($method),
69 11
                    ]
70 11
                ]
71 11
            ]
72 11
        ;
73
    }
74
75
    /**
76
     * @param MethodDoc $method
77
     *
78
     * @return array
79
     */
80 11
    protected function getDescriptionDoc(MethodDoc $method) : array
81
    {
82 11
        $docDescription = [];
83 11
        if (null !== $method->getDescription()) {
84 3
            $docDescription['description'] = $method->getDescription();
85
        }
86
87 11
        $responseDescription = $this->getResponseDescription($method);
88 11
        if (null !== $responseDescription) {
89 4
            $hasInitialDescription = isset($docDescription['description'])
90 4
                && strlen($docDescription['description']) > 0
91 4
            ;
92 4
            $docDescription['description'] = sprintf(
93 4
                '%s%s%s',
94 4
                ($hasInitialDescription ? $docDescription['description'] : ''),
95 4
                ($hasInitialDescription ? "\n" : ''),
96 4
                $responseDescription
97 4
            );
98
        }
99
100 11
        return $docDescription;
101
    }
102
103
    /**
104
     * @param MethodDoc $method
105
     *
106
     * @return string|null
107
     */
108 11
    private function getResponseDescription(MethodDoc $method) : ?string
109
    {
110 11
        if (count($method->getCustomErrorList())) {
111 4
            $self = $this;
112
            // Better to use raw string instead of \t
113 4
            $indentString = <<<STRING
114
115
    -
116 4
STRING;
117
118 4
            return sprintf(
119 4
                "Could throw custom errors : %s%s",
120 4
                $indentString,
121 4
                implode(
122 4
                    $indentString,
123 4
                    array_map(
124 4
                        function (ErrorDoc $errorDoc) use ($self) {
125 4
                            return $self->formatErrorForDescription($errorDoc);
126 4
                        },
127 4
                        $method->getCustomErrorList()
128 4
                    )
129 4
                )
130 4
            );
131
        }
132
133 7
        return null;
134
    }
135
136
    /**
137
     * @param ErrorDoc $errorDoc
138
     *
139
     * @return string
140
     */
141 4
    private function formatErrorForDescription(ErrorDoc $errorDoc) : string
142
    {
143 4
        return sprintf(
144 4
            '*%s* (**Definitions->%s**)',
145 4
            $errorDoc->getTitle(),
146 4
            $this->definitionRefResolver->getErrorDefinitionId(
147 4
                $errorDoc,
148 4
                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
149 4
            )
150 4
        );
151
    }
152
}
153