Completed
Pull Request — master (#466)
by
unknown
03:50
created

QuickBooks   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 15
lcom 0
cbo 4
dl 0
loc 107
rs 10
c 4
b 1
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequestTokenEndpoint() 0 4 1
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A __construct() 0 19 2
B parseRequestTokenResponse() 0 13 5
B parseAccessTokenResponse() 0 24 4
A request() 0 9 1
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
use OAuth\Common\Consumer\CredentialsInterface;
9
use OAuth\Common\Storage\TokenStorageInterface;
10
use OAuth\Common\Http\Client\ClientInterface;
11
use OAuth\Common\Http\Uri\UriInterface;
12
use OAuth\OAuth1\Signature\SignatureInterface;
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
            throw new TokenResponseException($message);
92
        }
93
94
        $token = new StdOAuth1Token();
95
96
        $token->setRequestToken($data['oauth_token']);
97
        $token->setRequestTokenSecret($data['oauth_token_secret']);
98
        $token->setAccessToken($data['oauth_token']);
99
        $token->setAccessTokenSecret($data['oauth_token_secret']);
100
101
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
102
        unset($data['oauth_token'], $data['oauth_token_secret']);
103
        $token->setExtraParams($data);
104
105
        return $token;
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    public function request(
112
        $path,
113
        $method = 'GET',
114
        $body = null,
115
        array $extraHeaders = array()
116
    ) {
117
        $extraHeaders['Accept'] = 'application/json';
118
        return parent::request($path, $method, $body, $extraHeaders);
119
    }
120
}
121