Passed
Pull Request — master (#26)
by Yo
04:47 queued 01:57
created

OperationDocNormalizer::getDescriptionDoc()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 6
nop 1
dl 0
loc 21
ccs 14
cts 14
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
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' => 'JSON-RPC response',
68 5
                        'schema' => $this->responseDocNormalizer->normalize($method),
69
                    ]
70
                ]
71
            ]
72
        ;
73
    }
74
75
    /**
76
     * @param MethodDoc $method
77
     *
78
     * @return array
79
     */
80 5
    protected function getDescriptionDoc(MethodDoc $method) : array
81
    {
82 5
        $docDescription = [];
83 5
        if (null !== $method->getDescription()) {
84 2
            $docDescription['description'] = $method->getDescription();
85
        }
86
87 5
        $responseDescription = $this->getResponseDescription($method);
88 5
        if (null !== $responseDescription) {
89 2
            $hasInitialDescription = isset($docDescription['description'])
90 2
                && strlen($docDescription['description']) > 0
91
            ;
92 2
            $docDescription['description'] = sprintf(
93 2
                '%s%s%s',
94 2
                ($hasInitialDescription ? $docDescription['description'] : ''),
95 2
                ($hasInitialDescription ? "\n" : ''),
96 2
                $responseDescription
97
            );
98
        }
99
100 5
        return $docDescription;
101
    }
102
103
    /**
104
     * @param MethodDoc $method
105
     *
106
     * @return string|null
107
     */
108 5
    private function getResponseDescription(MethodDoc $method) : ?string
109
    {
110 5
        if (count($method->getCustomErrorList())) {
111 2
            $self = $this;
112
            // Better to use raw string instead of \t
113
            $indentString = <<<STRING
114
115
    -
116
STRING;
117
118 2
            return sprintf(
119 2
                "Could throw custom errors : %s%s",
120 2
                $indentString,
121 2
                implode(
122 2
                    $indentString,
123 2
                    array_map(
124
                        function (ErrorDoc $errorDoc) use ($self) {
125 2
                            return $self->formatErrorForDescription($errorDoc);
126 2
                        },
127 2
                        $method->getCustomErrorList()
128
                    )
129
                )
130
            );
131
        }
132
133 3
        return null;
134
    }
135
136
    /**
137
     * @param ErrorDoc $errorDoc
138
     *
139
     * @return string
140
     */
141 2
    private function formatErrorForDescription(ErrorDoc $errorDoc) : string
142
    {
143 2
        return sprintf(
144 2
            '*%s* (**Definitions->%s**)',
145 2
            $errorDoc->getTitle(),
146 2
            $this->definitionRefResolver->getErrorDefinitionId(
147 2
                $errorDoc,
148 2
                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
149
            )
150
        );
151
    }
152
}
153