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