Passed
Pull Request — master (#26)
by Yo
04:47 queued 01:57
created

ResponseDocNormalizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 84
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A normalize() 0 34 1
A getMethodResultArrayDoc() 0 14 2
1
<?php
2
namespace Yoanm\JsonRpcHttpServerSwaggerDoc\App\Normalizer\Component;
3
4
use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Resolver\DefinitionRefResolver;
5
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc;
6
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc;
7
8
/**
9
 * Class ResponseDocNormalizer
10
 */
11
class ResponseDocNormalizer
12
{
13
    /** @var DefinitionRefResolver */
14
    private $definitionRefResolver;
15
    /** @var ShapeNormalizer */
16
    private $shapeNormalizer;
17
18
    /**
19
     * @param DefinitionRefResolver $definitionRefResolver
20
     * @param ShapeNormalizer       $shapeNormalizer
21
     */
22 3
    public function __construct(
23
        DefinitionRefResolver $definitionRefResolver,
24
        ShapeNormalizer $shapeNormalizer
25
    ) {
26 3
        $this->definitionRefResolver = $definitionRefResolver;
27 3
        $this->shapeNormalizer = $shapeNormalizer;
28 3
    }
29
30
    /**
31
     * @param MethodDoc $method
32
     *
33
     * @return array
34
     */
35 3
    public function normalize(MethodDoc $method) : array
36
    {
37
        return [
38 3
            'allOf' => array_merge(
39
                [
40 3
                    $this->shapeNormalizer->getResponseShapeDefinition(),
41
                    [
42 3
                        'type' => 'object',
43 3
                        'properties' => ['result' => $this->getMethodResultArrayDoc($method)],
44
                    ],
45
                ],
46 3
                array_map(
47
                    function ($errorIdentifier) {
48
                        return [
49 1
                            'type' => 'object',
50
                            'properties' => [
51
                                'error' => [
52 1
                                    '$ref' => $this->definitionRefResolver->getDefinitionRef(
53 1
                                        $this->definitionRefResolver->getErrorDefinitionId(
54 1
                                            new ErrorDoc('', 0, null, null, $errorIdentifier),
55 1
                                            DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
56
                                        )
57
                                    )
58
                                ]
59
                            ],
60
                        ];
61 3
                    },
62 3
                    $method->getGlobalErrorRefList()
63
                ),
64
                [
65
                    [
66 3
                        'type' => 'object',
67
                        'properties' => [
68 3
                            'error' => ['$ref' => $this->definitionRefResolver->getDefinitionRef('Default-Error')]
69
                        ],
70
                    ]
71
                ]
72
            )
73
        ];
74
    }
75
76
    /**
77
     * @param MethodDoc $method
78
     *
79
     * @return array
80
     */
81 3
    protected function getMethodResultArrayDoc(MethodDoc $method) : array
82
    {
83 3
        if (null !== $method->getResultDoc()) {
84
            return [
85 2
                '$ref' => $this->definitionRefResolver->getDefinitionRef(
86 2
                    $this->definitionRefResolver->getMethodDefinitionId(
87 2
                        $method,
88 2
                        DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE
89
                    )
90
                )
91
            ];
92
        }
93
94 1
        return ['description' => 'Method result'];
95
    }
96
}
97