Completed
Pull Request — develop (#273)
by Samuel
25:26 queued 13:47
created

ApiDefinitionLoader::loadApiDefinition()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 8.8571
cc 5
eloc 6
nc 3
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 a schema for one endpoint
70
     *
71
     * @param string $endpoint    endpoint
72
     * @param bool   $forceReload Switch to force a new api definition object will be provided.
73
     *
74
     * @return \stdClass
75
     */
76
    public function getEndpointSchema($endpoint, $forceReload = false)
77
    {
78
        $this->loadApiDefinition($forceReload);
79
80
        return $this->definition->getSchema($endpoint);
81
    }
82
83
    /**
84
     * get an endpoint
85
     *
86
     * @param string  $endpoint    endpoint
87
     * @param boolean $withHost    attach host name to the url
88
     * @param bool    $forceReload Switch to force a new api definition object will be provided.
89
     *
90
     * @return string
91
     */
92
    public function getEndpoint($endpoint, $withHost = false, $forceReload = false)
93
    {
94
        $this->loadApiDefinition($forceReload);
95
        $url = "";
96
        if ($withHost) {
97
            $url = $this->definition->getHost();
98
        }
99
        //$this->definition->hasEndpoint($endpoint);
100
101
        /*$endpoints = $this->definition->getEndpoints(false);
102
103
        //has url a id
104
        $searchString = $endpoint;
105
        if (preg_match("@\/[0-9]{1,}$@", $endpoint)) {
106
            $searchString = substr($endpoint, 0, strrpos($endpoint, '/'));
107
        }
108
109
        if (in_array($searchString, $endpoints)) {*/
110
            $url .= $endpoint;
111
        //}
112
113
        return $url;
114
    }
115
116
    /**
117
     * get all endpoints for an API
118
     *
119
     * @param boolean $withHost    attach host name to the url
120
     * @param bool    $forceReload Switch to force a new api definition object will be provided.
121
     *
122
     * @return array
123
     */
124
    public function getAllEndpoints($withHost = false, $forceReload = false)
125
    {
126
        $this->loadApiDefinition($forceReload);
127
128
        $prefix = self::PROXY_ROUTE;
129
        if (isset($this->options['prefix'])) {
130
            $prefix .= "/".$this->options['prefix'];
131
        }
132
        $retVal = array();
133
        if (is_object($this->definition)) {
134
            $retVal = $this->definition->getEndpoints($withHost, $prefix);
135
        }
136
137
        return $retVal;
138
    }
139
140
    /**
141
     * internal load method
142
     *
143
     * @param bool $forceReload Switch to force a new api definition object will be provided.
144
     *
145
     * @return void
146
     */
147
    private function loadApiDefinition($forceReload = false)
148
    {
149
        $supported = $this->definitionLoader->supports($this->options['uri']);
150
151
        if ($forceReload || ($this->definition == null && $supported)) {
152
            $this->definition = $this->definitionLoader->load($this->options['uri']);
153
        } elseif (!$supported) {
154
            throw new \RuntimeException("This resource (".$this->options['uri'].") is not supported.");
155
        }
156
    }
157
}
158