Completed
Pull Request — master (#49)
by John
05:45
created

OperationObject::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 12
nc 3
nop 3
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Document;
10
11
/**
12
 * @author John Kleijn <[email protected]>
13
 */
14
class OperationObject
15
{
16
    /**
17
     * @var SwaggerDocument
18
     */
19
    private $document;
20
21
    /**
22
     * @var object
23
     */
24
    private $definition;
25
26
    /**
27
     * @var string
28
     */
29
    private $path;
30
31
    /**
32
     * @var string
33
     */
34
    private $method;
35
36
    /**
37
     * @param SwaggerDocument $document
38
     * @param string          $path
39
     * @param string          $method
40
     */
41
    public function __construct(SwaggerDocument $document, $path, $method)
42
    {
43
        $paths = $document->getPathDefinitions();
44
45
        if (!property_exists($paths, $path)) {
46
            throw new \InvalidArgumentException("Path '$path' not in Swagger document");
47
        }
48
        $method = strtolower($method);
49
        if (!property_exists($paths->$path, $method)) {
50
            throw new \InvalidArgumentException("Method '$method' not supported for path '$path'");
51
        }
52
53
        $this->document = $document;
54
        $this->path = $path;
55
        $this->method = $method;
56
        $this->definition = $paths->$path->$method;
57
        $this->definition->{'x-request-schema'} = $this->assembleRequestSchema();
58
    }
59
60
    /**
61
     * @param object $definition
62
     * @param string $path
63
     * @param string $method
64
     *
65
     * @return static
66
     */
67
    public static function createFromOperationDefinition($definition, $path = '/', $method = 'GET')
68
    {
69
        $method = strtolower($method);
70
        $documentDefinition = (object)[
71
            'paths' => (object)[
72
                $path => (object)[
73
                    $method => $definition
74
                ]
75
            ]
76
        ];
77
        $document = new SwaggerDocument('', $documentDefinition);
78
79
        return new static($document, $path, $method);
80
    }
81
82
    /**
83
     * @return object
84
     */
85
    public function getRequestSchema()
86
    {
87
        return $this->definition->{'x-request-schema'};
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getPath()
94
    {
95
        return $this->path;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getMethod()
102
    {
103
        return $this->method;
104
    }
105
106
    /**
107
     * @return object
108
     */
109
    public function getDefinition()
110
    {
111
        return $this->definition;
112
    }
113
114
    /**
115
     * @return object
116
     */
117
    private function assembleRequestSchema()
118
    {
119
        if (!isset($this->definition->parameters)) {
120
            return new \stdClass;
121
        }
122
        $schema = new \stdClass;
123
        $schema->type = 'object';
124
        $schema->required = [];
125
        $schema->properties = new \stdClass;
126
127
        foreach ($this->definition->parameters as $paramDefinition) {
128
            if (isset($paramDefinition->required) && $paramDefinition->required) {
129
                $schema->required[] = $paramDefinition->name;
130
            }
131
            if ($paramDefinition->in === 'body') {
132
                $schema->properties->{$paramDefinition->name} = property_exists($paramDefinition, 'schema')
133
                    ? $paramDefinition->schema
134
                    : (object)['type' => 'object'];
135
                continue;
136
            }
137
138
            $type = property_exists($paramDefinition, 'type') ? $paramDefinition->type : 'string';
139
            $schema->properties->{$paramDefinition->name} = (object)['type' => $type];
140
        }
141
142
        return $schema;
143
    }
144
}
145