Passed
Push — release/1.0.0 ( a57e41...4b5090 )
by Yo
01:36
created

OperationDocNormalizer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 132
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

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