Completed
Push — master ( c9b397...a68c62 )
by Nelson J
03:37
created

Soundcloud::asXml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Njasm\Soundcloud;
4
5
use Njasm\Soundcloud\Request\RequestInterface;
6
use Njasm\Soundcloud\Factory\Factory;
7
8
/**
9
 * SoundCloud API wrapper in PHP
10
 *
11
 * @author      Nelson J Morais <[email protected]>
12
 * @copyright   2014 Nelson J Morais <[email protected]>
13
 * @license     http://www.opensource.org/licenses/mit-license.php MIT
14
 * @link        http://github.com/njasm/soundcloud
15
 * @package     Njasm\Soundcloud
16
 */
17
18
class Soundcloud
19
{
20
    const VERSION = '2.2.3';
21
    const LIB_NAME = 'Njasm-Soundcloud';
22
    const LIB_URL = 'https://www.github.com/njasm/soundcloud';
23
24
    protected $resource;
25
    protected $request;
26
    protected $response;
27
    protected $auth;
28
    protected $factory;
29
    protected $responseFormat;
30
31 22
    public function __construct($clientID = null, $clientSecret = null, $authCallbackUri = null)
32
    {
33 22
        $this->factory = new Factory();
34 22
        $this->auth = $this->make('AuthInterface', array($clientID, $clientSecret, $authCallbackUri));
35 22
    }
36
    
37
    /**
38
     * Get ClientID for this instance
39
     * 
40
     * @return string The ClientID set for this instance
41
     */
42 2
    public function getAuthClientID()
43
    {
44 2
        return $this->auth->getClientID();
45
    }
46
    
47
    /**
48
     * Get the access token.
49
     * 
50
     * @return mixed the token, else null is returned
51
     */
52 3
    public function getAuthToken()
53
    {
54 3
        return $this->auth->getToken();
55
    }
56
    
57
    /**
58
     * Get the token scope.
59
     * 
60
     * @return mixed the scope for this access token, null if empty
61
     */
62 1
    public function getAuthScope()
63
    {
64 1
        return $this->auth->getScope();
65
    }
66
67
    /**
68
     * Get the token scope.
69
     * 
70
     * @return mixed the scope for this access token, null if empty
71
     */
72 1
    public function getExpires()
73
    {
74 1
        return $this->auth->getExpires();
75
    }
76
77
    /**
78
     * Set the Auth access token.
79
     * 
80
     * @return void
81
     */
82
    public function setAccessToken($accessToken)
83
    {
84
        $this->auth->setToken($accessToken);
85
    }
86
87
    /**
88
     * Set the Auth Scope.
89
     * 
90
     * @return void
91
     */
92
    public function setAuthScope($scope)
93
    {
94
        $this->auth->setScope($scope);
95
    }
96
97
    /**
98
     * Set the Auth Expires.
99
     * 
100
     * @return void
101
     */
102
    public function setAuthExpires($expires)
103
    {
104
        $this->auth->setExpires($expires);
105
    }
106
    
107
    /**
108
     * Set the Auth refresh token.
109
     * 
110
     * @return void
111
     */    
112
    public function setRefreshToken($refreshToken)
113
    {
114
        $this->auth->setRefreshToken($refreshToken);
115
    }
116
        
117
    
118
    /**
119
     * Sets up a GET Resource.
120
     * 
121
     * @param string $path
122
     * @param array $params
123
     * @return \Njasm\Soundcloud\Soundcloud
124
     */
125 3
    public function get($path, array $params = array())
126
    {
127 3
        $params = $this->mergeAuthParams($params);
128 3
        $this->resource = $this->make('ResourceInterface', array('get', $path, $params));
129 3
        return $this;
130
    }
131
132
    /**
133
     * Sets up a PUT Resource.
134
     * 
135
     * @param string $path
136
     * @param array $params
137
     * @return \Njasm\Soundcloud\Soundcloud
138
     */
139 1
    public function put($path, array $params = array())
140
    {
141 1
        $params = $this->mergeAuthParams($params);
142 1
        $this->resource = $this->make('ResourceInterface', array('put', $path, $params));
143 1
        return $this;
144
    }
145
    
146
    /**
147
     * Sets up a POST Resource.
148
     * 
149
     * @param string $path
150
     * @param array $params
151
     * @return \Njasm\Soundcloud\Soundcloud
152
     */
153 5
    public function post($path, array $params = array())
154
    {
155 5
        $params = $this->mergeAuthParams($params);
156 5
        $this->resource = $this->make('ResourceInterface', array('post', $path, $params));
157 5
        return $this;
158
    }
159
    
160
    /**
161
     * Sets up a DELETE Resource.
162
     * 
163
     * @param string $path
164
     * @param array $params
165
     * @return \Njasm\Soundcloud\Soundcloud
166
     */
167 1
    public function delete($path, array $params = array())
168
    {
169 1
        $params = $this->mergeAuthParams($params);
170 1
        $this->resource = $this->make('ResourceInterface', array('delete', $path, $params));
171 1
        return $this;
172
    }
173
    
174
    /**
175
     * @param string $interface the interface to build
176
     * @param array $params the interface object dependencies
177
     * @return object
178
     */
179 22
    protected function make($interface, array $params = array())
180
    {
181 22
        return $this->factory->make($interface, $params);
182
    }
183
    
184
    /**
185
     * Sets resource params.
186
     * 
187
     * @param array $params
188
     * @return \Njasm\Soundcloud\Soundcloud
189
     * @throws RuntimeException
190
     */
191 3
    public function setParams(array $params = array())
192
    {
193 3
        if (!isset($this->resource)) {
194 1
            throw new \RuntimeException("No Resource found. you must call a http verb method before " . __METHOD__);
195
        }
196
        
197 2
        $this->resource->setParams($params);
198
        
199 2
        return $this;
200
    }
201
    
202
    /**
203
     * Executes the request against soundcloud api.
204
     * 
205
     * @param array $params
206
     * @return \Njasm\Soundcloud\Request\ResponseInterface
207
     */
208 6
    public function request(array $params = array())
209
    {
210 6
        $urlBuilder = $this->make('UrlBuilderInterface', array($this->resource));
211 6
        $this->request = $this->make('RequestInterface', array($this->resource, $urlBuilder, $this->factory));
212 6
        $this->request->setOptions($params);
213 6
        $this->setResponseFormat($this->request);
214
        
215 6
        $this->response = $this->request->exec();
216
        
217 6
        return $this->response;
218
    }
219
    
220
    /**
221
     * Get Last Curl Response object.
222
     * 
223
     * @return mixed The Response Object, null if no request was yet made
224
     */
225 1
    public function getCurlResponse()
226
    {
227 1
        return (isset($this->response)) ? $this->response : null;
228
    }
229
    
230
    /**
231
     * Sets the Accept Header to application/xml.
232
     *
233
     * @deprecated Soundcloud does not support XML responses anymore.
234
     * @see https://github.com/njasm/soundcloud/issues/16
235
     *
236
     * @return Soundcloud
237
     */
238 1
    public function asXml()
239
    {
240 1
        $this->asJson();
0 ignored issues
show
Deprecated Code introduced by
The method Njasm\Soundcloud\Soundcloud::asJson() has been deprecated with message: Soundcloud does not support XML responses anymore and calling this method will be redundant.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
241 1
        return $this;
242
    }
243
    
244
    /**
245
     * Sets the Accept Header to application/json.
246
     *
247
     * @deprecated Soundcloud does not support XML responses anymore and calling this method will be redundant.
248
     * @see https://github.com/njasm/soundcloud/issues/16
249
     *
250
     * @return Soundcloud
251
     */
252 5
    public function asJson()
253
    {
254 5
        $this->responseFormat = "json";
255 5
        return $this;
256
    }
257
    
258
    /**
259
     * Set response format for Request.
260
     *
261
     * @param \Njasm\Soundcloud\Request\RequestInterface
262
     * @return void
263
     */
264 7
    protected function setResponseFormat(RequestInterface $request)
265
    {
266 7
        if ($this->responseFormat == "xml") {
267
            $request->asXml();
268
        } else {
269 7
            $request->asJson();
270
        }
271 7
    }
272
    
273
    /**
274
     * Build array of auth params for the next request.
275
     * 
276
     * @param array $params
277
     * @param bool $includeClientSecret
278
     * @return array
279
     */
280 11
    protected function mergeAuthParams(array $params = array(), $includeClientSecret = false)
281
    {
282 11
        return $this->auth->mergeParams($params, $includeClientSecret);
283
    }
284
}
285