ResponseDocNormalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 84
ccs 52
cts 52
cp 1
rs 10
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 39
    public function __construct(
23
        DefinitionRefResolver $definitionRefResolver,
24
        ShapeNormalizer $shapeNormalizer
25
    ) {
26 39
        $this->definitionRefResolver = $definitionRefResolver;
27 39
        $this->shapeNormalizer = $shapeNormalizer;
28
    }
29
30
    /**
31
     * @param MethodDoc $method
32
     *
33
     * @return array
34
     */
35 9
    public function normalize(MethodDoc $method) : array
36
    {
37 9
        return [
38 9
            'allOf' => array_merge(
39 9
                [
40 9
                    $this->shapeNormalizer->getResponseShapeDefinition(),
41 9
                    [
42 9
                        'type' => 'object',
43 9
                        'properties' => ['result' => $this->getMethodResultArrayDoc($method)],
44 9
                    ],
45 9
                ],
46 9
                array_map(
47 9
                    function ($errorIdentifier) {
48 3
                        return [
49 3
                            'type' => 'object',
50 3
                            'properties' => [
51 3
                                'error' => [
52 3
                                    '$ref' => $this->definitionRefResolver->getDefinitionRef(
53 3
                                        $this->definitionRefResolver->getErrorDefinitionId(
54 3
                                            new ErrorDoc('', 0, null, null, $errorIdentifier),
55 3
                                            DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
56 3
                                        )
57 3
                                    )
58 3
                                ]
59 3
                            ],
60 3
                        ];
61 9
                    },
62 9
                    $method->getGlobalErrorRefList()
63 9
                ),
64 9
                [
65 9
                    [
66 9
                        'type' => 'object',
67 9
                        'properties' => [
68 9
                            'error' => ['$ref' => $this->definitionRefResolver->getDefinitionRef('Default-Error')]
69 9
                        ],
70 9
                    ]
71 9
                ]
72 9
            )
73 9
        ];
74
    }
75
76
    /**
77
     * @param MethodDoc $method
78
     *
79
     * @return array
80
     */
81 9
    protected function getMethodResultArrayDoc(MethodDoc $method) : array
82
    {
83 9
        if (null !== $method->getResultDoc()) {
84 4
            return [
85 4
                '$ref' => $this->definitionRefResolver->getDefinitionRef(
86 4
                    $this->definitionRefResolver->getMethodDefinitionId(
87 4
                        $method,
88 4
                        DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE
89 4
                    )
90 4
                )
91 4
            ];
92
        }
93
94 5
        return ['description' => 'Method result'];
95
    }
96
}
97