Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

SoundCloud::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
class SoundCloud extends AbstractService
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    protected function init()
15
    {
16
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
17
            $this->baseApiUri = new Uri('https://api.soundcloud.com/');
18
        }
19
    }
20
    
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getAuthorizationEndpoint()
25
    {
26
        return new Uri('https://soundcloud.com/connect');
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getAccessTokenEndpoint()
33
    {
34
        return new Uri('https://api.soundcloud.com/oauth2/token');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function parseAccessTokenResponse($responseBody)
41
    {
42
        $data = json_decode($responseBody, true);
43
44
        if (null === $data || !is_array($data)) {
45
            throw new TokenResponseException('Unable to parse response.');
46
        } elseif (isset($data['error'])) {
47
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
48
        }
49
50
        $token = new StdOAuth2Token();
51
        $token->setAccessToken($data['access_token']);
52
53
        if (isset($data['expires_in'])) {
54
            $token->setLifetime($data['expires_in']);
55
            unset($data['expires_in']);
56
        }
57
58
        if (isset($data['refresh_token'])) {
59
            $token->setRefreshToken($data['refresh_token']);
60
            unset($data['refresh_token']);
61
        }
62
63
        unset($data['access_token']);
64
65
        $token->setExtraParams($data);
66
67
        return $token;
68
    }
69
}
70