Nest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 78
c 0
b 0
f 0
wmc 10
lcom 0
cbo 4
rs 10

5 Methods

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