Completed
Push — feature/EVO-7964_fundInfo-exte... ( 142d3d...cf9459 )
by Bastian
08:28 queued 08:21
created

ApiDefinitionLoader::getEndpointSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * ApiDefinitionLoader
4
 */
5
6
namespace Graviton\ProxyBundle\Service;
7
8
use Graviton\ProxyBundle\Definition\ApiDefinition;
9
use Graviton\ProxyBundle\Definition\Loader\LoaderFactory;
10
use Graviton\ProxyBundle\Definition\Loader\LoaderInterface;
11
12
/**
13
 * load API definition from  a external source
14
 *
15
 * @package Graviton\ProxyBundle\Service
16
 * @author  List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link    http://swisscom.ch
19
 */
20
class ApiDefinitionLoader
21
{
22
    /**
23
     * @var string
24
     */
25
    const PROXY_ROUTE = "3rdparty";
26
27
    /**
28
     * @var LoaderInterface
29
     */
30
    private $definitionLoader;
31
32
    /**
33
     * @var array
34
     */
35
    private $options;
36
37
    /**
38
     * @var ApiDefinition
39
     */
40
    private $definition;
41
42
    /** @var  LoaderFactory */
43
    private $loaderFactory;
44
45
46
    /**
47
     * ApiDefinitionLoader constructor.
48
     *
49
     * @param LoaderFactory $loaderFactory Factory to initiate an apiloader
50
     */
51
    public function __construct(LoaderFactory $loaderFactory)
52
    {
53
        $this->loaderFactory = $loaderFactory;
54
    }
55
56
    /**
57
     * set loader
58
     *
59
     * @param LoaderInterface $loader loader
60
     *
61
     * @return void
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
62
     */
63
    public function setDefinitionLoader($loader)
0 ignored issues
show
Unused Code introduced by
The parameter $loader is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
66
        throw new \RuntimeException('ApiDefinitionLoader::setDefinitionLoader is deprecated.');
67
68
        //$this->definitionLoader = $loader;
69
    }
70
71
    /**
72
     * Provides the definition loader instance.
73
     *
74
     * @return void
75
     */
76
    public function getDefinitionLoader()
77
    {
78
        if (empty($this->definitionLoader) && array_key_exists('prefix', $this->options)) {
79
            $this->definitionLoader = $this->loaderFactory->create($this->options['prefix']);
80
        }
81
    }
82
83
    /**
84
     * Resets the definition loader
85
     *
86
     * @return void
87
     */
88
    public function resetDefinitionLoader()
89
    {
90
        $this->definitionLoader = null;
91
    }
92
93
    /**
94
     * set options for the loader
95
     *
96
     * @param array $options options [uri, prefix]
97
     *
98
     * @return void
99
     */
100
    public function setOption(array $options)
101
    {
102
        $this->options = $options;
103
        $this->getDefinitionLoader();
104
        $this->definitionLoader->setOptions($options);
105
    }
106
107
    /**
108
     * @param array $options Options to be added
109
     *
110
     * @return void
111
     */
112
    public function addOptions(array $options)
113
    {
114
        $this->options = array_merge($this->options, $options);
115
116
        $this->getDefinitionLoader();
117
        $this->definitionLoader->setOptions($options);
118
    }
119
120
    /**
121
     * get the origin service definition
122
     *
123
     * @param bool $forceReload Switch to force a new api definition object will be provided.
124
     *
125
     * @return mixed the origin service definition (type depends on dispersal strategy)
126
     */
127
    public function getOriginDefinition($forceReload = false)
128
    {
129
        $this->loadApiDefinition($forceReload);
130
131
        return $this->definition->getOrigin();
132
    }
133
134
    /**
135
     * get a schema for one endpoint
136
     *
137
     * @param string $endpoint    endpoint
138
     * @param bool   $forceReload Switch to force a new api definition object will be provided.
139
     *
140
     * @return \stdClass
141
     */
142
    public function getEndpointSchema($endpoint, $forceReload = false)
143
    {
144
        $this->loadApiDefinition($forceReload);
145
146
        return $this->definition->getSchema($endpoint);
147
    }
148
149
    /**
150
     * get an endpoint
151
     *
152
     * @param string  $endpoint    endpoint
153
     * @param boolean $withHost    attach host name to the url
154
     * @param bool    $forceReload Switch to force a new api definition object will be provided.
155
     *
156
     * @return string
157
     */
158
    public function getEndpoint($endpoint, $withHost = false, $forceReload = false)
159
    {
160
        $this->loadApiDefinition($forceReload);
161
        $url = "";
162
        if ($withHost) {
163
            $url = empty($this->options['host']) ? $this->definition->getHost() : $this->options['host'];
164
        }
165
166
        // If the base path is not already included, we need to add it.
167
        $url .= (empty($this->options['includeBasePath']) ? $this->definition->getBasePath() : '') . $endpoint;
168
169
        return $url;
170
    }
171
172
    /**
173
     * get all endpoints for an API
174
     *
175
     * @param boolean $withHost    attach host name to the url
176
     * @param bool    $forceReload Switch to force a new api definition object will be provided.
177
     *
178
     * @return array
179
     */
180
    public function getAllEndpoints($withHost = false, $forceReload = false)
181
    {
182
        $this->loadApiDefinition($forceReload);
183
184
        $host = empty($this->options['host']) ? '' : $this->options['host'];
185
        $prefix = self::PROXY_ROUTE;
186
        if (isset($this->options['prefix'])) {
187
            $prefix .= "/".$this->options['prefix'];
188
        }
189
190
        return !is_object($this->definition) ? [] : $this->definition->getEndpoints(
191
            $withHost,
192
            $prefix,
193
            $host,
194
            !empty($this->options['includeBasePath'])
195
        );
196
    }
197
198
    /**
199
     * internal load method
200
     *
201
     * @param bool $forceReload Switch to force a new api definition object will be provided.
202
     *
203
     * @return void
204
     */
205
    private function loadApiDefinition($forceReload = false)
206
    {
207
        $this->getDefinitionLoader();
208
209
        $supported = $this->definitionLoader->supports($this->options['uri']);
210
211
        if ($forceReload || ($this->definition == null && $supported)) {
212
            $this->definition = $this->definitionLoader->load($this->options['uri']);
213
        } elseif (!$supported) {
214
            throw new \RuntimeException("This resource (".$this->options['uri'].") is not supported.");
215
        }
216
    }
217
}
218