Passed
Push — release/1.0.0 ( 02b4c6...f79e53 )
by Yo
01:58 queued 34s
created

ResponseDocNormalizer::getMethodResultArrayDoc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 2
rs 9.9666
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 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
                    return ['$ref' => $self->definitionRefResolver->getDefinitionRef($errorRef)];
98 2
                },
99 2
                $method->getGlobalErrorRefList()
100
            ),
101 2
            array_map(
102
                function (ErrorDoc $errorDoc) use ($self) {
103
                    return [
104
                        '$ref' => $self->definitionRefResolver->getDefinitionRef(
105
                            $self->definitionRefResolver->getErrorDefinitionId(
106
                                $errorDoc,
107
                                DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
108
                            )
109
                        )
110
                    ];
111 2
                },
112 2
                $method->getCustomErrorList()
113
            ),
114 2
            $extraErrorDefinitionIdRefList
115
        );
116
117 2
        if (count($errorDocList) > 0) {
118
            return ['oneOf' => $errorDocList];
119
        }
120
121 2
        return ['type' => 'object'];
122
    }
123
}
124