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

Heroku::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
/**
10
 * Heroku service.
11
 *
12
 * @author Thomas Welton <[email protected]>
13
 * @link https://devcenter.heroku.com/articles/oauth
14
 */
15
class Heroku extends AbstractService
16
{
17
    /**
18
     * Defined scopes
19
     * @link https://devcenter.heroku.com/articles/oauth#scopes
20
     */
21
    const SCOPE_GLOBAL          = 'global';
22
    const SCOPE_IDENTITY        = 'identity';
23
    const SCOPE_READ            = 'read';
24
    const SCOPE_WRITE           = 'write';
25
    const SCOPE_READ_PROTECTED  = 'read-protected';
26
    const SCOPE_WRITE_PROTECTED = 'write-protected';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function init()
32
    {
33
        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...
34
            $this->baseApiUri = new Uri('https://api.heroku.com/');
35
        }
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getAuthorizationEndpoint()
42
    {
43
        return new Uri('https://id.heroku.com/oauth/authorize');
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getAccessTokenEndpoint()
50
    {
51
        return new Uri('https://id.heroku.com/oauth/token');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function getAuthorizationMethod()
58
    {
59
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function parseAccessTokenResponse($responseBody)
66
    {
67
        $data = json_decode($responseBody, true);
68
69
        if (null === $data || !is_array($data)) {
70
            throw new TokenResponseException('Unable to parse response.');
71
        } elseif (isset($data['error_description']) || isset($data['error'])) {
72
            throw new TokenResponseException(
73
                sprintf(
74
                    'Error in retrieving token: "%s"',
75
                    isset($data['error_description']) ? $data['error_description'] : $data['error']
76
                )
77
            );
78
        }
79
80
        $token = new StdOAuth2Token();
81
        $token->setAccessToken($data['access_token']);
82
        $token->setLifeTime($data['expires_in']);
83
84
        if (isset($data['refresh_token'])) {
85
            $token->setRefreshToken($data['refresh_token']);
86
            unset($data['refresh_token']);
87
        }
88
89
        unset($data['access_token']);
90
        unset($data['expires_in']);
91
92
        $token->setExtraParams($data);
93
94
        return $token;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function getExtraOAuthHeaders()
101
    {
102
        return array('Accept' => 'application/vnd.heroku+json; version=3');
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    protected function getExtraApiHeaders()
109
    {
110
        return array('Accept' => 'application/vnd.heroku+json; version=3', 'Content-Type' => 'application/json');
111
    }
112
}
113