Passed
Push — release/1.0.0 ( f79e53...fc1f78 )
by Yo
01:38
created

ResponseDocNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
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\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 4
    public function __construct(
26
        DefinitionRefResolver $definitionRefResolver,
27
        ShapeNormalizer $shapeNormalizer
28
    ) {
29 4
        $this->definitionRefResolver = $definitionRefResolver;
30 4
        $this->shapeNormalizer = $shapeNormalizer;
31 4
    }
32
33
    /**
34
     * @param MethodDoc $method
35
     * @param array     $extraErrorDefinitionIdRefList
36
     *
37
     * @return array
38
     */
39 4
    public function normalize(MethodDoc $method, array $extraErrorDefinitionIdRefList = [])
40
    {
41 4
        $responseErrorShape = [];
42 4
        $errorArrayDoc = $this->getMethodErrorArrayDoc($method, $extraErrorDefinitionIdRefList);
43 4
        if (count($errorArrayDoc) > 0) {
44
            $responseErrorShape = [
45 4
                'type' => 'object',
46 4
                'properties' => ['error' => $errorArrayDoc],
47
            ];
48
        }
49
        return [
50
            'allOf' => [
51 4
                $this->shapeNormalizer->getResponseShapeDefinition(),
52
                [
53 4
                    'type' => 'object',
54 4
                    'properties' => ['result' => $this->getMethodResultArrayDoc($method)],
55
                ],
56 4
                $responseErrorShape,
57
            ],
58
        ];
59
    }
60
61
    /**
62
     * @param MethodDoc $method
63
     *
64
     * @return array
65
     */
66 4
    protected function getMethodResultArrayDoc(MethodDoc $method)
67
    {
68 4
        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 3
            $result = ['description' => 'Method result'];
79
        }
80
81 4
        return $result;
82
    }
83
84
    /**
85
     * @param MethodDoc $method
86
     * @param string[]  $extraErrorDefinitionIdRefList
87
     *
88
     * @return array
89
     */
90 4
    protected function getMethodErrorArrayDoc(MethodDoc $method, array $extraErrorDefinitionIdRefList = [])
91
    {
92 4
        $self = $this;
93
94 4
        $errorDocList = array_merge(
95 4
            array_map(
96
                function ($errorRef) use ($self) {
97 1
                    $errorDoc = new ErrorDoc('', 0, null, null, $errorRef);
98
                    return [
99 1
                        '$ref' => $self->definitionRefResolver->getDefinitionRef(
100 1
                            $self->definitionRefResolver->getErrorDefinitionId(
101 1
                                $errorDoc,
102 1
                                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
103
                            )
104
                        )
105
                    ];
106 4
                },
107 4
                $method->getGlobalErrorRefList()
108
            ),
109 4
            array_map(
110
                function (ErrorDoc $errorDoc) use ($self) {
111
                    return [
112 1
                        '$ref' => $self->definitionRefResolver->getDefinitionRef(
113 1
                            $self->definitionRefResolver->getErrorDefinitionId(
114 1
                                $errorDoc,
115 1
                                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
116
                            )
117
                        )
118
                    ];
119 4
                },
120 4
                $method->getCustomErrorList()
121
            ),
122 4
            $extraErrorDefinitionIdRefList
123
        );
124
125 4
        if (count($errorDocList) > 0) {
126 2
            return ['oneOf' => $errorDocList];
127
        }
128
129 2
        return ['type' => 'object'];
130
    }
131
}
132