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

getDefaultSchemaList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 26
nc 2
nop 1
dl 0
loc 43
ccs 22
cts 22
cp 1
crap 2
rs 9.504
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
use Yoanm\JsonRpcServerDoc\Domain\Model\ServerDoc;
8
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc;
9
10
/**
11
 * Class ExternalSchemaListDocNormalizer
12
 */
13
class ExternalSchemaListDocNormalizer
14
{
15
    /** @var DefinitionRefResolver */
16
    private $definitionRefResolver;
17
    /** @var TypeDocNormalizer */
18
    private $typeDocNormalizer;
19
    /** @var ErrorDocNormalizer */
20
    private $errorDocNormalizer;
21
    /** @var ShapeNormalizer */
22
    private $shapeNormalizer;
23
24
    /**
25
     * @param DefinitionRefResolver $definitionRefResolver
26
     * @param TypeDocNormalizer     $typeDocNormalizer
27
     * @param ErrorDocNormalizer    $errorDocNormalizer
28
     * @param ShapeNormalizer       $shapeNormalizer
29
     */
30 7
    public function __construct(
31
        DefinitionRefResolver $definitionRefResolver,
32
        TypeDocNormalizer $typeDocNormalizer,
33
        ErrorDocNormalizer $errorDocNormalizer,
34
        ShapeNormalizer $shapeNormalizer
35
    ) {
36 7
        $this->definitionRefResolver = $definitionRefResolver;
37 7
        $this->typeDocNormalizer = $typeDocNormalizer;
38 7
        $this->errorDocNormalizer = $errorDocNormalizer;
39 7
        $this->shapeNormalizer = $shapeNormalizer;
40 7
    }
41
42
    /**
43
     * @param ServerDoc $doc
44
     *
45
     * @return array
46
     *
47
     * @throws \ReflectionException
48
     */
49 7
    public function normalize(ServerDoc $doc) : array
50
    {
51 7
        return array_merge(
52 7
            $this->getMethodsExternalSchemaList($doc),
53 7
            $this->getMethodErrorsExternalSchemaList($doc),
54 7
            $this->getServerErrorsExtraSchemaList($doc),
55 7
            $this->getDefaultSchemaList($doc)
56
        );
57
    }
58
59
    /**
60
     * @param ServerDoc $doc
61
     *
62
     * @return array
63
     *
64
     * @throws \ReflectionException
65
     */
66 7
    protected function getMethodsExternalSchemaList(ServerDoc $doc) : array
67
    {
68 7
        $list = [];
69 7
        foreach ($doc->getMethodList() as $method) {
70
            // Merge extra definitions
71 4
            $list = array_merge($list, $this->getMethodExternalSchemaList($method));
72
        }
73
74 7
        return $list;
75
    }
76
77
    /**
78
     * @param ServerDoc $doc
79
     *
80
     * @return array
81
     *
82
     * @throws \ReflectionException
83
     */
84 7
    protected function getMethodErrorsExternalSchemaList(ServerDoc $doc) : array
85
    {
86 7
        $list = [];
87 7
        foreach ($doc->getMethodList() as $method) {
88 4
            $list = array_merge(
89 4
                $list,
90 4
                $this->normalizeErrorList(
91 4
                    $method->getCustomErrorList(),
92 4
                    DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
93
                )
94
            );
95
        }
96
97 7
        return $list;
98
    }
99
100
101
    /**
102
     * @param ServerDoc $doc
103
     *
104
     * @return array
105
     *
106
     * @throws \ReflectionException
107
     */
108 7
    protected function getServerErrorsExtraSchemaList(ServerDoc $doc) : array
109
    {
110 7
        return array_merge(
111 7
            $this->normalizeErrorList(
112 7
                $doc->getGlobalErrorList(),
113 7
                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
114
            ),
115 7
            $this->normalizeErrorList(
116 7
                $doc->getServerErrorList(),
117 7
                DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE
118
            )
119
        );
120
    }
121
122
    /**
123
     * @param MethodDoc $method
124
     *
125
     * @return array[]
126
     *
127
     * @throws \ReflectionException
128
     */
129 4
    protected function getMethodExternalSchemaList(MethodDoc $method) : array
130
    {
131 4
        $list = [];
132
        // Create request params schema if provided
133 4
        $list = $this->appendAndNormalizeIfNotNull(
134 4
            $this->definitionRefResolver->getMethodDefinitionId(
135 4
                $method,
136 4
                DefinitionRefResolver::METHOD_PARAMS_DEFINITION_TYPE
137
            ),
138 4
            $method->getParamsDoc(),
139 4
            $list
140
        );
141
142
        // Create custom result schema if provided
143 4
        $list = $this->appendAndNormalizeIfNotNull(
144 4
            $this->definitionRefResolver->getMethodDefinitionId(
145 4
                $method,
146 4
                DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE
147
            ),
148 4
            $method->getResultDoc(),
149 4
            $list
150
        );
151
152 4
        return $list;
153
    }
154
155
    /**
156
     * @param ServerDoc $doc
157
     *
158
     * @return array
159
     */
160 7
    protected function getDefaultSchemaList(ServerDoc $doc) : array
161
    {
162
        $propertyList = [
163 7
            'code' => [
164
                'type' => 'integer',
165
            ],
166
        ];
167
168 7
        $errorList = array_merge($doc->getServerErrorList(), $doc->getGlobalErrorList());
169 7
        $errorList = array_reduce(
170 7
            array_map(
171
                function (MethodDoc $methodDoc) {
172 4
                    return $methodDoc->getCustomErrorList();
173 7
                },
174 7
                $doc->getMethodList()
175
            ),
176
            function (array $carry, array $subErrorList) {
177 4
                $carry = array_merge($carry, $subErrorList);
178
179 4
                return $carry;
180 7
            },
181 7
            $errorList
182
        );
183 7
        $codeList = array_unique(
184 7
            array_map(
185
                function (ErrorDoc $errorDoc) {
186 4
                    return $errorDoc->getCode();
187 7
                },
188 7
                $errorList
189
            )
190
        );
191
192 7
        if (count($codeList) > 0) {
193 4
            $propertyList['code']['enum'] = array_values($codeList);
194
        }
195
196
        return [
197
            'Default-Error' => [
198
                'allOf' => [
199 7
                    $this->shapeNormalizer->getErrorShapeDefinition(),
200
                    [
201 7
                        'type' => 'object',
202 7
                        'properties' => $propertyList,
203
                    ],
204
                ],
205
            ],
206
        ];
207
    }
208
209
    /**
210
     * @param array $errorDocList
211
     * @param       $definitionType
212
     *
213
     * @return array
214
     *
215
     * @throws \ReflectionException
216
     */
217 7
    private function normalizeErrorList(array $errorDocList, $definitionType) : array
218
    {
219 7
        $list = [];
220 7
        foreach ($errorDocList as $errorDoc) {
221 4
            $key = $this->definitionRefResolver->getErrorDefinitionId($errorDoc, $definitionType);
222
223 4
            $list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
224
        }
225
226 7
        return $list;
227
    }
228
229
    /**
230
     * @param string       $key
231
     * @param TypeDoc|null $value
232
     * @param array        $list
233
     *
234
     * @return array
235
     *
236
     * @throws \ReflectionException
237
     */
238 4
    protected function appendAndNormalizeIfNotNull(string $key, $value, array $list = []) : array
239
    {
240 4
        if (null !== $value) {
241 2
            $list[$key] = $this->typeDocNormalizer->normalize($value);
242
        }
243
244 4
        return $list;
245
    }
246
}
247