Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

RunKeeper::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
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
9
/**
10
 * RunKeeper service.
11
 *
12
 * @link http://runkeeper.com/developer/healthgraph/registration-authorization
13
 */
14
class RunKeeper extends AbstractService
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected function init()
20
    {
21
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
22
            $this->baseApiUri = new Uri('https://api.runkeeper.com/');
23
        }
24
    }
25
    
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getAuthorizationUri(array $additionalParameters = array())
30
    {
31
        $parameters = array_merge(
32
            $additionalParameters,
33
            array(
34
                'client_id'     => $this->credentials->getConsumerId(),
35
                'redirect_uri'  => $this->credentials->getCallbackUrl(),
36
                'response_type' => 'code',
37
            )
38
        );
39
40
        $parameters['scope'] = implode(' ', $this->scopes);
41
42
        // Build the url
43
        $url = clone $this->getAuthorizationEndpoint();
44
        foreach ($parameters as $key => $val) {
45
            $url->addToQuery($key, $val);
46
        }
47
48
        return $url;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getAuthorizationEndpoint()
55
    {
56
        return new Uri('https://runkeeper.com/apps/authorize');
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getAccessTokenEndpoint()
63
    {
64
        return new Uri('https://runkeeper.com/apps/token');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function getAuthorizationMethod()
71
    {
72
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function parseAccessTokenResponse($responseBody)
79
    {
80
        $data = json_decode($responseBody, true);
81
82
        if (null === $data || !is_array($data)) {
83
            throw new TokenResponseException('Unable to parse response.');
84
        } elseif (isset($data['error'])) {
85
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
86
        }
87
88
        $token = new StdOAuth2Token();
89
        $token->setAccessToken($data['access_token']);
90
91
        unset($data['access_token']);
92
93
        $token->setExtraParams($data);
94
95
        return $token;
96
    }
97
}
98