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

Box::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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