Completed
Push — master ( bca24d...1b0d8b )
by John
03:18
created

Description::getPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 5
Ratio 50 %

Importance

Changes 0
Metric Value
dl 5
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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 Document
46
     */
47
    protected $document;
48
49
    /**
50
     * Description constructor.
51
     *
52
     * @param Path[]        $paths
53
     * @param ComplexType[] $complexTypes
54
     * @param string        $host
55
     * @param array         $schemes
56
     * @param Document      $document
57
     */
58
    public function __construct(array $paths, array $complexTypes, $host, array $schemes, Document $document)
59
    {
60
        $this->paths        = $paths;
61
        $this->complexTypes = $complexTypes;
62
        $this->host         = $host;
63
        $this->schemes      = $schemes;
64
        $this->document     = $document;
65
    }
66
67
    /**
68
     * @return ComplexType[]
69
     */
70
    public function getComplexTypes(): array
71
    {
72
        return $this->complexTypes;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function getSchemes(): array
79
    {
80
        return $this->schemes;
81
    }
82
83
    /**
84
     * @return string|null
85
     */
86
    public function getHost()
87
    {
88
        return $this->host;
89
    }
90
91
    /**
92
     * @param string $path
93
     *
94
     * @return Path
95
     */
96
    public function getPath(string $path): Path
97
    {
98 View Code Duplication
        if (!isset($this->paths[$path])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
99
            throw new \InvalidArgumentException(
100
                "Path '$path' does not exist (have " . implode(', ', array_keys($this->paths)) . ')'
101
            );
102
        }
103
104
        return $this->paths[$path];
105
    }
106
107
    /**
108
     * @param string $path
109
     * @param string $method
110
     *
111
     * @return Schema
112
     */
113
    public function getRequestSchema(string $path, string $method): Schema
114
    {
115
        return $this->getPath($path)->getOperation($method)->getRequestSchema();
116
    }
117
118
    /**
119
     * @param string $path
120
     * @param string $method
121
     *
122
     * @param int    $code
123
     *
124
     * @return Schema
125
     */
126
    public function getResponseSchema(string $path, string $method, int $code): Schema
127
    {
128
        return $this->getPath($path)->getOperation($method)->getResponse($code)->getSchema();
129
    }
130
131
    /**
132
     * @return Path[]
133
     */
134
    public function getPaths(): array
135
    {
136
        return array_values($this->paths);
137
    }
138
139
    /**
140
     * @return Document
141
     */
142
    public function getDocument(): Document
143
    {
144
        return $this->document;
145
    }
146
}
147