JawboneUP   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 12
eloc 47
dl 0
loc 121
c 3
b 0
f 2
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthorizationEndpoint() 0 3 1
A getAuthorizationUri() 0 20 2
A __construct() 0 11 2
A getAccessTokenEndpoint() 0 3 1
A getAuthorizationMethod() 0 3 1
A parseAccessTokenResponse() 0 24 5
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth2\Token\StdOAuth2Token;
12
13
/**
14
 * Jawbone UP service.
15
 *
16
 * @author Andrii Gakhov <[email protected]>
17
 *
18
 * @see https://jawbone.com/up/developer/authentication
19
 */
20
class JawboneUP extends AbstractService
21
{
22
    /**
23
     * Defined scopes.
24
     *
25
     * @see 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
    public function __construct(
52
        CredentialsInterface $credentials,
53
        ClientInterface $httpClient,
54
        TokenStorageInterface $storage,
55
        $scopes = [],
56
        ?UriInterface $baseApiUri = null
57
    ) {
58
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
59
60
        if (null === $baseApiUri) {
61
            $this->baseApiUri = new Uri('https://jawbone.com/nudge/api/v.1.1/');
62
        }
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getAuthorizationUri(array $additionalParameters = [])
69
    {
70
        $parameters = array_merge(
71
            $additionalParameters,
72
            [
73
                'client_id' => $this->credentials->getConsumerId(),
74
                'redirect_uri' => $this->credentials->getCallbackUrl(),
75
                'response_type' => 'code',
76
            ]
77
        );
78
79
        $parameters['scope'] = implode(' ', $this->scopes);
80
81
        // Build the url
82
        $url = clone $this->getAuthorizationEndpoint();
83
        foreach ($parameters as $key => $val) {
84
            $url->addToQuery($key, $val);
85
        }
86
87
        return $url;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getAuthorizationEndpoint()
94
    {
95
        return new Uri('https://jawbone.com/auth/oauth2/auth');
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getAccessTokenEndpoint()
102
    {
103
        return new Uri('https://jawbone.com/auth/oauth2/token');
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    protected function getAuthorizationMethod()
110
    {
111
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    protected function parseAccessTokenResponse($responseBody)
118
    {
119
        $data = json_decode($responseBody, true);
120
121
        if (null === $data || !is_array($data)) {
122
            throw new TokenResponseException('Unable to parse response.');
123
        } elseif (isset($data['error'])) {
124
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
125
        }
126
127
        $token = new StdOAuth2Token();
128
        $token->setAccessToken($data['access_token']);
129
        $token->setLifeTime($data['expires_in']);
130
131
        if (isset($data['refresh_token'])) {
132
            $token->setRefreshToken($data['refresh_token']);
133
            unset($data['refresh_token']);
134
        }
135
136
        unset($data['access_token'], $data['expires_in']);
137
138
        $token->setExtraParams($data);
139
140
        return $token;
141
    }
142
}
143