Completed
Pull Request — master (#479)
by Andrey
03:26
created

JawboneUP   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 126
rs 10
c 3
b 0
f 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationMethod() 0 4 1
A __construct() 0 13 2
A getAuthorizationUri() 0 21 2
B parseAccessTokenResponse() 0 26 5
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
use OAuth\Common\Consumer\CredentialsInterface;
9
use OAuth\Common\Http\Client\ClientInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\Common\Http\Uri\UriInterface;
12
13
/**
14
 * Jawbone UP service.
15
 *
16
 * @author Andrii Gakhov <[email protected]>
17
 * @link https://jawbone.com/up/developer/authentication
18
 */
19
class JawboneUP extends AbstractService
20
{
21
    /**
22
     * Defined scopes
23
     *
24
     *
25
     * @link https://jawbone.com/up/developer/authentication
26
     */
27
    // general information scopes
28
    const SCOPE_BASIC_READ          = 'basic_read';
29
    const SCOPE_EXTENDED_READ       = 'extended_read';
30
    const SCOPE_LOCATION_READ       = 'location_read';
31
    const SCOPE_FRIENDS_READ        = 'friends_read';
32
    // mood scopes
33
    const SCOPE_MOOD_READ           = 'mood_read';
34
    const SCOPE_MOOD_WRITE          = 'mood_write';
35
    // move scopes
36
    const SCOPE_MOVE_READ           = 'move_read';
37
    const SCOPE_MOVE_WRITE          = 'move_write';
38
    // sleep scopes
39
    const SCOPE_SLEEP_READ          = 'sleep_read';
40
    const SCOPE_SLEEP_WRITE         = 'sleep_write';
41
    // meal scopes
42
    const SCOPE_MEAL_READ           = 'meal_read';
43
    const SCOPE_MEAL_WRITE          = 'meal_write';
44
    // weight scopes
45
    const SCOPE_WEIGHT_READ         = 'weight_read';
46
    const SCOPE_WEIGHT_WRITE        = 'weight_write';
47
    // generic event scopes
48
    const SCOPE_GENERIC_EVENT_READ  = 'generic_event_read';
49
    const SCOPE_GENERIC_EVENT_WRITE = 'generic_event_write';
50
51
52
    public function __construct(
53
        CredentialsInterface $credentials,
54
        ClientInterface $httpClient,
55
        TokenStorageInterface $storage,
56
        $scopes = array(),
57
        UriInterface $baseApiUri = null
58
    ) {
59
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
60
61
        if (null === $baseApiUri) {
62
            $this->baseApiUri = new Uri('https://jawbone.com/nudge/api/v.1.1/');
63
        }
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getAuthorizationUri(array $additionalParameters = array())
70
    {
71
        $parameters = array_merge(
72
            $additionalParameters,
73
            array(
74
                'client_id'     => $this->credentials->getConsumerId(),
75
                'redirect_uri'  => $this->credentials->getCallbackUrl(),
76
                'response_type' => 'code',
77
            )
78
        );
79
80
        $parameters['scope'] = implode(' ', $this->scopes);
81
82
        // Build the url
83
        $url = clone $this->getAuthorizationEndpoint();
84
        foreach ($parameters as $key => $val) {
85
            $url->addToQuery($key, $val);
86
        }
87
88
        return $url;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getAuthorizationEndpoint()
95
    {
96
        return new Uri('https://jawbone.com/auth/oauth2/auth');
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getAccessTokenEndpoint()
103
    {
104
        return new Uri('https://jawbone.com/auth/oauth2/token');
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    protected function getAuthorizationMethod()
111
    {
112
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    protected function parseAccessTokenResponse($responseBody)
119
    {
120
        $data = json_decode($responseBody, true);
121
122
        if (null === $data || !is_array($data)) {
123
            throw new TokenResponseException('Unable to parse response.');
124
        } elseif (isset($data['error'])) {
125
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
126
        }
127
128
        $token = new StdOAuth2Token();
129
        $token->setAccessToken($data['access_token']);
130
        $token->setLifeTime($data['expires_in']);
131
132
        if (isset($data['refresh_token'])) {
133
            $token->setRefreshToken($data['refresh_token']);
134
            unset($data['refresh_token']);
135
        }
136
137
        unset($data['access_token']);
138
        unset($data['expires_in']);
139
140
        $token->setExtraParams($data);
141
142
        return $token;
143
    }
144
}
145