Passed
Push — develop ( e19e63...191701 )
by Daniel
05:18
created

SwaggerDecorator::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 29
nc 1
nop 3
dl 0
loc 47
ccs 0
cts 16
cp 0
crap 2
rs 9.0303
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
        $patchOpPath = '/component/forms/{id}/submit';
22
23
        $patchOp = $docs['paths'][$patchOpPath]['patch'];
24
        $patchOp['summary'] = 'Submit a single input for validation';
25
        $patchOp['parameters'] = $docs['paths']['/component/forms/{id}']['get']['parameters'];
26
        $patchOp['parameters'][] = [
27
            'name' => 'form',
28
            'in' => 'body',
29
            'required' => false,
30
            'schema' => [
31
                'type' => 'object',
32
                'properties' => [
33
                    'form_name' => [
34
                        'type' => 'object',
35
                        'properties' => [
36
                            'input_name' => [
37
                                'type' => 'string'
38
                            ]
39
                        ]
40
                    ]
41
                ]
42
            ]
43
        ];
44
        $patchOp['responses'] = $docs['paths']['/component/forms/{id}']['get']['responses'];
45
        $patchOp['responses']['200']['description'] = 'Validation passed successfully';
46
        $patchOp['responses']['400'] = [
47
            'description' => 'Validation failed',
48
            'schema' => [
49
                '$ref' => '#/definitions/Form-page'
50
            ]
51
        ];
52
        // $patchOp['responses']['406']['description'] = "Invalid field name for the form ID";
53
        $patchOp['consumes'] = $docs['paths']['/component/forms/{id}']['put']['consumes'];
54
        $patchOp['produces'] = $docs['paths']['/component/forms/{id}']['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
        // $docs['paths'][$patchOpPath]['post']['parameters'] = $patchOp['parameters'];
60
        // $docs['paths'][$patchOpPath]['post']['responses']['201']['description'] = "Form successfully submitted and valid";
61
62
        return $docs;
63
    }
64
65 2
    public function supportsNormalization($data, $format = null)
66
    {
67 2
        return $this->decorated->supportsNormalization($data, $format);
68
    }
69
}
70