1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwaggerGen\Swagger; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Describes a Swagger Operation object, which describes a method call of a |
7
|
|
|
* specific endpoint. |
8
|
|
|
* |
9
|
|
|
* @package SwaggerGen |
10
|
|
|
* @author Martijn van der Lee <[email protected]> |
11
|
|
|
* @copyright 2014-2015 Martijn van der Lee |
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
13
|
|
|
*/ |
14
|
|
|
class Operation extends AbstractDocumentableObject |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
private $tags = array(); |
18
|
|
|
private $summary; |
19
|
|
|
private $description; |
20
|
|
|
private $operationid; |
21
|
|
|
private $consumes = array(); |
22
|
|
|
private $produces = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var IParameter[] |
26
|
|
|
*/ |
27
|
|
|
private $parameters = array(); |
28
|
|
|
private $responses = array(); |
29
|
|
|
private $schemes = array(); |
30
|
|
|
private $deprecated = false; |
31
|
|
|
private $security = array(); |
32
|
|
|
|
33
|
|
|
public function getConsumes() |
34
|
|
|
{ |
35
|
|
|
return $this->consumes; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function __construct(AbstractObject $parent, $summary = null, Tag $tag = null) |
39
|
|
|
{ |
40
|
|
|
parent::__construct($parent); |
41
|
|
|
$this->summary = $summary; |
42
|
|
|
$this->operationid = uniqid('', true); //@todo getSwagger()->title -> Do some complex construction? |
43
|
|
|
if ($tag) { |
44
|
|
|
$this->tags[] = $tag->getName(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function handleCommand($command, $data = null) |
49
|
|
|
{ |
50
|
|
|
switch (strtolower($command)) { |
51
|
|
|
// string |
52
|
|
|
case 'summary': |
53
|
|
|
case 'description': |
54
|
|
|
$this->$command = $data; |
55
|
|
|
return $this; |
56
|
|
|
|
57
|
|
|
// string[] |
58
|
|
|
case 'tags': |
59
|
|
View Code Duplication |
case 'schemes': |
|
|
|
|
60
|
|
|
$this->$command = array_merge($this->$command, self::wordSplit($data)); |
61
|
|
|
return $this; |
62
|
|
|
|
63
|
|
|
// MIME[] |
64
|
|
|
case 'consumes': |
65
|
|
View Code Duplication |
case 'produces': |
|
|
|
|
66
|
|
|
$this->$command = array_merge($this->$command, self::translateMimeTypes(self::wordSplit($data))); |
67
|
|
|
return $this; |
68
|
|
|
|
69
|
|
|
// boolean |
70
|
|
|
case 'deprecated': |
71
|
|
|
$this->deprecated = true; |
72
|
|
|
return $this; |
73
|
|
|
|
74
|
|
|
case 'error': |
75
|
|
|
$code = self::wordShift($data); |
76
|
|
|
$reasoncode = Response::getCode($code); |
77
|
|
|
if ($reasoncode === null) { |
78
|
|
|
throw new \SwaggerGen\Exception("Invalid error code: '$code'"); |
79
|
|
|
} |
80
|
|
|
$description = $data; |
81
|
|
|
$Error = new Error($this, $reasoncode, $description); |
82
|
|
|
$this->responses[$reasoncode] = $Error; |
83
|
|
|
return $Error; |
84
|
|
|
|
85
|
|
|
case 'errors': |
86
|
|
|
foreach (self::wordSplit($data) as $code) { |
87
|
|
|
$reasoncode = Response::getCode($code); |
88
|
|
|
if ($reasoncode === null) { |
89
|
|
|
throw new \SwaggerGen\Exception("Invalid error code: '$code'"); |
90
|
|
|
} |
91
|
|
|
$this->responses[$reasoncode] = new Error($this, $reasoncode); |
92
|
|
|
} |
93
|
|
|
return $this; |
94
|
|
|
|
95
|
|
|
case 'path': |
96
|
|
|
case 'query': case 'query?': |
|
|
|
|
97
|
|
|
case 'header': case 'header?': |
|
|
|
|
98
|
|
|
case 'form': case 'form?': |
|
|
|
|
99
|
|
|
$in = rtrim($command, '?'); |
100
|
|
|
$Parameter = new Parameter($this, $in, $data, substr($command, -1) !== '?'); |
101
|
|
|
$this->parameters[$Parameter->getName()] = $Parameter; |
102
|
|
|
return $Parameter; |
103
|
|
|
|
104
|
|
|
case 'body': case 'body?': |
|
|
|
|
105
|
|
|
$Parameter = new BodyParameter($this, $data, substr($command, -1) !== '?'); |
106
|
|
|
$this->parameters[$Parameter->getName()] = $Parameter; |
107
|
|
|
return $Parameter; |
108
|
|
|
|
109
|
|
|
case 'response': |
110
|
|
|
$code = self::wordShift($data); |
111
|
|
|
$reasoncode = Response::getCode($code); |
112
|
|
|
if ($reasoncode === null) { |
113
|
|
|
throw new \SwaggerGen\Exception("Invalid response code: '$code'"); |
114
|
|
|
} |
115
|
|
|
$definition = self::wordShift($data); |
116
|
|
|
$description = $data; |
117
|
|
|
$Response = new Response($this, $reasoncode, $definition, $description); |
118
|
|
|
$this->responses[$reasoncode] = $Response; |
119
|
|
|
return $Response; |
120
|
|
|
|
121
|
|
View Code Duplication |
case 'require': |
|
|
|
|
122
|
|
|
$name = self::wordShift($data); |
123
|
|
|
if (empty($name)) { |
124
|
|
|
throw new \SwaggerGen\Exception('Empty security requirement name'); |
125
|
|
|
} |
126
|
|
|
$scopes = self::wordSplit($data); |
127
|
|
|
sort($scopes); |
128
|
|
|
$this->security[] = array( |
129
|
|
|
$name => empty($scopes) ? array() : $scopes, |
130
|
|
|
); |
131
|
|
|
return $this; |
132
|
|
|
|
133
|
|
|
//@todo operationId |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return parent::handleCommand($command, $data); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function toArray() |
140
|
|
|
{ |
141
|
|
|
if (empty($this->responses)) { |
142
|
|
|
throw new \SwaggerGen\Exception('No response defined for operation'); |
143
|
|
|
} |
144
|
|
|
ksort($this->responses); |
145
|
|
|
|
146
|
|
|
$tags = array_unique($this->tags); |
147
|
|
|
sort($tags); |
148
|
|
|
|
149
|
|
|
$schemes = array_unique($this->schemes); |
150
|
|
|
sort($schemes); |
151
|
|
|
|
152
|
|
|
$consumes = array_unique($this->consumes); |
153
|
|
|
sort($consumes); |
154
|
|
|
|
155
|
|
|
$produces = array_unique($this->produces); |
156
|
|
|
sort($produces); |
157
|
|
|
|
158
|
|
|
foreach ($this->security as $security) { |
159
|
|
|
foreach ($security as $name => $scope) { |
160
|
|
|
if ($this->getRoot()->getSecurity($name) === false) { |
161
|
|
|
throw new \SwaggerGen\Exception("Required security scheme not defined: '{$name}'"); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$parameters = $this->parameters ? array_values($this->parameters) : null; |
167
|
|
|
|
168
|
|
|
return self::arrayFilterNull(array_merge(array( |
169
|
|
|
'deprecated' => $this->deprecated ? true : null, |
170
|
|
|
'tags' => $tags, |
171
|
|
|
'summary' => empty($this->summary) ? null : $this->summary, |
172
|
|
|
'description' => empty($this->description) ? null : $this->description, |
173
|
|
|
//'operationId' => $this->description, |
|
|
|
|
174
|
|
|
'consumes' => $consumes, |
175
|
|
|
'produces' => $produces, |
176
|
|
|
'parameters' => $parameters ? self::objectsToArray($parameters) : null, |
177
|
|
|
'schemes' => $schemes, |
178
|
|
|
'responses' => $this->responses ? self::objectsToArray($this->responses) : null, |
179
|
|
|
'security' => $this->security, |
180
|
|
|
), parent::toArray())); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function __toString() |
184
|
|
|
{ |
185
|
|
|
return __CLASS__ . ' ' . $this->summary; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.