QuickBooks::parseAccessTokenResponse()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 24
c 1
b 0
f 0
rs 9.7666
cc 4
nc 3
nop 1
1
<?php
2
3
namespace OAuth\OAuth1\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\OAuth1\Signature\SignatureInterface;
12
use OAuth\OAuth1\Token\StdOAuth1Token;
13
14
class QuickBooks extends AbstractService
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function __construct(
20
        CredentialsInterface $credentials,
21
        ClientInterface $httpClient,
22
        TokenStorageInterface $storage,
23
        SignatureInterface $signature,
24
        ?UriInterface $baseApiUri = null
25
    ) {
26
        parent::__construct(
27
            $credentials,
28
            $httpClient,
29
            $storage,
30
            $signature,
31
            $baseApiUri
32
        );
33
34
        if (null === $baseApiUri) {
35
            $this->baseApiUri = new Uri('https://quickbooks.api.intuit.com/');
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getRequestTokenEndpoint()
43
    {
44
        return new Uri('https://oauth.intuit.com/oauth/v1/get_request_token');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getAuthorizationEndpoint()
51
    {
52
        return new Uri('https://appcenter.intuit.com/Connect/Begin');
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getAccessTokenEndpoint()
59
    {
60
        return new Uri('https://oauth.intuit.com/oauth/v1/get_access_token');
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function parseRequestTokenResponse($responseBody)
67
    {
68
        parse_str($responseBody, $data);
69
70
        if (null === $data || !is_array($data)) {
71
            throw new TokenResponseException('Unable to parse response.');
72
        } elseif (!isset($data['oauth_callback_confirmed'])
73
            || $data['oauth_callback_confirmed'] !== 'true') {
74
            throw new TokenResponseException('Error in retrieving token.');
75
        }
76
77
        return $this->parseAccessTokenResponse($responseBody);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function parseAccessTokenResponse($responseBody)
84
    {
85
        parse_str($responseBody, $data);
86
87
        if (null === $data || !is_array($data)) {
88
            throw new TokenResponseException('Unable to parse response.');
89
        } elseif (isset($data['error'])) {
90
            $message = 'Error in retrieving token: "' . $data['error'] . '"';
91
92
            throw new TokenResponseException($message);
93
        }
94
95
        $token = new StdOAuth1Token();
96
97
        $token->setRequestToken($data['oauth_token']);
98
        $token->setRequestTokenSecret($data['oauth_token_secret']);
99
        $token->setAccessToken($data['oauth_token']);
100
        $token->setAccessTokenSecret($data['oauth_token_secret']);
101
102
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
103
        unset($data['oauth_token'], $data['oauth_token_secret']);
104
        $token->setExtraParams($data);
105
106
        return $token;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function request(
113
        $path,
114
        $method = 'GET',
115
        $body = null,
116
        array $extraHeaders = []
117
    ) {
118
        $extraHeaders['Accept'] = 'application/json';
119
120
        return parent::request($path, $method, $body, $extraHeaders);
121
    }
122
}
123