Completed
Pull Request — release/1.0.0 (#9)
by Yo
01:35
created

appendAndNormalizeIfNotNull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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