Passed
Push — master ( 02b4c6...8eef8d )
by Yo
46s
created

ExternalSchemaListDocNormalizer::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component;
3
4
use Yoanm\JsonRpcHttpServerOpenAPIDoc\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 TypeDocNormalizer */
14
    private $typeDocNormalizer;
15
    /** @var DefinitionRefResolver */
16
    private $definitionRefResolver;
17
    /** @var ErrorDocNormalizer */
18
    private $errorDocNormalizer;
19
20
    /**
21
     * @param DefinitionRefResolver  $definitionRefResolver
22
     * @param TypeDocNormalizer $typeDocNormalizer
23
     * @param ErrorDocNormalizer     $errorDocNormalizer
24
     */
25 5
    public function __construct(
26
        DefinitionRefResolver $definitionRefResolver,
27
        TypeDocNormalizer $typeDocNormalizer,
28
        ErrorDocNormalizer $errorDocNormalizer
29
    ) {
30 5
        $this->definitionRefResolver = $definitionRefResolver;
31 5
        $this->typeDocNormalizer = $typeDocNormalizer;
32 5
        $this->errorDocNormalizer = $errorDocNormalizer;
33 5
    }
34
35
    /**
36
     * @param ServerDoc $doc
37
     *
38
     * @return array
39
     *
40
     * @throws \ReflectionException
41
     */
42 5
    public function normalize(ServerDoc $doc) : array
43
    {
44 5
        return array_merge(
45 5
            $this->getMethodsExternalSchemaList($doc),
46 5
            $this->getMethodErrorsExternalSchemaList($doc),
47 5
            $this->getServerErrorsExtraSchemaList($doc)
48
        );
49
    }
50
51
    /**
52
     * @param ServerDoc $doc
53
     *
54
     * @return array
55
     *
56
     * @throws \ReflectionException
57
     */
58 5
    protected function getMethodsExternalSchemaList(ServerDoc $doc) : array
59
    {
60 5
        $list = [];
61 5
        foreach ($doc->getMethodList() as $method) {
62
            // Merge extra definitions
63 3
            $list = array_merge($list, $this->getMethodExternalSchemaList($method));
64
        }
65
66 5
        return $list;
67
    }
68
69
    /**
70
     * @param ServerDoc $doc
71
     *
72
     * @return array
73
     *
74
     * @throws \ReflectionException
75
     */
76 5
    protected function getMethodErrorsExternalSchemaList(ServerDoc $doc) : array
77
    {
78 5
        $list = [];
79 5
        foreach ($doc->getMethodList() as $method) {
80 3
            foreach ($method->getCustomErrorList() as $errorDoc) {
81 1
                $key = $this->definitionRefResolver->getErrorDefinitionId(
82 1
                    $errorDoc,
83 1
                    DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
84
                );
85 3
                $list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
86
            }
87
        }
88
89 5
        return $list;
90
    }
91
92
93
    /**
94
     * @param ServerDoc $doc
95
     *
96
     * @return array
97
     *
98
     * @throws \ReflectionException
99
     */
100 5
    protected function getServerErrorsExtraSchemaList(ServerDoc $doc) : array
101
    {
102 5
        $list = [];
103 5
        foreach ($doc->getGlobalErrorList() as $errorDoc) {
104 1
            $key = $this->definitionRefResolver->getErrorDefinitionId(
105 1
                $errorDoc,
106 1
                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
107
            );
108 1
            $list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
109
        }
110
111 5
        foreach ($doc->getServerErrorList() as $errorDoc) {
112 1
            $key = $this->definitionRefResolver->getErrorDefinitionId(
113 1
                $errorDoc,
114 1
                DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE
115
            );
116 1
            $list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
117
        }
118
119 5
        return $list;
120
    }
121
122
    /**
123
     * @param MethodDoc $method
124
     *
125
     * @return array[]
126
     *
127
     * @throws \ReflectionException
128
     */
129 3
    protected function getMethodExternalSchemaList(MethodDoc $method) : array
130
    {
131 3
        $list = [];
132
        // Create request params schema if provided
133 3
        if (null !== $method->getParamsDoc()) {
134 1
            $key = $this->definitionRefResolver->getMethodDefinitionId(
135 1
                $method,
136 1
                DefinitionRefResolver::METHOD_PARAMS_DEFINITION_TYPE
137
            );
138 1
            $list[$key] = $this->typeDocNormalizer->normalize($method->getParamsDoc());
139
        }
140
141
        // Create custom result schema if provided
142 3
        if (null !== $method->getResultDoc()) {
143 1
            $key = $this->definitionRefResolver->getMethodDefinitionId(
144 1
                $method,
145 1
                DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE
146
            );
147 1
            $list[$key] = $this->typeDocNormalizer->normalize($method->getResultDoc());
148
        }
149
150 3
        return $list;
151
    }
152
}
153