Completed
Push — master ( ba30b6...904d28 )
by Christian
13:26
created

TensideApiDocHandler   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 100
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 25 6
D convertField() 0 28 9
A inferType() 0 9 3
1
<?php
2
3
/**
4
 * This file is part of tenside/core.
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
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core
18
 * @filesource
19
 */
20
21
namespace Tenside\CoreBundle;
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
use Tenside\CoreBundle\Annotation\ApiDescription;
28
29
/**
30
 * This class parses the API annotation.
31
 */
32
class TensideApiDocHandler implements HandlerInterface
33
{
34
    /**
35
     * The field names of parameters to convert by default.
36
     *
37
     * @var array
38
     */
39
    public static $convertFields = [
40
        'default',
41
        'description',
42
        'format',
43
        'actualType',
44
        'subType',
45
        'sinceVersion',
46
        'untilVersion',
47
        'requirement'
48
    ];
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
54
    {
55
        foreach ($annotations as $description) {
56
            if (!($description instanceof ApiDescription)) {
57
                continue;
58
            }
59
60
            $current = $annotation->toArray();
61
62
            $request = [];
63
            foreach ($description->getRequest() as $name => $field) {
64
                $request[$name] = $this->convertField($field);
65
            }
66
            $annotation->setParameters($request);
67
68
            if (!isset($current['response'])) {
69
                $response = [];
70
                foreach ($description->getResponse() as $name => $field) {
71
                    $response[$name] = $this->convertField($field);
72
                }
73
74
                $annotation->setResponse($response);
75
            }
76
        }
77
    }
78
79
    /**
80
     * Convert the annotation for a field.
81
     *
82
     * @param array $array The information for the field.
83
     *
84
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|boolean>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
85
     */
86
    private function convertField($array)
87
    {
88
        $result = [];
89
90
        // Copy over well known keys.
91
        foreach (static::$convertFields as $key) {
92
            if (isset($array[$key])) {
93
                $result[$key] = $array[$key];
94
            }
95
        }
96
97
        if (isset($array['dataType'])) {
98
            $result['dataType'] = $this->inferType($array['dataType']);
99
        } else {
100
            $result['dataType'] = isset($array['children']) ? 'object' : DataTypes::STRING;
101
        }
102
103
        $result['required'] = isset($array['children']) && (bool) $array['children'];
104
        $result['readonly'] = isset($array['readonly']) && (bool) $array['readonly'];
105
106
        if (isset($array['children'])) {
107
            foreach ($array['children'] as $key => $value) {
108
                $result['children'][$key] = $this->convertField($value);
109
            }
110
        }
111
112
        return $result;
113
    }
114
115
    /**
116
     * Convert the type.
117
     *
118
     * @param string $type The type name.
119
     *
120
     * @return string
121
     */
122
    public function inferType($type)
123
    {
124
        if (DataTypes::isPrimitive($type)) {
125
            return $type;
126
        } elseif (DataTypes::COLLECTION === strtolower($type)) {
127
            return $type;
128
        }
129
        return DataTypes::STRING;
130
    }
131
}
132