Completed
Push — master ( 3247cf...1ea949 )
by Christian
09:40
created

TensideApiDocHandler   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 21
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 121
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 25 6
C convertField() 0 24 7
B convertChildren() 0 15 5
A inferType() 0 9 3
1
<?php
2
3
/**
4
 * This file is part of tenside/core-bundle.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core-bundle
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core-bundle/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core-bundle
18
 * @filesource
19
 */
20
21
namespace Tenside\CoreBundle\Annotation;
22
23
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
24
use Nelmio\ApiDocBundle\DataTypes;
25
use Nelmio\ApiDocBundle\Extractor\HandlerInterface;
26
use Symfony\Component\Routing\Route;
27
28
/**
29
 * This class parses the API annotation.
30
 */
31
class TensideApiDocHandler implements HandlerInterface
32
{
33
    /**
34
     * The field names of parameters to convert by default.
35
     *
36
     * @var array
37
     */
38
    public static $convertFields = [
39
        'default',
40
        'description',
41
        'format',
42
        'actualType',
43
        'subType',
44
        'sinceVersion',
45
        'untilVersion',
46
        'requirement'
47
    ];
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
53
    {
54
        foreach ($annotations as $description) {
55
            if (!($description instanceof ApiDescription)) {
56
                continue;
57
            }
58
59
            $current = $annotation->toArray();
60
61
            $request = [];
62
            foreach ($description->getRequest() as $name => $field) {
63
                $request[$name] = $this->convertField($field);
64
            }
65
            $annotation->setParameters($request);
66
67
            if (!isset($current['response'])) {
68
                $response = [];
69
                foreach ($description->getResponse() as $name => $field) {
70
                    $response[$name] = $this->convertField($field);
71
                }
72
73
                $annotation->setResponse($response);
74
            }
75
        }
76
    }
77
78
    /**
79
     * Convert the annotation for a field.
80
     *
81
     * @param array $array The information for the field.
82
     *
83
     * @return array
84
     */
85
    private function convertField($array)
86
    {
87
        $result = [];
88
89
        // Copy over well known keys.
90
        foreach (static::$convertFields as $key) {
91
            if (isset($array[$key])) {
92
                $result[$key] = $array[$key];
93
            }
94
        }
95
96
        if (isset($array['dataType'])) {
97
            $result['dataType'] = $this->inferType($array['dataType']);
98
        } else {
99
            $result['dataType'] = isset($array['children']) ? 'object' : DataTypes::STRING;
100
        }
101
102
        $result['required'] = isset($array['required']) && (bool) $array['required'];
103
        $result['readonly'] = isset($array['readonly']) && (bool) $array['readonly'];
104
105
        $result = $this->convertChildren($array, $result);
106
107
        return $result;
108
    }
109
110
    /**
111
     * Convert the children key of an array.
112
     *
113
     * @param array $array  The source array.
114
     *
115
     * @param array $result The partly converted array.
116
     *
117
     * @return array
118
     */
119
    private function convertChildren($array, $result)
120
    {
121
        if (isset($array['children'])) {
122
            foreach ($array['children'] as $key => $value) {
123
                $result['children'][$key] = $this->convertField($value);
124
                if (isset($result['children'][$key]['required'])) {
125
                    $result['required'] = $result['required'] || (bool) $result['children'][$key]['required'];
126
                }
127
            }
128
129
            return $result;
130
        }
131
132
        return $result;
133
    }
134
135
    /**
136
     * Convert the type.
137
     *
138
     * @param string $type The type name.
139
     *
140
     * @return string
141
     */
142
    public function inferType($type)
143
    {
144
        if (DataTypes::isPrimitive($type)) {
145
            return $type;
146
        } elseif (DataTypes::COLLECTION === strtolower($type)) {
147
            return $type;
148
        }
149
        return DataTypes::STRING;
150
    }
151
}
152