Completed
Push — master ( 641bb6...ea06d2 )
by Nelson J
02:37
created

Soundcloud::getAuthScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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