Completed
Push — master ( 1a3eb9...f4b0a7 )
by John
02:06
created

Description::getBasePath()   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\Document\Document;
12
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
13
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\ClosureVisitorScope;
14
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\Visitee;
15
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\VisiteeMixin;
16
17
/**
18
 * @author John Kleijn <[email protected]>
19
 */
20
class Description implements Visitee, ClosureVisitorScope
21
{
22
    use VisiteeMixin;
23
24
    /**
25
     * @var Path[]
26
     */
27
    protected $paths;
28
29
    /**
30
     * @var ComplexType[]
31
     */
32
    protected $complexTypes;
33
34
    /**
35
     * @var string
36
     */
37
    protected $host;
38
39
    /**
40
     * @var array
41
     */
42
    protected $schemes = [];
43
44
    /**
45
     * @var array
46
     */
47
    protected $extensions = [];
48
49
    /**
50
     * @var Document
51
     */
52
    protected $document;
53
54
    /**
55
     * @var string
56
     */
57
    private $basePath;
58
59
    /**
60
     * Description constructor.
61
     *
62
     * @param Path[]        $paths
63
     * @param ComplexType[] $complexTypes
64
     * @param string        $host
65
     * @param array         $schemes
66
     * @param array         $extensions
67
     * @param Document      $document
68
     * @param string        $basePath
69
     */
70
    public function __construct(
71
        array $paths,
72
        array $complexTypes,
73
        $host,
74
        array $schemes,
75
        array $extensions,
76
        Document $document,
77
        string $basePath = ''
78
    ) {
79
        $this->paths        = $paths;
80
        $this->complexTypes = $complexTypes;
81
        $this->host         = $host;
82
        $this->schemes      = $schemes;
83
        $this->document     = $document;
84
        $this->extensions   = $extensions;
85
        $this->basePath     = $basePath;
86
    }
87
88
    /**
89
     * @param string $name
90
     *
91
     * @return mixed
92
     */
93
    public function getExtension(string $name)
94
    {
95
        return isset($this->extensions[$name]) ? $this->extensions[$name] : null;
96
    }
97
98
    /**
99
     * @return ComplexType[]
100
     */
101
    public function getComplexTypes(): array
102
    {
103
        return $this->complexTypes;
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function getSchemes(): array
110
    {
111
        return $this->schemes;
112
    }
113
114
    /**
115
     * @return string|null
116
     */
117
    public function getHost()
118
    {
119
        return $this->host;
120
    }
121
122
    /**
123
     * @param string $path
124
     *
125
     * @return Path
126
     */
127
    public function getPath(string $path): Path
128
    {
129
        if (!$this->hasPath($path)) {
130
            throw new \InvalidArgumentException(
131
                "Path '$path' does not exist (have ".implode(', ', array_keys($this->paths)).')'
132
            );
133
        }
134
135
        return $this->paths[$path];
136
    }
137
138
    /**
139
     * @param string $path
140
     * @return bool
141
     */
142
    public function hasPath(string $path): bool
143
    {
144
        return isset($this->paths[$path]);
145
    }
146
147
    /**
148
     * @param string $path
149
     * @param string $method
150
     *
151
     * @return Schema
152
     */
153
    public function getRequestSchema(string $path, string $method): Schema
154
    {
155
        return $this->getPath($path)->getOperation($method)->getRequestSchema();
156
    }
157
158
    /**
159
     * @param string $path
160
     * @param string $method
161
     *
162
     * @return Parameter|null
163
     */
164
    public function getRequestBodyParameter(string $path, string $method)
165
    {
166
        foreach ($this->getPath($path)->getOperation($method)->getParameters() as $parameter) {
167
            if ($parameter->getIn() === Parameter::IN_BODY) {
168
                return $parameter;
169
            }
170
        }
171
    }
172
173
    /**
174
     * @param string $path
175
     * @param string $method
176
     *
177
     * @param int    $code
178
     *
179
     * @return Schema
180
     */
181
    public function getResponseSchema(string $path, string $method, int $code): Schema
182
    {
183
        return $this->getPath($path)->getOperation($method)->getResponse($code)->getSchema();
184
    }
185
186
    /**
187
     * @return Path[]
188
     */
189
    public function getPaths(): array
190
    {
191
        return array_values($this->paths);
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getBasePath(): string
198
    {
199
        return $this->basePath;
200
    }
201
202
    /**
203
     * @return Document
204
     */
205
    public function getDocument(): Document
206
    {
207
        return $this->document;
208
    }
209
}
210