Completed
Pull Request — master (#506)
by Dragonqos
02:52
created

Nest::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
 * Nest service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7
 * @link    https://developer.nest.com/documentation
8
 */
9
10
namespace OAuth\OAuth2\Service;
11
12
use OAuth\OAuth2\Token\StdOAuth2Token;
13
use OAuth\Common\Http\Exception\TokenResponseException;
14
use OAuth\Common\Http\Uri\Uri;
15
16
/**
17
 * Nest service.
18
 *
19
 * @author  Pedro Amorim <[email protected]>
20
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
21
 * @link    https://developer.nest.com/documentation
22
 */
23
class Nest extends AbstractService
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function init()
29
    {
30
        $this->stateParameterInAuthUrl = true;
31
32
        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...
33
            $this->baseApiUri = new Uri('https://developer-api.nest.com/');
34
        }
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getAuthorizationEndpoint()
41
    {
42
        return new Uri('https://home.nest.com/login/oauth2');
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getAccessTokenEndpoint()
49
    {
50
        return new Uri('https://api.home.nest.com/oauth2/access_token');
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function getAuthorizationMethod()
57
    {
58
        return static::AUTHORIZATION_METHOD_QUERY_STRING_V4;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function parseAccessTokenResponse($responseBody)
65
    {
66
        $data = json_decode($responseBody, true);
67
68
        if (null === $data || !is_array($data)) {
69
            throw new TokenResponseException('Unable to parse response.');
70
        } elseif (isset($data['error'])) {
71
            throw new TokenResponseException(
72
                'Error in retrieving token: "' . $data['error'] . '"'
73
            );
74
        }
75
76
        $token = new StdOAuth2Token();
77
        $token->setAccessToken($data['access_token']);
78
        $token->setLifeTime($data['expires_in']);
79
80
        if (isset($data['refresh_token'])) {
81
            $token->setRefreshToken($data['refresh_token']);
82
            unset($data['refresh_token']);
83
        }
84
85
        unset($data['access_token']);
86
        unset($data['expires_in']);
87
88
        $token->setExtraParams($data);
89
90
        return $token;
91
    }
92
}
93