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

ResponseDocNormalizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 77.27%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 118
ccs 34
cts 44
cp 0.7727
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A normalize() 0 18 2
A getMethodResultArrayDoc() 0 16 2
A getMethodErrorArrayDoc() 0 40 2
1
<?php
2
namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component;
3
4
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Helper\ArrayAppendHelperTrait;
5
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Resolver\DefinitionRefResolver;
6
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc;
7
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc;
8
9
/**
10
 * Class ResponseDocNormalizer
11
 */
12
class ResponseDocNormalizer
13
{
14
    use ArrayAppendHelperTrait;
15
16
    /** @var DefinitionRefResolver */
17
    private $definitionRefResolver;
18
    /** @var ShapeNormalizer */
19
    private $shapeNormalizer;
20
21
    /**
22
     * @param DefinitionRefResolver $definitionRefResolver
23
     * @param ShapeNormalizer       $shapeNormalizer
24
     */
25 2
    public function __construct(
26
        DefinitionRefResolver $definitionRefResolver,
27
        ShapeNormalizer $shapeNormalizer
28
    ) {
29 2
        $this->definitionRefResolver = $definitionRefResolver;
30 2
        $this->shapeNormalizer = $shapeNormalizer;
31 2
    }
32
33
    /**
34
     * @param MethodDoc $method
35
     * @param array     $extraErrorDefinitionIdRefList
36
     *
37
     * @return array
38
     */
39 2
    public function normalize(MethodDoc $method, array $extraErrorDefinitionIdRefList = [])
40
    {
41 2
        $responseErrorShape = [];
42 2
        $errorArrayDoc = $this->getMethodErrorArrayDoc($method, $extraErrorDefinitionIdRefList);
43 2
        if (count($errorArrayDoc) > 0) {
44
            $responseErrorShape = [
45 2
                'type' => 'object',
46 2
                'properties' => ['error' => $errorArrayDoc],
47
            ];
48
        }
49
        return [
50
            'allOf' => [
51 2
                $this->shapeNormalizer->getResponseShapeDefinition(),
52
                [
53 2
                    'type' => 'object',
54 2
                    'properties' => ['result' => $this->getMethodResultArrayDoc($method)],
55
                ],
56 2
                $responseErrorShape,
57
            ],
58
        ];
59
    }
60
61
    /**
62
     * @param MethodDoc $method
63
     *
64
     * @return array
65
     */
66 2
    protected function getMethodResultArrayDoc(MethodDoc $method)
67
    {
68 2
        if (null !== $method->getResultDoc()) {
69
            $result = [
70 1
                '$ref' => $this->definitionRefResolver->getDefinitionRef(
71 1
                    $this->definitionRefResolver->getMethodDefinitionId(
72 1
                        $method,
73 1
                        DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE
74
                    )
75
                )
76
            ];
77
        } else {
78 1
            $result = ['description' => 'Method result'];
79
        }
80
81 2
        return $result;
82
    }
83
84
    /**
85
     * @param MethodDoc $method
86
     * @param string[]  $extraErrorDefinitionIdRefList
87
     *
88
     * @return array
89
     */
90 2
    protected function getMethodErrorArrayDoc(MethodDoc $method, array $extraErrorDefinitionIdRefList = [])
91
    {
92 2
        $self = $this;
93
94 2
        $errorDocList = array_merge(
95 2
            array_map(
96
                function ($errorRef) use ($self) {
97
                    $errorDoc = new ErrorDoc('', 0, null, null, $errorRef);
98
                    return [
99
                        '$ref' => $self->definitionRefResolver->getDefinitionRef(
100
                            $self->definitionRefResolver->getErrorDefinitionId(
101
                                $errorDoc,
102
                                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
103
                            )
104
                        )
105
                    ];
106 2
                },
107 2
                $method->getGlobalErrorRefList()
108
            ),
109 2
            array_map(
110
                function (ErrorDoc $errorDoc) use ($self) {
111
                    return [
112
                        '$ref' => $self->definitionRefResolver->getDefinitionRef(
113
                            $self->definitionRefResolver->getErrorDefinitionId(
114
                                $errorDoc,
115
                                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
116
                            )
117
                        )
118
                    ];
119 2
                },
120 2
                $method->getCustomErrorList()
121
            ),
122 2
            $extraErrorDefinitionIdRefList
123
        );
124
125 2
        if (count($errorDocList) > 0) {
126
            return ['oneOf' => $errorDocList];
127
        }
128
129 2
        return ['type' => 'object'];
130
    }
131
}
132