ExternalSchemaListDocNormalizer   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 81
c 1
b 0
f 0
dl 0
loc 232
ccs 109
cts 109
cp 1
rs 10
wmc 14

9 Methods

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