Completed
Push — master ( 63f157...ad3633 )
by John
06:56
created

Operation::getResponses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions 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\PhpApi\Descriptions\Description;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
12
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\VisiteeMixin;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class Operation implements Element
18
{
19
    use VisiteeMixin;
20
21
    /**
22
     * @var string
23
     */
24
    protected $id;
25
26
27
    /**
28
     * @var string
29
     */
30
    protected $path;
31
32
    /**
33
     * @var string
34
     */
35
    protected $method;
36
37
    /**
38
     * @var Parameter[]
39
     */
40
    protected $parameters;
41
42
    /**
43
     * @var Schema
44
     */
45
    protected $requestSchema;
46
47
    /**
48
     * @var Response[]
49
     */
50
    protected $responses;
51
52
    /**
53
     * @var array
54
     */
55
    private $extensions;
56
57
    /**
58
     * Operation constructor.
59
     *
60
     * @param string      $id
61
     * @param string      $path
62
     * @param string      $method
63
     * @param Parameter[] $parameters
64
     * @param Schema      $requestSchema
65
     * @param Response[]  $responses
66
     * @param array       $extensions
67
     */
68
    public function __construct(
69
        string $id,
70
        string $path,
71
        string $method,
72
        array $parameters = [],
73
        Schema $requestSchema = null,
74
        array $responses = [],
75
        array $extensions = []
76
    ) {
77
        $this->id            = $id;
78
        $this->path          = $path;
79
        $this->method        = $method;
80
        $this->parameters    = $parameters;
81
        $this->requestSchema = $requestSchema;
82
        $this->responses     = $responses;
83
        $this->extensions    = $extensions;
84
    }
85
86
    /**
87
     * @param string $name
88
     *
89
     * @return mixed
90
     */
91
    public function getExtension(string $name)
92
    {
93
        return isset($this->extensions[$name]) ? $this->extensions[$name] : null;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getPath(): string
100
    {
101
        return $this->path;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getId(): string
108
    {
109
        return $this->id;
110
    }
111
112
    /**
113
     * @return int[]
114
     */
115
    public function getStatusCodes(): array
116
    {
117
        return array_keys($this->responses);
118
    }
119
120
    /**
121
     * @param int $code
122
     *
123
     * @return Response
124
     */
125
    public function getResponse(int $code): Response
126
    {
127
        if (!isset($this->responses[$code])) {
128
            if (isset($this->responses[0])) {
129
                // Return default response
130
                return $this->responses[0];
131
            }
132
            throw new \InvalidArgumentException(
133
                "Operation '{$this->getId()}' does not have a definition for status '$code'" .
134
                " (has " . implode(', ', array_keys($this->responses)) . ')'
135
            );
136
        }
137
138
        return $this->responses[$code];
139
    }
140
141
    /**
142
     * @return Response[]
143
     */
144
    public function getResponses()
145
    {
146
        return array_values($this->responses);
147
    }
148
149
    /**
150
     * @return Schema
151
     */
152
    public function getRequestSchema(): Schema
153
    {
154
        return $this->requestSchema;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function hasParameters(): bool
161
    {
162
        return (bool)count($this->parameters);
163
    }
164
165
    /**
166
     * @return  Parameter[]
167
     */
168
    public function getParameters(): array
169
    {
170
        return $this->hasParameters() ? $this->parameters : [];
171
    }
172
173
    /**
174
     * @param string $path
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
175
     * @param string $method
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
176
     *
177
     * @return Parameter|null
178
     */
179
    public function getRequestBodyParameter()
180
    {
181
        foreach ($this->getParameters() as $parameter) {
182
            if ($parameter->getIn() === Parameter::IN_BODY) {
183
                return $parameter;
184
            }
185
        }
186
    }
187
188
    /**
189
     * @param string $name
190
     *
191
     * @return Parameter
192
     */
193
    public function getParameter(string $name): Parameter
194
    {
195
        foreach ($this->getParameters() as $parameter) {
196
            if ($parameter->getName() === $name) {
197
                return $parameter;
198
            }
199
        }
200
        throw new \OutOfBoundsException("Parameter '$name' does not exist");
201
    }
202
203
    /**
204
     * @return string
205
     */
206
    public function getMethod(): string
207
    {
208
        return $this->method;
209
    }
210
}
211