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

Box::getAccessTokenEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
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
 * Box service.
15
 *
16
 * @author Antoine Corcy <[email protected]>
17
 * @link https://developers.box.com/oauth/
18
 */
19
class Box extends AbstractService
20
{
21
    public function __construct(
22
        CredentialsInterface $credentials,
23
        ClientInterface $httpClient,
24
        TokenStorageInterface $storage,
25
        $scopes = array(),
26
        UriInterface $baseApiUri = null
27
    ) {
28
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
29
30
        if (null === $baseApiUri) {
31
            $this->baseApiUri = new Uri('https://api.box.com/2.0/');
32
        }
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getAuthorizationEndpoint()
39
    {
40
        return new Uri('https://www.box.com/api/oauth2/authorize');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getAccessTokenEndpoint()
47
    {
48
        return new Uri('https://www.box.com/api/oauth2/token');
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function getAuthorizationMethod()
55
    {
56
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function parseAccessTokenResponse($responseBody)
63
    {
64
        $data = json_decode($responseBody, true);
65
66
        if (null === $data || !is_array($data)) {
67
            throw new TokenResponseException('Unable to parse response.');
68
        } elseif (isset($data['error'])) {
69
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
70
        }
71
72
        $token = new StdOAuth2Token();
73
        $token->setAccessToken($data['access_token']);
74
        $token->setLifeTime($data['expires_in']);
75
76
        if (isset($data['refresh_token'])) {
77
            $token->setRefreshToken($data['refresh_token']);
78
            unset($data['refresh_token']);
79
        }
80
81
        unset($data['access_token']);
82
        unset($data['expires_in']);
83
84
        $token->setExtraParams($data);
85
86
        return $token;
87
    }
88
}
89