Completed
Pull Request — master (#467)
by
unknown
03:05
created

Netatmo   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 91
rs 10
c 1
b 0
f 1

5 Methods

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