Completed
Pull Request — master (#498)
by Dragonqos
04:53 queued 02:07
created

Ustream::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
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 Ustream extends AbstractService
10
{
11
    /**
12
     * Scopes
13
     *
14
     * @var string
15
     */
16
    const SCOPE_OFFLINE     = 'offline';
17
    const SCOPE_BROADCASTER = 'broadcaster';
18
    
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected function init()
23
    {
24
        $this->stateParameterInAuthUrl = true;
25
26
        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...
27
            $this->baseApiUri = new Uri('https://api.ustream.tv/');
28
        }
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getAuthorizationEndpoint()
35
    {
36
        return new Uri('https://www.ustream.tv/oauth2/authorize');
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getAccessTokenEndpoint()
43
    {
44
        return new Uri('https://www.ustream.tv/oauth2/token');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function getAuthorizationMethod()
51
    {
52
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function parseAccessTokenResponse($responseBody)
59
    {
60
        $data = json_decode($responseBody, true);
61
62
        if (null === $data || !is_array($data)) {
63
            throw new TokenResponseException('Unable to parse response.');
64
        } elseif (isset($data['error'])) {
65
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
66
        }
67
68
        $token = new StdOAuth2Token();
69
        $token->setAccessToken($data['access_token']);
70
        $token->setLifeTime($data['expires_in']);
71
72
        if (isset($data['refresh_token'])) {
73
            $token->setRefreshToken($data['refresh_token']);
74
            unset($data['refresh_token']);
75
        }
76
77
        unset($data['access_token']);
78
        unset($data['expires_in']);
79
80
        $token->setExtraParams($data);
81
82
        return $token;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    protected function getExtraOAuthHeaders()
89
    {
90
        return array('Authorization' => 'Basic ' . $this->credentials->getConsumerSecret());
91
    }
92
}
93