Passed
Push — develop ( 75d1fe...ccc913 )
by Daniel
06:14
created

SwaggerDecorator::supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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