Completed
Pull Request — develop (#368)
by Adrian
13:00 queued 06:33
created

ApiDefinition::addSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Store all necessary information about 3rd party APIs
4
 */
5
6
namespace Graviton\ProxyBundle\Definition;
7
8
/**
9
 * ApiDefinition
10
 *
11
 * @author  List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
12
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13
 * @link    http://swisscom.ch
14
 */
15
class ApiDefinition
16
{
17
    /**
18
     * @var string
19
     */
20
    private $basePath;
21
22
    /**
23
     * @var string
24
     */
25
    private $host;
26
27
    /**
28
     * @var mixed
29
     */
30
    private $origin;
31
32
    /**
33
     * @var array
34
     */
35
    private $endpoints = array();
36
37
    /**
38
     * @var array
39
     */
40
    private $schemes = array();
41
42
    /**
43
     * sets the base path of the api
44
     *
45
     * @param string $basePath API base path
46
     *
47
     * @return void
48
     */
49 1
    public function setBasePath($basePath)
50
    {
51 1
        $this->basePath = $basePath;
52 1
    }
53
54
    /**
55
     * Gets the API's base path
56
     *
57
     * @return string The base path
58
     */
59
    public function getBasePath()
60
    {
61
        return $this->basePath;
62
    }
63
64
    /**
65
     * sets the FQDN of the API
66
     *
67
     * @param string $host FQDN
68
     *
69
     * @return void
70
     */
71 4
    public function setHost($host)
72
    {
73 4
        $this->host = $host;
74 4
    }
75
76
    /**
77
     * get the FQDN of the API
78
     *
79
     * @return string FQDN
80
     */
81 3
    public function getHost()
82
    {
83 3
        return $this->host;
84
    }
85
86
    /**
87
     * set the origin service definition
88
     *
89
     * @param mixed $origin the origin service definition (type depends on dispersal strategy)
90
     *
91
     * @return void
92
     */
93 1
    public function setOrigin($origin)
94
    {
95 1
        $this->origin = $origin;
96 1
    }
97
98
    /**
99
     * get the origin service definition
100
     *
101
     * @return mixed the origin service definition (type depends on dispersal strategy)
102
     */
103
    public function getOrigin()
104
    {
105
        return $this->origin;
106
    }
107
108
    /**
109
     * add an endpoint
110
     *
111
     * @param string $endpoint endpoint
112
     *
113
     * @return void
114
     */
115 4
    public function addEndpoint($endpoint)
116
    {
117 4
        $this->endpoints[] = $endpoint;
118 4
    }
119
120
    /**
121
     * check if an endpoint exists
122
     *
123
     * @param string $endpoint endpoint
124
     *
125
     * @return boolean
126
     */
127 2
    public function hasEndpoint($endpoint)
128
    {
129 2
        $retVal = false;
130 2
        if (isset($this->endpoints)) {
131 2
            $retVal = in_array($endpoint, $this->endpoints);
132 2
        }
133
134 2
        return $retVal;
135
    }
136
137
    /**
138
     * get all defined API endpoints
139
     *
140
     * @param boolean $withHost     url with hostname
141
     * @param string  $prefix       add a prefix to the url (blub/endpoint/url)
142
     * @param string  $host         Host to be used instead of the host defined in the swagger json
143
     * @param bool    $withBasePath Defines whether the API's base path should be included or not
144
     *
145
     * @return array
146
     */
147 4
    public function getEndpoints($withHost = true, $prefix = null, $host = '', $withBasePath = true)
148
    {
149 4
        $endpoints = array();
150 4
        $basePath = "";
151 4
        if ($withHost) {
152 3
            $basePath = (empty($host)) ? $this->getHost() : $host;
153 3
        }
154 4
        if (!empty($prefix)) {
155 1
            $basePath .= $prefix;
156 1
        }
157 4
        if ($withBasePath && isset($this->basePath)) {
158 1
            $basePath .= $this->basePath;
159 1
        }
160 4
        foreach ($this->endpoints as $endpoint) {
161 4
            $endpoints[] = $basePath.$endpoint;
162 4
        }
163
164 4
        return $endpoints;
165
    }
166
167
    /**
168
     * add a schema for an endpoint
169
     *
170
     * @param string    $endpoint endpoint
171
     * @param \stdClass $schema   schema
172
     *
173
     * @return void
174
     */
175 2
    public function addSchema($endpoint, $schema)
176
    {
177 2
        $this->schemes[$endpoint] = $schema;
178 2
    }
179
180
    /**
181
     * get a schema for an endpoint
182
     *
183
     * @param string $endpoint endpoint
184
     *
185
     * @return \stdClass
186
     */
187 1
    public function getSchema($endpoint)
188
    {
189
        //remove base path
190 1
        if ('/' !== $this->basePath) {
191 1
            $endpoint = str_replace($this->basePath, '', $endpoint);
192 1
        }
193 1
        $retVal = new \stdClass();
194 1
        if (array_key_exists($endpoint, $this->schemes)) {
195 1
            $retVal = $this->schemes[$endpoint];
196 1
        }
197
198 1
        return $retVal;
199
    }
200
}
201