Passed
Push — release/1.0.0 ( 02b4c6...f79e53 )
by Yo
01:58 queued 34s
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
     * @return array
38
     */
39 5
    public function normalize(ServerDoc $doc)
40
    {
41 5
        return array_merge(
42 5
            $this->getMethodsExternalSchemaList($doc),
43 5
            $this->getMethodErrorsExternalSchemaList($doc),
44 5
            $this->getServerErrorsExtraSchemaList($doc)
45
        );
46
    }
47
48
    /**
49
     * @param ServerDoc $doc
50
     *
51
     * @return array
52
     */
53 5
    protected function getMethodsExternalSchemaList(ServerDoc $doc)
54
    {
55 5
        $list = [];
56 5
        foreach ($doc->getMethodList() as $method) {
57
            // Merge extra definitions
58 3
            $list = array_merge($list, $this->getMethodExternalSchemaList($method));
59
        }
60
61 5
        return $list;
62
    }
63
64
    /**
65
     * @param ServerDoc $doc
66
     *
67
     * @return array
68
     */
69 5
    protected function getMethodErrorsExternalSchemaList(ServerDoc $doc)
70
    {
71 5
        $list = [];
72 5
        foreach ($doc->getMethodList() as $method) {
73 3
            foreach ($method->getCustomErrorList() as $errorDoc) {
74 1
                $key = $this->definitionRefResolver->getErrorDefinitionId(
75 1
                    $errorDoc,
76 1
                    DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
77
                );
78 3
                $list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
79
            }
80
        }
81
82 5
        return $list;
83
    }
84
85
86
    /**
87
     * @return array
88
     */
89 5
    protected function getServerErrorsExtraSchemaList(ServerDoc $doc)
90
    {
91 5
        $list = [];
92 5
        foreach ($doc->getGlobalErrorList() as $errorDoc) {
93 1
            $key = $this->definitionRefResolver->getErrorDefinitionId(
94 1
                $errorDoc,
95 1
                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
96
            );
97 1
            $list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
98
        }
99
100 5
        foreach ($doc->getServerErrorList() as $errorDoc) {
101 1
            $key = $this->definitionRefResolver->getErrorDefinitionId(
102 1
                $errorDoc,
103 1
                DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE
104
            );
105 1
            $list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
106
        }
107
108 5
        return $list;
109
    }
110
111
    /**
112
     * @param MethodDoc $method
113
     *
114
     * @return array[]
115
     */
116 3
    protected function getMethodExternalSchemaList(MethodDoc $method) : array
117
    {
118 3
        $list = [];
119
        // Create request params schema if provided
120 3
        if (null !== $method->getParamsDoc()) {
121 1
            $key = $this->definitionRefResolver->getMethodDefinitionId(
122 1
                $method,
123 1
                DefinitionRefResolver::METHOD_PARAMS_DEFINITION_TYPE
124
            );
125 1
            $list[$key] = $this->typeDocNormalizer->normalize($method->getParamsDoc());
126
        }
127
128
        // Create custom result schema if provided
129 3
        if (null !== $method->getResultDoc()) {
130 1
            $key = $this->definitionRefResolver->getMethodDefinitionId(
131 1
                $method,
132 1
                DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE
133
            );
134 1
            $list[$key] = $this->typeDocNormalizer->normalize($method->getResultDoc());
135
        }
136
137 3
        return $list;
138
    }
139
}
140