Netatmo   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 31
dl 0
loc 89
c 1
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parseAccessTokenResponse() 0 26 5
A getAccessTokenEndpoint() 0 3 1
A getAuthorizationMethod() 0 3 1
A __construct() 0 18 2
A getAuthorizationEndpoint() 0 3 1
1
<?php
2
/**
3
 * Netatmo service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7
 *
8
 * @see    https://dev.netatmo.com/doc/
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
 * Netatmo service.
23
 *
24
 * @author  Pedro Amorim <[email protected]>
25
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
26
 *
27
 * @see    https://dev.netatmo.com/doc/
28
 */
29
class Netatmo extends AbstractService
30
{
31
    /**
32
     * SCOPES.
33
     *
34
     * @see https://dev.netatmo.com/doc/authentication/scopes
35
     */
36
37
    // Used to read weather station's data (devicelist, getmeasure)
38
    const SCOPE_STATION_READ = 'read_station';
39
    // Used to read thermostat's data (devicelist, getmeasure, getthermstate)
40
    const SCOPE_THERMOSTAT_READ = 'read_thermostat';
41
    // Used to configure the thermostat (syncschedule, setthermpoint)
42
    const SCOPE_THERMOSTAT_WRITE = 'write_thermostat';
43
44
    public function __construct(
45
        CredentialsInterface $credentials,
46
        ClientInterface $httpClient,
47
        TokenStorageInterface $storage,
48
        $scopes = [],
49
        ?UriInterface $baseApiUri = null
50
    ) {
51
        parent::__construct(
52
            $credentials,
53
            $httpClient,
54
            $storage,
55
            $scopes,
56
            $baseApiUri,
57
            true // use parameter state
58
        );
59
60
        if (null === $baseApiUri) {
61
            $this->baseApiUri = new Uri('https://api.netatmo.net/');
62
        }
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getAuthorizationEndpoint()
69
    {
70
        return new Uri($this->baseApiUri . 'oauth2/authorize');
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getAccessTokenEndpoint()
77
    {
78
        return new Uri($this->baseApiUri . 'oauth2/token');
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function getAuthorizationMethod()
85
    {
86
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected function parseAccessTokenResponse($responseBody)
93
    {
94
        $data = json_decode($responseBody, true);
95
96
        if (null === $data || !is_array($data)) {
97
            throw new TokenResponseException('Unable to parse response.');
98
        } elseif (isset($data['error'])) {
99
            throw new TokenResponseException(
100
                'Error in retrieving token: "' . $data['error'] . '"'
101
            );
102
        }
103
104
        $token = new StdOAuth2Token();
105
        $token->setAccessToken($data['access_token']);
106
        $token->setLifetime($data['expires_in']);
107
108
        if (isset($data['refresh_token'])) {
109
            $token->setRefreshToken($data['refresh_token']);
110
            unset($data['refresh_token']);
111
        }
112
113
        unset($data['access_token'], $data['expires_in']);
114
115
        $token->setExtraParams($data);
116
117
        return $token;
118
    }
119
}
120