Passed
Push — develop ( 61061c...1ac38a )
by Daniel
04:56
created

SwaggerDecorator::normalize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 3
dl 0
loc 45
ccs 0
cts 17
cp 0
crap 2
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Swagger;
4
5
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
6
7
final class SwaggerDecorator implements NormalizerInterface
8
{
9
    private $decorated;
10
11 5
    public function __construct(NormalizerInterface $decorated)
12
    {
13 5
        $this->decorated = $decorated;
14 5
    }
15
16
    public function normalize($object, $format = null, array $context = [])
17
    {
18
        /** @var array $docs */
19
        $docs = $this->decorated->normalize($object, $format, $context);
20
21
        $currentPath = '/forms/{id}';
22
        $patchOpPath = $currentPath . '/submit';
23
24
        $patchOp = $docs['paths'][$patchOpPath]['patch'];
25
        $patchOp['summary'] = 'Submit a single input for validation';
26
        $patchOp['parameters'] = $docs['paths'][$currentPath]['get']['parameters'];
27
        $patchOp['parameters'][] = [
28
            'name' => 'form',
29
            'in' => 'body',
30
            'required' => false,
31
            'schema' => [
32
                'type' => 'object',
33
                'properties' => [
34
                    'form_name' => [
35
                        'type' => 'object',
36
                        'properties' => [
37
                            'input_name' => [
38
                                'type' => 'string'
39
                            ]
40
                        ]
41
                    ]
42
                ]
43
            ]
44
        ];
45
        $patchOp['responses'] = $docs['paths'][$currentPath]['get']['responses'];
46
        $patchOp['responses']['200']['description'] = 'Validation passed successfully';
47
        $patchOp['responses']['400'] = [
48
            'description' => 'Validation failed',
49
            'schema' => [
50
                '$ref' => '#/definitions/Form'
51
            ]
52
        ];
53
        $patchOp['consumes'] = $docs['paths'][$currentPath]['put']['consumes'];
54
        $patchOp['produces'] = $docs['paths'][$currentPath]['put']['produces'];
55
56
        $docs['paths'][$patchOpPath]['patch'] = $patchOp;
57
        $docs['paths'][$patchOpPath]['post']['parameters'] = $docs['paths'][$patchOpPath]['patch']['parameters'];
58
        $docs['paths'][$patchOpPath]['post']['summary'] = 'Submit and validate the entire form';
59
60
        return $docs;
61
    }
62
63 2
    public function supportsNormalization($data, $format = null)
64
    {
65 2
        return $this->decorated->supportsNormalization($data, $format);
66
    }
67
}
68