Completed
Pull Request — develop (#512)
by Adrian
16:23 queued 09:34
created

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