Completed
Push — feature/service-wrapper-for-sw... ( 178655...7b5228 )
by Adrian
08:46
created

ApiDefinitionLoader::getOriginDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4286
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\LoaderInterface;
10
11
/**
12
 * load API definition from  a external source
13
 *
14
 * @package Graviton\ProxyBundle\Service
15
 * @author  List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link    http://swisscom.ch
18
 */
19
class ApiDefinitionLoader
20
{
21
    /**
22
     * @var string
23
     */
24
    const PROXY_ROUTE = "3rdparty";
25
26
    /**
27
     * @var LoaderInterface
28
     */
29
    private $definitionLoader;
30
31
    /**
32
     * @var array
33
     */
34
    private $options;
35
36
    /**
37
     * @var ApiDefinition
38
     */
39
    private $definition;
40
41
    /**
42
     * set loader
43
     *
44
     * @param LoaderInterface $loader loader
45
     *
46
     * @return void
47
     */
48
    public function setDefinitionLoader($loader)
49
    {
50
        $this->definitionLoader = $loader;
51
    }
52
53
    /**
54
     * set options for the loader
55
     *
56
     * @param array $options options [uri, prefix]
57
     *
58
     * @return void
59
     */
60
    public function setOption(array $options)
61
    {
62
        $this->options = $options;
63
        if (!empty($this->definitionLoader)) {
64
            $this->definitionLoader->setOptions($options);
65
        }
66
    }
67
68
    /**
69
     * get the origin service definition
70
     *
71
     * @return mixed the origin service definition (type depends on dispersal strategy)
72
     */
73
    public function getOriginDefinition($forceReload = false)
74
    {
75
        $this->loadApiDefinition($forceReload);
76
        return $this->definition->getOrigin();
77
    }
78
79
    /**
80
     * get a schema for one endpoint
81
     *
82
     * @param string $endpoint    endpoint
83
     * @param bool   $forceReload Switch to force a new api definition object will be provided.
84
     *
85
     * @return \stdClass
86
     */
87
    public function getEndpointSchema($endpoint, $forceReload = false)
88
    {
89
        $this->loadApiDefinition($forceReload);
90
91
        return $this->definition->getSchema($endpoint);
92
    }
93
94
    /**
95
     * get an endpoint
96
     *
97
     * @param string  $endpoint    endpoint
98
     * @param boolean $withHost    attach host name to the url
99
     * @param bool    $forceReload Switch to force a new api definition object will be provided.
100
     *
101
     * @return string
102
     */
103
    public function getEndpoint($endpoint, $withHost = false, $forceReload = false)
104
    {
105
        $this->loadApiDefinition($forceReload);
106
        $url = "";
107
        if ($withHost) {
108
            $url = $this->definition->getHost();
109
        }
110
        //$this->definition->hasEndpoint($endpoint);
111
112
        /*$endpoints = $this->definition->getEndpoints(false);
113
114
        //has url a id
115
        $searchString = $endpoint;
116
        if (preg_match("@\/[0-9]{1,}$@", $endpoint)) {
117
            $searchString = substr($endpoint, 0, strrpos($endpoint, '/'));
118
        }
119
120
        if (in_array($searchString, $endpoints)) {*/
121
            $url .= $endpoint;
122
        //}
123
124
        return $url;
125
    }
126
127
    /**
128
     * get all endpoints for an API
129
     *
130
     * @param boolean $withHost    attach host name to the url
131
     * @param bool    $forceReload Switch to force a new api definition object will be provided.
132
     *
133
     * @return array
134
     */
135
    public function getAllEndpoints($withHost = false, $forceReload = false)
136
    {
137
        $this->loadApiDefinition($forceReload);
138
139
        $prefix = self::PROXY_ROUTE;
140
        if (isset($this->options['prefix'])) {
141
            $prefix .= "/".$this->options['prefix'];
142
        }
143
        $retVal = array();
144
        if (is_object($this->definition)) {
145
            $retVal = $this->definition->getEndpoints($withHost, $prefix);
146
        }
147
148
        return $retVal;
149
    }
150
151
    /**
152
     * internal load method
153
     *
154
     * @param bool $forceReload Switch to force a new api definition object will be provided.
155
     *
156
     * @return void
157
     */
158
    private function loadApiDefinition($forceReload = false)
159
    {
160
        $supported = $this->definitionLoader->supports($this->options['uri']);
161
162
        if ($forceReload || ($this->definition == null && $supported)) {
163
            $this->definition = $this->definitionLoader->load($this->options['uri']);
164
        } elseif (!$supported) {
165
            throw new \RuntimeException("This resource (".$this->options['uri'].") is not supported.");
166
        }
167
    }
168
}
169