Completed
Push — master ( aed5d3...cc8c38 )
by Nelson J
14:51 queued 25s
created

Soundcloud::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
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
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
    protected $factory;
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
        $this->initialize();
48
        //$this->factory = new Factory();
49
        $this->auth = $this->make(AuthInterface::class, array($clientID, $clientSecret, $authCallbackUri));
50
    }
51
52 3
    protected function initialize()
53
    {
54 3
        $a = ['AuthInterface'         => 'Njasm\\Soundcloud\\Auth\\Auth',
0 ignored issues
show
Unused Code introduced by
$a is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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