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

QuickBooks::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\OAuth1\Service;
4
5
use OAuth\OAuth1\Token\StdOAuth1Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
class QuickBooks extends AbstractService
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function init()
15
    {
16
        if (null === $this->baseApiUri) {
17
            $this->baseApiUri = new Uri('https://quickbooks.api.intuit.com/');
18
        }
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getRequestTokenEndpoint()
25
    {
26
        return new Uri('https://oauth.intuit.com/oauth/v1/get_request_token');
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getAuthorizationEndpoint()
33
    {
34
        return new Uri('https://appcenter.intuit.com/Connect/Begin');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getAccessTokenEndpoint()
41
    {
42
        return new Uri('https://oauth.intuit.com/oauth/v1/get_access_token');
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function parseRequestTokenResponse($responseBody)
49
    {
50
        parse_str($responseBody, $data);
51
52
        if (null === $data || !is_array($data)) {
53
            throw new TokenResponseException('Unable to parse response.');
54
        } elseif (!isset($data['oauth_callback_confirmed'])
55
            || $data['oauth_callback_confirmed'] !== 'true') {
56
            throw new TokenResponseException('Error in retrieving token.');
57
        }
58
59
        return $this->parseAccessTokenResponse($responseBody);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function parseAccessTokenResponse($responseBody)
66
    {
67
        parse_str($responseBody, $data);
68
69
        if (null === $data || !is_array($data)) {
70
            throw new TokenResponseException('Unable to parse response.');
71
        } elseif (isset($data['error'])) {
72
            $message = 'Error in retrieving token: "' . $data['error'] . '"';
73
            throw new TokenResponseException($message);
74
        }
75
76
        $token = new StdOAuth1Token();
77
78
        $token->setRequestToken($data['oauth_token']);
79
        $token->setRequestTokenSecret($data['oauth_token_secret']);
80
        $token->setAccessToken($data['oauth_token']);
81
        $token->setAccessTokenSecret($data['oauth_token_secret']);
82
83
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
84
        unset($data['oauth_token'], $data['oauth_token_secret']);
85
        $token->setExtraParams($data);
86
87
        return $token;
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function request(
94
        $path,
95
        $method = 'GET',
96
        $body = null,
97
        array $extraHeaders = array()
98
    ) {
99
        $extraHeaders['Accept'] = 'application/json';
100
        return parent::request($path, $method, $body, $extraHeaders);
101
    }
102
}
103