Completed
Push — master ( cc8c38...9e3b00 )
by Nelson J
15:54 queued 01:25
created

Soundcloud::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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