Failed Conditions
Pull Request — release/1.0.0 (#9)
by Yo
01:41
created

getDefaultSchemaList()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 24
nop 1
dl 0
loc 33
ccs 16
cts 16
cp 1
crap 6
rs 8.439
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\MethodDoc;
6
use Yoanm\JsonRpcServerDoc\Domain\Model\ServerDoc;
7
8
/**
9
 * Class ExternalSchemaListDocNormalizer
10
 */
11
class ExternalSchemaListDocNormalizer
12
{
13
    /** @var DefinitionRefResolver */
14
    private $definitionRefResolver;
15
    /** @var TypeDocNormalizer */
16
    private $typeDocNormalizer;
17
    /** @var ErrorDocNormalizer */
18
    private $errorDocNormalizer;
19
    /** @var ShapeNormalizer */
20
    private $shapeNormalizer;
21
22
    /**
23
     * @param ExternalSchemaNormalizerHelper $externalSchemaNormalizerHelper
0 ignored issues
show
Bug introduced by
The type Yoanm\JsonRpcHttpServerS...lSchemaNormalizerHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
     * @param ShapeNormalizer                $shapeNormalizer
25
     */
26 7
    public function __construct(
27
        DefinitionRefResolver $definitionRefResolver,
28
        TypeDocNormalizer $typeDocNormalizer,
29
        ErrorDocNormalizer $errorDocNormalizer,
30
        ShapeNormalizer $shapeNormalizer
31
    ) {
32 7
        $this->definitionRefResolver = $definitionRefResolver;
33 7
        $this->typeDocNormalizer = $typeDocNormalizer;
34 7
        $this->errorDocNormalizer = $errorDocNormalizer;
35 7
        $this->shapeNormalizer = $shapeNormalizer;
36 7
    }
37
38
    /**
39
     * @param ServerDoc $doc
40
     * @return array
41
     */
42 7
    public function normalize(ServerDoc $doc)
43
    {
44 7
        return array_merge(
45 7
            $this->getMethodsExternalSchemaList($doc),
46 7
            $this->getMethodErrorsExternalSchemaList($doc),
47 7
            $this->getServerErrorsExtraSchemaList($doc),
48 7
            $this->getDefaultSchemaList($doc)
49
        );
50
    }
51
52
    /**
53
     * @param ServerDoc $doc
54
     *
55
     * @return array
56
     */
57 7
    protected function getMethodsExternalSchemaList(ServerDoc $doc)
58
    {
59 7
        $list = [];
60 7
        foreach ($doc->getMethodList() as $method) {
61
            // Merge extra definitions
62 4
            $list = array_merge($list, $this->getMethodExternalSchemaList($method));
63
        }
64
65 7
        return $list;
66
    }
67
68
    /**
69
     * @param ServerDoc $doc
70
     *
71
     * @return array
72
     */
73 7
    protected function getMethodErrorsExternalSchemaList(ServerDoc $doc)
74
    {
75 7
        $list = [];
76 7
        foreach ($doc->getMethodList() as $method) {
77 4
            $list = array_merge(
78 4
                $list,
79 4
                $this->normalizeErrorList(
80 4
                    $method->getCustomErrorList(),
81 4
                    DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
82
                )
83
            );
84
        }
85
86 7
        return $list;
87
    }
88
89
90
    /**
91
     * @param ServerDoc $doc
92
     *
93
     * @return array
94
     */
95 7
    protected function getServerErrorsExtraSchemaList(ServerDoc $doc)
96
    {
97 7
        return array_merge(
98 7
            $this->normalizeErrorList(
99 7
                $doc->getGlobalErrorList(),
100 7
                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
101
            ),
102 7
            $this->normalizeErrorList(
103 7
                $doc->getServerErrorList(),
104 7
                DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE
105
            )
106
        );
107
    }
108
109
    /**
110
     * @param MethodDoc $method
111
     *
112
     * @return array[]
113
     */
114 4
    protected function getMethodExternalSchemaList(MethodDoc $method) : array
115
    {
116 4
        $list = [];
117
        // Create request params schema if provided
118 4
        if (null !== $method->getParamsDoc()) {
119 1
            $key = $this->definitionRefResolver->getMethodDefinitionId(
120 1
                $method,
121 1
                DefinitionRefResolver::METHOD_PARAMS_DEFINITION_TYPE
122
            );
123 1
            $list[$key] = $this->typeDocNormalizer->normalize($method->getParamsDoc());
124
        }
125
126
        // Create custom result schema if provided
127 4
        if (null !== $method->getResultDoc()) {
128 1
            $key = $this->definitionRefResolver->getMethodDefinitionId(
129 1
                $method,
130 1
                DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE
131
            );
132 1
            $list[$key] = $this->typeDocNormalizer->normalize($method->getResultDoc());
133
        }
134
135 4
        return $list;
136
    }
137
138
    /**
139
     * @param ServerDoc $doc
140
     * @return array
141
     */
142 7
    protected function getDefaultSchemaList(ServerDoc $doc)
143
    {
144
        $propertyList = [
145 7
            'code' => [
146
                'type' => 'integer',
147
            ],
148
        ];
149
150 7
        $codeList = [];
151 7
        foreach ($doc->getServerErrorList() as $errorDoc) {
152 2
            $codeList[] = $errorDoc->getCode();
153
        }
154 7
        foreach ($doc->getGlobalErrorList() as $errorDoc) {
155 2
            $codeList[] = $errorDoc->getCode();
156
        }
157 7
        foreach ($doc->getMethodList() as $method) {
158 4
            foreach ($method->getCustomErrorList() as $errorDoc) {
159 4
                $codeList[] = $errorDoc->getCode();
160
            }
161
        }
162
163 7
        $codeList = array_unique($codeList);
164 7
        if (count($codeList) > 0) {
165 4
            $propertyList['code']['enum'] = $codeList;
166
        }
167
168
        return [
169
            'Default-Error' => [
170
                'allOf' => [
171 7
                    $this->shapeNormalizer->getErrorShapeDefinition(),
172
                    [
173 7
                        'type' => 'object',
174 7
                        'properties' => $propertyList,
175
                    ],
176
                ],
177
            ],
178
        ];
179
    }
180
181
    /**
182
     * @param array  $errorDocList
183
     * @param string $definitionType
184
     *
185
     * @return array
186
     */
187 7
    private function normalizeErrorList(array $errorDocList, $definitionType)
188
    {
189 7
        $list = [];
190 7
        foreach ($errorDocList as $errorDoc) {
191 4
            $key = $this->definitionRefResolver->getErrorDefinitionId($errorDoc, $definitionType);
192
193 4
            $list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
194
        }
195
196 7
        return $list;
197
    }
198
}
199