Completed
Push — feature/EVO-7964_fundInfo-exte... ( d593d5 )
by Bastian
08:56
created

ApiDefinitionLoader::getDefinitionLoader()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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