ExternalSchemaListDocNormalizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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