Completed
Pull Request — develop (#273)
by Samuel
19:01 queued 08:39
created

ApiDefinition::addEndpoint()   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 1
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
     * sets the FQDN of the API
56
     *
57
     * @param string $host FQDN
58
     *
59
     * @return void
60
     */
61 3
    public function setHost($host)
62
    {
63 3
        $this->host = $host;
64 3
    }
65
66
    /**
67
     * get the FQDN of the API
68
     *
69
     * @return string FQDN
70
     */
71 3
    public function getHost()
72
    {
73 3
        return $this->host;
74
    }
75
76
    /**
77
     * set the origin service definition
78
     *
79
     * @param mixed $origin the origin service definition (type depends on dispersal strategy)
80
     *
81
     * @return void
82
     */
83 1
    public function setOrigin($origin)
84
    {
85 1
        $this->origin = $origin;
86 1
    }
87
88
    /**
89
     * get the origin service definition
90
     *
91
     * @return mixed the origin service definition (type depends on dispersal strategy)
92
     */
93
    public function getOrigin()
94
    {
95
        return $this->origin;
96
    }
97
98
    /**
99
     * add an endpoint
100
     *
101
     * @param string $endpoint endpoint
102
     *
103
     * @return void
104
     */
105 3
    public function addEndpoint($endpoint)
106
    {
107 3
        $this->endpoints[] = $endpoint;
108 3
    }
109
110
    /**
111
     * check if an endpoint exists
112
     *
113
     * @param string $endpoint endpoint
114
     *
115
     * @return boolean
116
     */
117 2
    public function hasEndpoint($endpoint)
118
    {
119 2
        $retVal = false;
120 2
        if (isset($this->endpoints)) {
121 2
            $retVal = in_array($endpoint, $this->endpoints);
122 2
        }
123
124 2
        return $retVal;
125
    }
126
127
    /**
128
     * get all defined API endpoints
129
     *
130
     * @param boolean $withHost url with hostname
131
     * @param string  $prefix   add a prefix to the url (blub/endpoint/url)
132
     *
133
     * @return array
134
     */
135 3
    public function getEndpoints($withHost = true, $prefix = null)
136
    {
137 3
        $endpoints = array();
138 3
        $basePath = "";
139 3
        if ($withHost) {
140 2
            $basePath = $this->getHost();
141 2
        }
142 3
        if (!empty($prefix)) {
143 1
            $basePath .= $prefix;
144 1
        }
145 3
        if (isset($this->basePath)) {
146 1
            $basePath .= $this->basePath;
147 1
        }
148 3
        foreach ($this->endpoints as $endpoint) {
149 3
            $endpoints[] = $basePath.$endpoint;
150 3
        }
151
152 3
        return $endpoints;
153
    }
154
155
    /**
156
     * add a schema for an endpoint
157
     *
158
     * @param string    $endpoint endpoint
159
     * @param \stdClass $schema   schema
160
     *
161
     * @return void
162
     */
163 2
    public function addSchema($endpoint, $schema)
164
    {
165 2
        $this->schemes[$endpoint] = $schema;
166 2
    }
167
168
    /**
169
     * get a schema for an endpoint
170
     *
171
     * @param string $endpoint endpoint
172
     *
173
     * @return \stdClass
174
     */
175 1
    public function getSchema($endpoint)
176
    {
177
        //remove base path
178 1
        if ('/' !== $this->basePath) {
179 1
            $endpoint = str_replace($this->basePath, '', $endpoint);
180 1
        }
181 1
        $retVal = new \stdClass();
182 1
        if (array_key_exists($endpoint, $this->schemes)) {
183 1
            $retVal = $this->schemes[$endpoint];
184 1
        }
185
186 1
        return $retVal;
187
    }
188
}
189