Passed
Pull Request — release/1.0.0 (#13)
by Yo
01:34
created

OperationDocNormalizer::normalize()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 1
dl 0
loc 29
ccs 13
cts 13
cp 1
crap 2
rs 8.8571
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' => '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
            $docDescription['description'] = isset($docDescription['description'])
89 2
                && strlen($docDescription['description']) > 0
90 1
                ? $docDescription['description'] . "\n"
91 1
                : ''
92
            ;
93 2
            $docDescription['description'] = $docDescription['description'] . $responseDescription;
94
        }
95
96 5
        return $docDescription;
97
    }
98
99
    /**
100
     * @param MethodDoc $method
101
     *
102
     * @return string|null
103
     */
104 5
    private function getResponseDescription(MethodDoc $method)
105
    {
106 5
        if (count($method->getCustomErrorList())) {
107 2
            $self = $this;
108
            // Better to use raw string instead of \t
109
            $indentString = <<<STRING
110
111
    -
112
STRING;
113
114 2
            return sprintf(
115 2
                "Could throw custom errors : %s%s",
116 2
                $indentString,
117 2
                implode(
118 2
                    $indentString,
119 2
                    array_map(
120
                        function (ErrorDoc $errorDoc) use ($self) {
121 2
                            return sprintf(
122 2
                                '*%s* (**Models->%s**)',
123 2
                                $errorDoc->getTitle(),
124 2
                                $self->definitionRefResolver->getErrorDefinitionId(
125 2
                                    $errorDoc,
126 2
                                    DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
127
                                )
128
                            );
129 2
                        },
130 2
                        $method->getCustomErrorList()
131
                    )
132
                )
133
            );
134
        }
135
136 3
        return null;
137
    }
138
}
139